/
main.go
58 lines (51 loc) 路 1.04 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
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)
fmt.Println(problems)
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)
}