-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (83 loc) · 1.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/csv"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
type problem struct {
q string
a string
}
func main() {
// Get the csv file name from command line
csvFilename := flag.String("csv", "problems.csv", "a csv file in a format of 'question,answer'")
timeLimit := flag.Int("limit", 30, "the time limit for the quiz in seconds")
shuffle := flag.Bool("shuffle", true, "shuffle the order of the questions")
flag.Parse()
// Open the csv file in read mode
file, err := os.Open(*csvFilename)
if err != nil {
exit(fmt.Sprintf("Failed to open the CSV file: %s\n", *csvFilename))
}
r := csv.NewReader(file)
// Read from the csv file using Reader created in line above
lines, err := r.ReadAll()
if err != nil {
exit("Failed to parse the lines from csv file")
}
// Parse the lines as slice of struct
problems := parseLines(lines)
// Shuffle the quiz questions
if *shuffle {
problems = shuffleQuiz(problems)
}
// Start the quiz
startQuiz(problems, timeLimit)
}
func startQuiz(problems []problem, timeLimit *int) {
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)
answerCh := make(chan string)
go func() {
var answer string
fmt.Scanf("%s\n", &answer)
answerCh <- answer
}()
select {
case <-timer.C:
fmt.Printf("You scored %d out of %d.\n", correct, len(problems))
return
case answer := <-answerCh:
if answer == p.a {
correct++
}
}
}
fmt.Printf("You scored %d out of %d.\n", correct, len(problems))
}
func shuffleQuiz(problems []problem) []problem {
rand.Shuffle(len(problems), func(i, j int) {
problems[i], problems[j] = problems[j], problems[i]
})
return 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
}
func exit(msg string) {
fmt.Println(msg)
os.Exit(1)
}