-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.go
59 lines (52 loc) · 1.27 KB
/
question.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
package main
import (
"gopkg.in/yaml.v3"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
type Question struct {
Topic string `yaml:"topic"`
Text string `yaml:"text"`
Answer string `yaml:"answer"`
Difficulty int `yaml:"difficulty"`
}
type Questions []Question
func (q *Questions) load() *Questions {
file, err := os.ReadFile("questions.yaml")
handle(err, "os.ReadFile")
err = yaml.Unmarshal(file, q)
handle(err, "yaml.Unmarshal")
return q
}
func (q *Questions) restrain(topics []string, difficulty string) *Questions {
minDif, err := strconv.Atoi(difficulty[:1])
handle(err, "strconv.Atoi")
maxDif, err := strconv.Atoi(difficulty[len(difficulty)-1:])
handle(err, "strconv.Atoi")
newQuestions := new(Questions)
for _, question := range *q {
for _, topic := range topics {
if question.Topic == topic {
dif := question.Difficulty
if dif >= minDif && dif <= maxDif {
*newQuestions = append(*newQuestions, question)
}
}
}
}
return newQuestions
}
func (q *Questions) shuffle() *Questions {
qD := *q
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(qD), func(i, j int) {
qD[i], qD[j] = qD[j], qD[i]
})
return &qD
}
func (q *Question) checkAnswer(input string) bool {
return strings.ToLower(q.Answer) == strings.ToLower(input)
}