From 6d9a7b168cf72ab92ec9232a98e57170c4f619c3 Mon Sep 17 00:00:00 2001 From: Jon Calhoun Date: Fri, 13 Oct 2017 12:42:53 -0400 Subject: [PATCH 1/3] Finished p1 --- main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ problems.csv | 12 +---------- 2 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..1a735a5 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "os" + "strings" +) + +func main() { + csvFilename := flag.String("csv", "problems.csv", "a csv file in the format of 'question,answer'") + flag.Parse() + + file, err := os.Open(*csvFilename) + if err != nil { + exit(fmt.Sprintf("Failed to open the CSV file: %s\n", *csvFilename)) + } + r := csv.NewReader(file) + lines, err := r.ReadAll() + if err != nil { + exit("Failed to parse the provided CSV file.") + } + problems := parseLines(lines) + + correct := 0 + for i, p := range problems { + fmt.Printf("Problem #%d: %s = \n", i+1, p.q) + var answer string + fmt.Scanf("%s\n", &answer) + if answer == p.a { + correct++ + } + } + + fmt.Printf("You scored %d out of %d.\n", correct, len(problems)) +} + +func parseLines(lines [][]string) []problem { + ret := make([]problem, len(lines)) + for i, line := range lines { + ret[i] = problem{ + q: line[0], + a: strings.TrimSpace(line[1]), + } + } + return ret +} + +type problem struct { + q string + a string +} + +func exit(msg string) { + fmt.Println(msg) + os.Exit(1) +} diff --git a/problems.csv b/problems.csv index 11506cb..610d2e0 100644 --- a/problems.csv +++ b/problems.csv @@ -1,12 +1,2 @@ -5+5,10 +5+5, 10 1+1,2 -8+3,11 -1+2,3 -8+6,14 -3+1,4 -1+4,5 -5+1,6 -2+3,5 -3+3,6 -2+4,6 -5+2,7 From c7609c1e50f86f450bd69551dbf8109b07026419 Mon Sep 17 00:00:00 2001 From: Jon Calhoun Date: Fri, 13 Oct 2017 15:25:18 -0400 Subject: [PATCH 2/3] Reverting the problems csv --- problems.csv | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/problems.csv b/problems.csv index 610d2e0..11506cb 100644 --- a/problems.csv +++ b/problems.csv @@ -1,2 +1,12 @@ -5+5, 10 +5+5,10 1+1,2 +8+3,11 +1+2,3 +8+6,14 +3+1,4 +1+4,5 +5+1,6 +2+3,5 +3+3,6 +2+4,6 +5+2,7 From cffd0a1c60deb680187decefd1306ad908f27ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sin=C3=A9sio=20Neto?= Date: Mon, 22 Jul 2019 10:12:48 -0300 Subject: [PATCH 3/3] add sinetoami's solution for Part 1 (#45) --- students/sinetoami/main.go | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 students/sinetoami/main.go diff --git a/students/sinetoami/main.go b/students/sinetoami/main.go new file mode 100644 index 0000000..054e78a --- /dev/null +++ b/students/sinetoami/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "os" +) + +type Problem struct { + question, answer string +} + +func main() { + filename := flag.String("csv", "problems.csv", "CSV file") + file, err := os.Open(*filename) + + if err != nil { + fmt.Errorf("could not open file: %v", err) + } + defer file.Close() + + lines, err := csv.NewReader(file).ReadAll() + if err != nil { + fmt.Errorf("could not read file: %v", err) + } + + problems := []Problem{} + for _, line := range lines { + problems = append(problems, Problem{line[0], line[1]}) + } + + score := 0 + for i, problem := range problems { + userAnswer := "" + fmt.Printf("Problem #%d: %s = ", i+1, problem.question) + fmt.Scan(&userAnswer) + if userAnswer == problem.answer { + score++ + } + } + + fmt.Printf("You scored %d out of %d.", score, len(problems)) +}