-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (63 loc) · 1.33 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
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strings"
)
type QuestionAnswer struct {
Question string
Answer string
}
func main() {
strPtr := flag.String("filename", "problems.csv", "CSV File Name")
flag.Parse()
var commandIs string = *strPtr
if *strPtr != "problems.csv" {
commandIs = *strPtr + ".csv"
}
f, err := os.Open(commandIs)
checkNilErr(err)
defer f.Close()
stat, err := f.Stat()
checkNilErr(err)
if stat.Size() == 0 {
log.Fatal("empty CSV file given")
}
csvReader := csv.NewReader(f)
data, err := csvReader.ReadAll()
checkNilErr(err)
if len(data) == 0 {
log.Fatal("no questions found in the CSV file")
}
var questionAnswers []QuestionAnswer
for _, row := range data {
if len(row) != 2 {
log.Fatal("invalid CSV format: each row must contain exactly two columns")
}
questionAnswers = append(questionAnswers, QuestionAnswer{
Question: row[0],
Answer: row[1],
})
}
var score int
reader := bufio.NewReader(os.Stdin)
for i, q := range questionAnswers {
fmt.Printf("%d. %s ", i+1, q.Question)
val, err := reader.ReadString('\n')
checkNilErr(err)
val = strings.TrimSpace(val)
if val == q.Answer {
score++
}
}
fmt.Printf("You have scored %d out of %d\n", score, len(questionAnswers))
}
func checkNilErr(err error) {
if err != nil {
log.Fatal(err)
}
}