Skip to content

Commit 14e3609

Browse files
Dmitry Inuytinjoncalhoun
authored andcommitted
Done (#40)
* done * Delete vcs.xml
1 parent 821688a commit 14e3609

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

students/inyutin/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

students/inyutin/quiz.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/csv"
6+
"flag"
7+
"fmt"
8+
"io"
9+
"log"
10+
"os"
11+
"strconv"
12+
"time"
13+
)
14+
15+
type Line struct {
16+
Question string
17+
Answer string
18+
}
19+
20+
var (
21+
csvName = flag.String("csv", "problems.csv", "path to csv file with quiz(question,answer)")
22+
limit = flag.Int("limit", 30, "the time limit for the quiz in seconds")
23+
)
24+
25+
func main() {
26+
flag.Parse()
27+
28+
csvFile, err := os.Open(*csvName)
29+
if err != nil {
30+
fmt.Println(err)
31+
return
32+
}
33+
defer csvFile.Close()
34+
35+
csvReader := csv.NewReader(bufio.NewReader(csvFile))
36+
var lines []Line
37+
for {
38+
line, error := csvReader.Read()
39+
if error == io.EOF {
40+
break
41+
} else if error != nil {
42+
log.Fatal(error)
43+
}
44+
lines = append(lines, Line{
45+
Question: line[0],
46+
Answer: line[1],
47+
})
48+
}
49+
50+
reader := bufio.NewReader(os.Stdin)
51+
count := 0
52+
53+
T := time.Duration(*limit)
54+
timer := time.NewTimer(T * time.Second)
55+
go func() {
56+
<-timer.C
57+
fmt.Println()
58+
fmt.Println("You scored " + strconv.Itoa(count) + " out of " + strconv.Itoa(len(lines)))
59+
os.Exit(0)
60+
}()
61+
62+
for idx, line := range lines {
63+
fmt.Print("Question №" + strconv.Itoa(idx+1) + ": " + line.Question + " = ")
64+
ans, _ := reader.ReadString('\n')
65+
if ans == line.Answer+"\n" {
66+
count++
67+
}
68+
}
69+
stop := timer.Stop()
70+
if stop {
71+
fmt.Println("You scored " + strconv.Itoa(count) + " out of " + strconv.Itoa(len(lines)))
72+
}
73+
}

0 commit comments

Comments
 (0)