Skip to content

Commit be5fe8d

Browse files
teimurjanjoncalhoun
authored andcommitted
Add teimurjan quiz implementation (#42)
1 parent 5223681 commit be5fe8d

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

students/teimurjan/main.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/csv"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"log"
10+
"os"
11+
"strings"
12+
"time"
13+
)
14+
15+
// Problem object
16+
type Problem struct {
17+
Question string
18+
Answer string
19+
}
20+
21+
// ValidateAnswer validates answer for the problem
22+
func (p *Problem) ValidateAnswer(answer string) bool {
23+
return p.Answer == answer
24+
}
25+
26+
// Quiz object
27+
type Quiz struct {
28+
Problems []Problem
29+
Score int
30+
}
31+
32+
func main() {
33+
var file string
34+
flag.StringVar(&file, "file", "problems.csv", "--file=path/to/problems/file")
35+
var quizTime int
36+
flag.IntVar(&quizTime, "time", 30, "--time=15")
37+
flag.Parse()
38+
39+
WaitForStart()
40+
quiz := Quiz{
41+
Problems: ParseProblemsFrom(file),
42+
Score: 0,
43+
}
44+
go func() {
45+
<-time.After(time.Duration(quizTime) * time.Second)
46+
ShowTimeIsUpMessage()
47+
ShowFinalMessage(quiz.Score, len(quiz.Problems))
48+
os.Exit(0)
49+
}()
50+
RunQuiz(&quiz)
51+
ShowFinalMessage(quiz.Score, len(quiz.Problems))
52+
}
53+
54+
// WaitForStart makes the program wait for a user to press Enter button
55+
func WaitForStart() {
56+
fmt.Print("Press 'Enter' to start the quiz.")
57+
bufio.NewReader(os.Stdin).ReadBytes('\n')
58+
}
59+
60+
// ParseProblemsFrom parses problems from file by the provided path
61+
func ParseProblemsFrom(pathToFile string) []Problem {
62+
file, err := os.Open(pathToFile)
63+
if err != nil {
64+
log.Fatal("File does not exists")
65+
}
66+
reader := csv.NewReader(bufio.NewReader(file))
67+
var problems []Problem
68+
for {
69+
line, error := reader.Read()
70+
if error == io.EOF {
71+
break
72+
} else if error != nil {
73+
log.Fatal(error)
74+
}
75+
problems = append(problems, Problem{
76+
Question: line[0],
77+
Answer: line[1],
78+
})
79+
}
80+
return problems
81+
}
82+
83+
// RunQuiz starts the quiz
84+
func RunQuiz(q *Quiz) {
85+
reader := bufio.NewReader(os.Stdin)
86+
for _, problem := range q.Problems {
87+
AskQuestion(&problem)
88+
answer := ReadLine(reader)
89+
if problem.ValidateAnswer(answer) {
90+
q.Score++
91+
}
92+
}
93+
}
94+
95+
// AskQuestion asks a problem's question
96+
func AskQuestion(p *Problem) {
97+
fmt.Print(p.Question + " ")
98+
}
99+
100+
// ReadLine read a line using buifo.Reader
101+
func ReadLine(reader *bufio.Reader) string {
102+
str, _, err := reader.ReadLine()
103+
if err == io.EOF {
104+
return ""
105+
}
106+
107+
return strings.TrimRight(string(str), "\r\n")
108+
}
109+
110+
// ShowTimeIsUpMessage shows time is up message
111+
func ShowTimeIsUpMessage() {
112+
fmt.Println("\rTime is up!")
113+
}
114+
115+
// ShowFinalMessage shows final message with the correctness statistics
116+
func ShowFinalMessage(correctAnswersCount int, problemsCount int) {
117+
fmt.Printf("\rThere is/are %d correct answers given for %d problems.\n", correctAnswersCount, problemsCount)
118+
}

students/teimurjan/problems.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
5+5,10
2+
1+1,2
3+
8+3,11
4+
1+2,3
5+
8+6,14
6+
3+1,4
7+
1+4,5
8+
5+1,6
9+
2+3,5
10+
3+3,6
11+
2+4,6
12+
5+2,7

0 commit comments

Comments
 (0)