Saving work
parent
c7de4c2f93
commit
ad88f1694a
@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Swap(target []int, index int) {
|
||||
// Swaps the position of index with index+1 in a slice of integers
|
||||
temp := target[index]
|
||||
target[index] = target[index+1]
|
||||
target[index+1] = temp
|
||||
}
|
||||
|
||||
func BubbleSort(target []int) {
|
||||
// Performs Bubble Sort on a target slice of integers
|
||||
for n := len(target); n > 1; n-- {
|
||||
for i := 1; i < n; i++ {
|
||||
if target[i] < target[i-1] {
|
||||
Swap(target, i-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ParseIntegers(integers string) []int {
|
||||
_integers := []int{}
|
||||
for _, v := range strings.Split(integers, " ") {
|
||||
_int, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to parse integer %s", v)
|
||||
}
|
||||
_integers = append(_integers, _int)
|
||||
}
|
||||
return _integers
|
||||
}
|
||||
|
||||
func main() {
|
||||
var userIntegers []int
|
||||
inputReader := bufio.NewReader(os.Stdin)
|
||||
fmt.Println("Type in a sequence of up to 10 integers separated by white space:")
|
||||
integersString, _ := inputReader.ReadString('\n')
|
||||
userIntegers = ParseIntegers(strings.Trim(integersString, "\n"))
|
||||
BubbleSort(userIntegers)
|
||||
|
||||
fmt.Println(userIntegers)
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSwap(t *testing.T) {
|
||||
testIndex := 0
|
||||
expected := []int{1, 3, 5, 9, 7}
|
||||
actual := []int{3, 1, 5, 9, 7}
|
||||
Swap(actual, testIndex)
|
||||
|
||||
if expected[testIndex] != actual[testIndex] && expected[testIndex+1] != actual[testIndex+1] {
|
||||
t.Errorf("Swap function failed to swap elements at index %d and %d", testIndex, testIndex+1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBubbleSort(t *testing.T) {
|
||||
expected := []int{1, 3, 5, 7, 9}
|
||||
actual := []int{3, 1, 5, 9, 7}
|
||||
BubbleSort(actual)
|
||||
|
||||
if len(expected) != len(actual) {
|
||||
t.Errorf("Error: Expected len: %d, Actual len: %d", len(expected), len(actual))
|
||||
}
|
||||
|
||||
for i, v := range actual {
|
||||
if v != expected[i] {
|
||||
t.Errorf("Value: %d at actual[%d] does not match %d at expected[%d]", v, i, expected[i], i)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
module assignment
|
||||
|
||||
go 1.21.0
|
@ -0,0 +1,49 @@
|
||||
// NOT DREWS SOLUTION BUT YOU CAN USED IT AS ANOTHER EXAMPLE
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GenDisplaceFn(acceleration, initialVelocity, initialDisplacement float64) func(float64) float64 {
|
||||
calcDisplace := func(time float64) float64 {
|
||||
return 0.5*acceleration*math.Pow(time, 2) + initialVelocity*time + initialDisplacement
|
||||
}
|
||||
return calcDisplace
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
acceleration float64
|
||||
initialVelocity float64
|
||||
initialDisplacement float64
|
||||
time float64
|
||||
inputAcc string
|
||||
inputVel string
|
||||
inputDisp string
|
||||
inputTime string
|
||||
)
|
||||
|
||||
fmt.Printf("Please enter acceleration\n")
|
||||
_, _ = fmt.Scanln(&inputAcc)
|
||||
inputAcceralation, _ := strconv.Atoi(inputAcc)
|
||||
acceleration = float64(inputAcceralation)
|
||||
fmt.Printf("Please enter Velocity\n")
|
||||
_, _ = fmt.Scan(&inputVel)
|
||||
inputVelocity, _ := strconv.Atoi(inputVel)
|
||||
initialVelocity = float64(inputVelocity)
|
||||
fmt.Printf("Please enter Displacement\n")
|
||||
_, _ = fmt.Scan(&inputDisp)
|
||||
inputDisplacement, _ := strconv.Atoi(inputDisp)
|
||||
initialDisplacement = float64(inputDisplacement)
|
||||
fmt.Printf("Please enter time\n")
|
||||
_, _ = fmt.Scan(&inputTime)
|
||||
inputtime, _ := strconv.Atoi(inputTime)
|
||||
time = float64(inputtime)
|
||||
|
||||
fn := GenDisplaceFn(acceleration, initialVelocity, initialDisplacement)
|
||||
fmt.Println(fn(time))
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var ascii_intro string = `
|
||||
_ ___ _ _ _____ _
|
||||
| | / (_) | | (_) / ___| | |
|
||||
| |/ / _ _ __ ___ _ __ ___ __ _| |_ _ ___ ___ \ ` + "`" + `--. ___ | |_ _____ _ __
|
||||
| \| | '_ \ / _ \ '_ ` + "`" + ` _ \ / _` + "`" + ` | __| |/ __/ __| ` + "`" + `--. \/ _ \| \ \ / / _ \ '__|
|
||||
| |\ \ | | | | __/ | | | | | (_| | |_| | (__\__ \ /\__/ / (_) | |\ V / __/ |
|
||||
\_| \_/_|_| |_|\___|_| |_| |_|\__,_|\__|_|\___|___/ \____/ \___/|_| \_/ \___|_|
|
||||
`
|
||||
|
||||
// GenDisplaceFn produces functions that solves a linear kinematics scenario for a given time value.
|
||||
// `acceleration` is a float64 number of the units the meters per second squared (m/s^2)
|
||||
// `initialVelocity` is a float64 number of the units meters per second (m/s)
|
||||
// `initialDisplacement` is a float64 number in the unit of meters(m) refering to the starting position of
|
||||
// object under consideration
|
||||
// s = 1/2*a*t^2 +vo*t+so
|
||||
// vo is initial displacement so initial displacement
|
||||
func GenDisplaceFn(acceleration float64, initialVelocity float64, intialDisplacement float64) func(float64) float64 {
|
||||
displacementFunc := func(t float64) float64 {
|
||||
return (0.5*acceleration*t*t + initialVelocity*t + intialDisplacement)
|
||||
}
|
||||
return displacementFunc
|
||||
}
|
||||
|
||||
func processUserInput(value string) float64 {
|
||||
|
||||
userFloat, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
fmt.Println("Opps you entered and incorrect input! Please enter float values only.")
|
||||
os.Exit(1)
|
||||
}
|
||||
return userFloat
|
||||
}
|
||||
|
||||
func main() {
|
||||
var acceleration, initialVelocity, initialDisplacement, displaceTime float64
|
||||
var userInput string
|
||||
fmt.Println(ascii_intro)
|
||||
fmt.Println("Ok cowboy! Let's solve some linear kinematics.")
|
||||
fmt.Println("Give me a value for \"acceleration\":")
|
||||
fmt.Scan(&userInput)
|
||||
acceleration = processUserInput(userInput)
|
||||
fmt.Println("Give me a value for \"initial velocity\":")
|
||||
fmt.Scan(&userInput)
|
||||
initialVelocity = processUserInput(userInput)
|
||||
fmt.Println("Give me a value for \"initial displacement\":")
|
||||
fmt.Scan(&userInput)
|
||||
initialDisplacement = processUserInput(userInput)
|
||||
|
||||
displacementFn := GenDisplaceFn(acceleration, initialVelocity, initialDisplacement)
|
||||
|
||||
fmt.Println("What value of \"time\" will we calculate displacement for?:")
|
||||
fmt.Scan(&userInput)
|
||||
displaceTime = processUserInput(userInput)
|
||||
fmt.Println(displacementFn(displaceTime))
|
||||
|
||||
}
|
Loading…
Reference in New Issue