-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (69 loc) · 1.56 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
package main
import (
"encoding/csv"
"flag"
"fmt"
"os"
"strings"
"time"
)
type Problem struct {
question string
answer string
}
func main() {
// Generate flag
filename := flag.String("csv", "file.csv", "CSV file in formation 'question,answer'")
timeLimit := flag.Int("limit", 3, "The current time limit in seconds")
flag.Parse()
// Read the file
file, err := os.Open(*filename)
if err != nil {
exit(fmt.Sprintf("Could not open the file : %s\n", *filename))
}
// Create the csv reader
reader := csv.NewReader(file)
// Read all lines
lines, err := reader.ReadAll()
if err != nil {
exit(fmt.Sprintf("Failed to parse the file"))
}
problems := parseLines(lines)
correct := 0
// set timer
timer := time.NewTimer(time.Duration(*timeLimit) * time.Second)
for i, problem := range problems {
fmt.Printf("Problem #%d: %s = \n", i+1, problem.question)
answerChannel := make(chan string)
go func() {
var answer string
// make sur it has a pointer value to it
_, _ = fmt.Scanf("%s\n", &answer)
answerChannel <- answer
}()
select {
case <-timer.C:
fmt.Printf("\nScored %d out of %d.\n", correct, len(problems))
return
case answer := <-answerChannel:
if answer == problem.answer {
correct++
}
}
}
fmt.Printf("Scored %d out of %d.\n", correct, len(problems))
}
func parseLines(lines [][]string) []Problem {
r := make([]Problem, len(lines))
for i, line := range lines {
r[i] = Problem{
question: line[0],
answer: strings.TrimSpace(line[1]),
}
}
return r
}
func exit(msg string) {
fmt.Println(msg)
os.Exit(1)
}