From 9fe6dcac42e59a545ab1d713972915b555e32128 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Wed, 18 Oct 2023 20:46:25 -0400 Subject: [PATCH] working with average --- averageHeadFirstSolution.go | 24 ++++++++++++++++++++++++ average.go => averageMySolution.go | 9 +-------- error.go | 10 ++++++++++ go.mod | 1 + go.sum | 2 ++ playground.go | 9 ++++++--- 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 averageHeadFirstSolution.go rename average.go => averageMySolution.go (88%) create mode 100644 error.go diff --git a/averageHeadFirstSolution.go b/averageHeadFirstSolution.go new file mode 100644 index 0000000..4db9977 --- /dev/null +++ b/averageHeadFirstSolution.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "strings" + + "git.linuxhg.com/john-okeefe/datafile" + "git.linuxhg.com/john-okeefe/keyboard" +) + +func averageHeadFirstSolution() { + fmt.Printf("What is the name of the file: ") + fileString, err := keyboard.GetString() + Error(err) + fileString = strings.TrimSpace(fileString) + numbers, err := datafile.GetFloats(fileString) + Error(err) + var sum float64 = 0 + for _, number := range numbers { + sum += number + } + sampleCount := float64(len(numbers)) + fmt.Printf("Average: %0.2f\n", sum/sampleCount) +} diff --git a/average.go b/averageMySolution.go similarity index 88% rename from average.go rename to averageMySolution.go index 192dbdd..506dc47 100644 --- a/average.go +++ b/averageMySolution.go @@ -11,14 +11,7 @@ import ( "git.linuxhg.com/john-okeefe/keyboard" ) -func Error(err error) { - if err != nil { - log.Fatal(err) - } - return -} - -func average() { +func averageMySolution() { fmt.Printf("What is the name of the file: ") fileString, err := keyboard.GetString() Error(err) diff --git a/error.go b/error.go new file mode 100644 index 0000000..a54a5fa --- /dev/null +++ b/error.go @@ -0,0 +1,10 @@ +package main + +import "log" + +func Error(err error) { + if err != nil { + log.Fatal(err) + } + return +} diff --git a/go.mod b/go.mod index d949887..3c9d49f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( ) require ( + git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect diff --git a/go.sum b/go.sum index 813900f..93565f5 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627 h1:CssED3WO+ie9309BqxqHWPzU2dJiVgn/M6m2Xi9B0mg= +git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627/go.mod h1:HexHt1UMGUDMV5bhB81nAKdvGaCnxKA2upRd7cV8laI= git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e h1:oCGqHZUhesu14KQUELz0jxIPnL/KcGFhsmNnKoAZMo4= git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e/go.mod h1:zxW/S0TzdzmyfloJ0ZHwMaTjMgfd8hK8TycSEAyMRkY= git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016161119-714c103574cc h1:J5VWew9I6gCCYVxQ/5NEtsD4wcMZcc4e05V44OiSNi4= diff --git a/playground.go b/playground.go index bd89c0d..58d474f 100644 --- a/playground.go +++ b/playground.go @@ -28,8 +28,10 @@ func main() { double() case "Convert To Celsius": toCelsius() - case "Get the Average": - average() + case "Get the Average My Solution": + averageMySolution() + case "Get the Average Head First Solution": + averageHeadFirstSolution() } return nil }) @@ -42,7 +44,8 @@ func main() { menu.Option("myIntPointer", nil, false, nil) menu.Option("Double", nil, false, nil) menu.Option("Convert To Celsius", nil, false, nil) - menu.Option("Get the Average", nil, false, nil) + menu.Option("Get the Average My Solution", nil, false, nil) + menu.Option("Get the Average Head First Solution", nil, false, nil) err := menu.Run() if err != nil { log.Fatal(err)