-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.go
139 lines (114 loc) · 3.06 KB
/
quiz.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"math/rand"
"os"
"strings"
"time"
)
type Quiz struct {
Problems []Problem
}
type Problem struct {
Question string // a question
Answer string // the correct answer
UserAnswer string // answer provided by user
}
type Tally struct {
Correct int
Incorrect int
}
type Sleeper interface {
Sleep()
}
type ConfigurableSleeper struct {
duration time.Duration
sleep func(time.Duration)
}
func (c ConfigurableSleeper) Sleep() {
c.sleep(c.duration)
}
func main() {
timeout := flag.Duration("timeout", 30*time.Second, "timeout in time.Duration format")
shuffle := flag.Bool("shuffle", false, "shuffles order of the quetions")
flag.Parse()
sleeper := &ConfigurableSleeper{*timeout, time.Sleep}
file, _ := os.Open("problems.csv")
problems := LoadProblems(file)
quiz := Quiz{problems}
fmt.Println("Press Enter to start the quiz")
fmt.Scanln()
fmt.Printf("Good luck! You've got %v\n", timeout)
go QuizTimeLimit(os.Stdout, sleeper, quiz)
quiz.LoopProblems(*shuffle)
quiz.PrintResults(os.Stdout)
}
// QuizTimeLimit exits the program after specified amount of time
func QuizTimeLimit(out io.Writer, sleeper Sleeper, quiz Quiz) {
sleeper.Sleep()
quiz.PrintResults(out)
fmt.Fprintln(out, "Oops! Time's up.")
os.Exit(0)
}
// LoopProblems prints all quiz questions and collects UserAnswers
func (quiz Quiz) LoopProblems(shuffle bool) {
if shuffle {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(quiz.Problems), func(i, j int) { quiz.Problems[i], quiz.Problems[j] = quiz.Problems[j], quiz.Problems[i] })
}
for i, problem := range quiz.Problems {
problem.PrintQuestion(os.Stdout)
problem := problem.GetUserAnswer()
quiz.Problems[i] = problem
}
}
// LoadProblems converts a CSV into a slice of Problem structs
func LoadProblems(problemsCsv io.Reader) []Problem {
reader := csv.NewReader(problemsCsv)
lines, _ := reader.ReadAll()
problems := make([]Problem, len(lines))
for i, line := range lines {
problems[i] = Problem{line[0], line[1], ""}
}
return problems
}
// PrintResults outputs the tally in readable format
func (q *Quiz) PrintResults(out io.Writer) {
tally := q.Tally()
fmt.Fprintf(out, "correct: %d\n", tally.Correct)
fmt.Fprintf(out, "incorrect: %d\n", tally.Incorrect)
}
// PrintQuestion outputs the question
func (p Problem) PrintQuestion(out io.Writer) {
fmt.Fprint(out, p.Question+"=\n")
}
// GetUserAnswer collects the answer from Stdin
func (p Problem) GetUserAnswer() Problem {
var answer string
fmt.Scanf("%s\n", &answer)
p.RecordAnswer(answer)
return p
}
// RecordAnswer stores a string in Problem.UserAnswer
func (p *Problem) RecordAnswer(answer string) {
p.UserAnswer = strings.TrimSpace(answer)
}
// Tally calculates the number of correct/incorrect answers
func (q Quiz) Tally() Tally {
tally := Tally{Correct: 0, Incorrect: 0}
for _, problem := range q.Problems {
if problem.Correct() {
tally.Correct++
} else {
tally.Incorrect++
}
}
return tally
}
// Correct checks if an answer is correct
func (p Problem) Correct() bool {
return (p.Answer == p.UserAnswer)
}