From e959ad0bb3bab172dc8e2d0d45734fb12ae1857e Mon Sep 17 00:00:00 2001 From: Soumil-Bhattacharya Date: Tue, 16 Jan 2024 22:57:15 +0530 Subject: [PATCH 1/4] Completed Part 1 --- go.mod | 3 +++ main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d61ce8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gophercises/quiz + +go 1.21.6 diff --git a/main.go b/main.go new file mode 100644 index 0000000..26a962f --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "os" + "strings" +) + +type problem struct { + q string + a string +} + +func exit(msg string) { + fmt.Println(msg) + os.Exit(1) +} + +func parseLines(lines [][]string) []problem { + res := make([]problem, len(lines)) + + for i, line := range lines { + res[i] = problem{ + q: line[0], + a: strings.TrimSpace(line[1]), + } + } + + return res +} + +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 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", correct, len(problems)) +} From 0f1f2e70e8d8873a41da234ecf663d320c038a3c Mon Sep 17 00:00:00 2001 From: Soumil-Bhattacharya Date: Tue, 16 Apr 2024 02:32:05 +0530 Subject: [PATCH 2/4] Completed Part 2 --- main.go | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 26a962f..e087830 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "strings" + "time" ) type problem struct { @@ -32,7 +33,12 @@ func parseLines(lines [][]string) []problem { } func main() { - csvFileName := flag.String("csv", "problems.csv", "a csv file in the format of 'question, answer'") + csvFileName := flag.String( + "csv", + "problems.csv", + "a csv file in the format of 'question, answer'", + ) + timeLimit := flag.Int("limit", 30, "the time limit for the quiz in seconds") flag.Parse() file, err := os.Open(*csvFileName) @@ -48,16 +54,34 @@ func main() { } problems := parseLines(lines) + timer := time.NewTimer(time.Duration(*timeLimit) * time.Second) + 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) + problemLoop: + for index, problem := range problems { + + fmt.Printf("Problem #%d: %s = ", index+1, problem.q) + answerCh := make(chan string) + + go func() { + var answer string + fmt.Scanf("%s\n", &answer) + answerCh <- answer + }() + select { + case <-timer.C: + fmt.Println() + break problemLoop + case answer := <- answerCh : + + if problem.a == answer { + correct++ + } - if answer == p.a { - correct++ } + } + // <-timer.C - fmt.Printf("You scored %d out of %d", correct, len(problems)) + fmt.Printf("\nYou scored %d out of %d", correct, len(problems)) } From 28dee511d0e1f992f7bfbd2fdf9711cdd005e1be Mon Sep 17 00:00:00 2001 From: Soumil-Bhattacharya Date: Tue, 16 Apr 2024 19:25:03 +0530 Subject: [PATCH 3/4] Completed Bonus Exercises 1. Added string trimming and cleanup to help ensure correct with extra whitespace, capitalization, etc are not considered incorrect. 2. -shuffle flag shuffles the quiz order each time is run --- main.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index e087830..790c227 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/csv" "flag" "fmt" + "math/rand" "os" "strings" "time" @@ -14,6 +15,15 @@ type problem struct { a string } +// Fisher-Yates shuffle algorithm +func Shuffle(data []problem) { + random := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < len(data); i++ { + r := random.Intn(i + 1) + data[i], data[r] = data[r], data[i] + } +} + func exit(msg string) { fmt.Println(msg) os.Exit(1) @@ -39,6 +49,8 @@ func main() { "a csv file in the format of 'question, answer'", ) timeLimit := flag.Int("limit", 30, "the time limit for the quiz in seconds") + + shuffle := flag.Bool("shuffle", false, "shuffle order of the questions") flag.Parse() file, err := os.Open(*csvFileName) @@ -54,25 +66,31 @@ func main() { } problems := parseLines(lines) + if *shuffle { + Shuffle(problems) + } + timer := time.NewTimer(time.Duration(*timeLimit) * time.Second) correct := 0 - problemLoop: +problemLoop: for index, problem := range problems { fmt.Printf("Problem #%d: %s = ", index+1, problem.q) - answerCh := make(chan string) + answerCh := make(chan string) - go func() { - var answer string + go func() { + var answer string fmt.Scanf("%s\n", &answer) - answerCh <- answer - }() + answer = strings.TrimSpace(answer) + answer = strings.ToUpper(answer) + answerCh <- answer + }() select { case <-timer.C: - fmt.Println() - break problemLoop - case answer := <- answerCh : + fmt.Println() + break problemLoop + case answer := <-answerCh: if problem.a == answer { correct++ From eaa7cfb9f82ebe7d21bb03d3ff13ad3aab9e4e65 Mon Sep 17 00:00:00 2001 From: Soumil-Bhattacharya Date: Thu, 18 Apr 2024 02:08:02 +0530 Subject: [PATCH 4/4] Added Soumil-Bhattacharya's solution --- main.go => students/Soumil-Bhattacharya/main.go | 0 students/Soumil-Bhattacharya/problems.csv | 12 ++++++++++++ 2 files changed, 12 insertions(+) rename main.go => students/Soumil-Bhattacharya/main.go (100%) create mode 100644 students/Soumil-Bhattacharya/problems.csv diff --git a/main.go b/students/Soumil-Bhattacharya/main.go similarity index 100% rename from main.go rename to students/Soumil-Bhattacharya/main.go diff --git a/students/Soumil-Bhattacharya/problems.csv b/students/Soumil-Bhattacharya/problems.csv new file mode 100644 index 0000000..11506cb --- /dev/null +++ b/students/Soumil-Bhattacharya/problems.csv @@ -0,0 +1,12 @@ +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