From ad88f1694a6c48442b3f20c688e6b5438986d5d0 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 23 Sep 2023 14:03:53 -0400 Subject: [PATCH] Saving work --- coursera/{ => intro_course1}/week2/findian.go | 0 coursera/{ => intro_course1}/week2/trunc.go | 0 coursera/{ => intro_course1}/week3/array.go | 0 .../week3/assignments/slice.go | 0 coursera/{ => intro_course1}/week3/maps.go | 0 coursera/{ => intro_course1}/week3/slice.go | 0 coursera/{ => intro_course1}/week3/struct.go | 0 .../week4/assignment/makejson.go | 0 .../week4/assignment/names.txt | 0 .../week4/assignment/read.go | 0 .../week4/json_mashaling.go | 0 coursera/{ => intro_course1}/week4/net.go | 0 .../week1/assignment/bubble_sort.go | 50 +++++++++++++++ .../week1/assignment/bubble_sort_test.go | 34 ++++++++++ .../second_course/week1/assignment/go.mod | 3 + .../week2/assignment/calc_physics.go | 49 ++++++++++++++ .../week2/assignment/kinematics.go | 64 +++++++++++++++++++ 17 files changed, 200 insertions(+) rename coursera/{ => intro_course1}/week2/findian.go (100%) rename coursera/{ => intro_course1}/week2/trunc.go (100%) rename coursera/{ => intro_course1}/week3/array.go (100%) rename coursera/{ => intro_course1}/week3/assignments/slice.go (100%) rename coursera/{ => intro_course1}/week3/maps.go (100%) rename coursera/{ => intro_course1}/week3/slice.go (100%) rename coursera/{ => intro_course1}/week3/struct.go (100%) rename coursera/{ => intro_course1}/week4/assignment/makejson.go (100%) rename coursera/{ => intro_course1}/week4/assignment/names.txt (100%) rename coursera/{ => intro_course1}/week4/assignment/read.go (100%) rename coursera/{ => intro_course1}/week4/json_mashaling.go (100%) rename coursera/{ => intro_course1}/week4/net.go (100%) create mode 100644 coursera/second_course/week1/assignment/bubble_sort.go create mode 100644 coursera/second_course/week1/assignment/bubble_sort_test.go create mode 100644 coursera/second_course/week1/assignment/go.mod create mode 100644 coursera/second_course/week2/assignment/calc_physics.go create mode 100644 coursera/second_course/week2/assignment/kinematics.go diff --git a/coursera/week2/findian.go b/coursera/intro_course1/week2/findian.go similarity index 100% rename from coursera/week2/findian.go rename to coursera/intro_course1/week2/findian.go diff --git a/coursera/week2/trunc.go b/coursera/intro_course1/week2/trunc.go similarity index 100% rename from coursera/week2/trunc.go rename to coursera/intro_course1/week2/trunc.go diff --git a/coursera/week3/array.go b/coursera/intro_course1/week3/array.go similarity index 100% rename from coursera/week3/array.go rename to coursera/intro_course1/week3/array.go diff --git a/coursera/week3/assignments/slice.go b/coursera/intro_course1/week3/assignments/slice.go similarity index 100% rename from coursera/week3/assignments/slice.go rename to coursera/intro_course1/week3/assignments/slice.go diff --git a/coursera/week3/maps.go b/coursera/intro_course1/week3/maps.go similarity index 100% rename from coursera/week3/maps.go rename to coursera/intro_course1/week3/maps.go diff --git a/coursera/week3/slice.go b/coursera/intro_course1/week3/slice.go similarity index 100% rename from coursera/week3/slice.go rename to coursera/intro_course1/week3/slice.go diff --git a/coursera/week3/struct.go b/coursera/intro_course1/week3/struct.go similarity index 100% rename from coursera/week3/struct.go rename to coursera/intro_course1/week3/struct.go diff --git a/coursera/week4/assignment/makejson.go b/coursera/intro_course1/week4/assignment/makejson.go similarity index 100% rename from coursera/week4/assignment/makejson.go rename to coursera/intro_course1/week4/assignment/makejson.go diff --git a/coursera/week4/assignment/names.txt b/coursera/intro_course1/week4/assignment/names.txt similarity index 100% rename from coursera/week4/assignment/names.txt rename to coursera/intro_course1/week4/assignment/names.txt diff --git a/coursera/week4/assignment/read.go b/coursera/intro_course1/week4/assignment/read.go similarity index 100% rename from coursera/week4/assignment/read.go rename to coursera/intro_course1/week4/assignment/read.go diff --git a/coursera/week4/json_mashaling.go b/coursera/intro_course1/week4/json_mashaling.go similarity index 100% rename from coursera/week4/json_mashaling.go rename to coursera/intro_course1/week4/json_mashaling.go diff --git a/coursera/week4/net.go b/coursera/intro_course1/week4/net.go similarity index 100% rename from coursera/week4/net.go rename to coursera/intro_course1/week4/net.go diff --git a/coursera/second_course/week1/assignment/bubble_sort.go b/coursera/second_course/week1/assignment/bubble_sort.go new file mode 100644 index 0000000..5c72e8b --- /dev/null +++ b/coursera/second_course/week1/assignment/bubble_sort.go @@ -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) +} diff --git a/coursera/second_course/week1/assignment/bubble_sort_test.go b/coursera/second_course/week1/assignment/bubble_sort_test.go new file mode 100644 index 0000000..4f1c143 --- /dev/null +++ b/coursera/second_course/week1/assignment/bubble_sort_test.go @@ -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) + } + } + +} diff --git a/coursera/second_course/week1/assignment/go.mod b/coursera/second_course/week1/assignment/go.mod new file mode 100644 index 0000000..18bdc2f --- /dev/null +++ b/coursera/second_course/week1/assignment/go.mod @@ -0,0 +1,3 @@ +module assignment + +go 1.21.0 diff --git a/coursera/second_course/week2/assignment/calc_physics.go b/coursera/second_course/week2/assignment/calc_physics.go new file mode 100644 index 0000000..7fb913a --- /dev/null +++ b/coursera/second_course/week2/assignment/calc_physics.go @@ -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)) +} diff --git a/coursera/second_course/week2/assignment/kinematics.go b/coursera/second_course/week2/assignment/kinematics.go new file mode 100644 index 0000000..e929ea7 --- /dev/null +++ b/coursera/second_course/week2/assignment/kinematics.go @@ -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)) + +}