-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (134 loc) · 3.1 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"encoding/csv"
"errors"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
type Score struct {
question int
answer int
}
func (s Score) printScore() {
fmt.Printf("Total correct answers: %d/%d\n", s.answer, s.question)
}
var (
fileFlag string
timeoutFlag int
shuffle bool
)
func init() {
flag.StringVar(&fileFlag, "f", "test/fixtures/problems.csv", "File containing the quiz")
flag.IntVar(&timeoutFlag, "t", 0, "Set time limit to the quiz")
flag.BoolVar(&shuffle, "s", false, "Shuffle the questions")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
fmt.Println("Options:")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
path := *&fileFlag
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
fmt.Println("Error reading csv:", err)
return
}
records = validateQuestions(records)
if shuffle {
randomizeSlice(records)
}
scoreChan := make(chan Score)
go RunQuiz(records, scoreChan, timeoutFlag)
score := <-scoreChan
score.printScore()
}
func RunQuiz(records [][]string, scoreChan chan<- Score, duration int) {
totalQuestion := len(records)
score := Score{
question: totalQuestion,
}
if duration != 0 {
timeout := time.Duration(duration) * time.Second
timer := time.NewTimer(timeout)
defer timer.Stop()
go func() {
<-timer.C
fmt.Println("\nTime's up! Quiz is over.")
scoreChan <- score
}()
}
stdinReader := bufio.NewReader(os.Stdin)
fmt.Printf("This quiz contains %d questions. Please answer it and marks will be given by the end of the quiz\n", totalQuestion)
if duration != 0 {
fmt.Printf("You have %d seconds to answer all questions\n", totalQuestion)
}
for i, record := range records {
correctAnswer, _ := strconv.Atoi(record[1])
var (
line string
providedAnswer int
err error
)
for {
fmt.Printf("Question %d: %s = ", i+1, record[0])
if line, err = stdinReader.ReadString('\n'); err != nil {
fmt.Fprintln(os.Stderr, "Error reading solution:", err)
return
}
if providedAnswer, err = strconv.Atoi(strings.TrimRight(line, "\n")); err != nil {
fmt.Fprintln(os.Stderr, "Invalid number:", err)
continue
}
break
}
if providedAnswer == correctAnswer {
score.answer++
}
}
scoreChan <- score
}
func validateQuestions(records [][]string) [][]string {
var result [][]string
var err error
for _, record := range records {
if err = validQuestion(record[0]); err != nil {
break
}
if err = validAnswer(record[1]); err != nil {
break
}
result = append(result, []string{record[0], record[1]})
}
return result
}
func validQuestion(q string) error {
return nil
}
func validAnswer(a string) error {
_, err := strconv.Atoi(a)
if err != nil {
return errors.New("Invalid number")
}
return nil
}
func randomizeSlice(slice [][]string) {
for i := len(slice) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}