-
Notifications
You must be signed in to change notification settings - Fork 728
added my solution #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in> added my solution fixed issue with blocking input added my solution Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in> added my solution
8ebb5f1 to
764f0c2
Compare
arsham
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome coding skills.
| "math/rand" | ||
| ) | ||
|
|
||
| type Question struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea to document an exported struct. You can use golint tool to tell you about these actions:
golint students/viveksyngh| if(err != nil) { | ||
| log.Fatal("Failed to parse CSV file.") | ||
| } | ||
| questions := make([]Question, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now you have successfully loaded your questionList, you have the required length. You can do this instead:
questions := make([]Question, len(questionList))
for i, question := range questionList {
if len(question) != 2 {
continue
}
questions[i] = Question{
strings.TrimSpace(question[0]),
strings.TrimSpace(question[1]),
}
}This way you don't allocate new array every time append is called. Also always check the input coming from outside world.
| return questions | ||
| } | ||
|
|
||
| func Quiz(questions []Question, timer *time.Timer) (score int){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for named return, it's another allocation. If you simply name your function score it informs the reader what they expect.
| }() | ||
|
|
||
| select { | ||
| case <-timer.C: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clever use of timers.
| return score | ||
| case userAnswer := <- answerChannel: | ||
| if(strings.TrimSpace(userAnswer) == question.answer) { | ||
| score += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simply do: score++
| } | ||
|
|
||
| func randomize(questions []Question) []Question{ | ||
| n := len(questions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this too:
rand.Seed(time.Now().UnixNano())
for i := range questions {
j := rand.Intn(i + 1)
questions[i], questions[j] = questions[j], questions[i]
}or
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(questions), func(i, j int) {
questions[i], questions[j] = questions[j], questions[i]
})It's a good idea to seed your random generator otherwise the shuffle returns the same results every time you run the program, which defeats its purpose.
Signed-off-by: Vivek Singh vivekkmr45@yahoo.in
added my solution