Skip to content

Commit 82fe91a

Browse files
Julianjoncalhoun
authored andcommitted
Liikt's solution for the quiz (#21)
* my solution for the quiz * implemented the suggestions from @arsham
1 parent 8b6e57d commit 82fe91a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

students/liikt/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"encoding/csv"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"os"
9+
"strings"
10+
"time"
11+
)
12+
13+
var (
14+
problemsFile string
15+
timeout int
16+
correct int
17+
b bool
18+
inChan chan int
19+
outChan chan int
20+
)
21+
22+
func getAnswer(solution string) {
23+
var inp string
24+
fmt.Scanln(&inp)
25+
if sanitize(inp) == sanitize(solution) {
26+
outChan <- <-inChan + 1
27+
} else {
28+
outChan <- <-inChan
29+
}
30+
}
31+
32+
func updateCorrect() bool {
33+
select {
34+
case res := <-outChan:
35+
correct = res
36+
return true
37+
case <-time.After(time.Duration(timeout) * time.Second):
38+
close(outChan)
39+
fmt.Println()
40+
return false
41+
}
42+
}
43+
44+
func sanitize(s string) string {
45+
return strings.ToLower(strings.Trim(s, "\n\r\t "))
46+
}
47+
48+
func main() {
49+
flag.StringVar(&problemsFile, "path", "problems.csv", "This is the flag to the CSV containing the problems for the quiz")
50+
flag.IntVar(&timeout, "timeout", 30, "The amount of time you have for a single question in seconds")
51+
flag.Parse()
52+
53+
file, err := os.Open(problemsFile)
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
defer file.Close()
58+
59+
problems, err := csv.NewReader(file).ReadAll()
60+
if err != nil {
61+
log.Fatal(err)
62+
}
63+
64+
inChan, outChan = make(chan int, 1), make(chan int, 1)
65+
for c, q := range problems {
66+
fmt.Printf("Question %v: %v -> ", c, q[0])
67+
68+
go getAnswer(q[1])
69+
inChan <- correct
70+
71+
if !updateCorrect() {
72+
break
73+
}
74+
75+
}
76+
fmt.Println("You got", correct, "out of", len(problems), "correct.")
77+
}

students/liikt/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)