Skip to content

Commit

Permalink
my solution (gophercises#41)
Browse files Browse the repository at this point in the history
* Add another student solution to Part 1.

* Refactor code.

* Refactor code.

* Part2: add timer.

* Remove unnecessary code.
  • Loading branch information
mirekwalczak authored and joncalhoun committed Jul 22, 2019
1 parent be5fe8d commit 821688a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions students/mirekwalczak/problems.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
5+5, 10
"1+1",2
8+3, 11
1+2,3
8+6,14
3+1,4
1+4,5
5+1,6
2+3,5
3+3,6
2+4,6
5+2,7
96 changes: 96 additions & 0 deletions students/mirekwalczak/quiz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)

// Quiz is structure for questions and answers
type Quiz struct {
question, answer string
}

// Stat is struct for quiz statistics
type Stat struct {
all, correct, incorrect int
}

func readCSV(file string) ([]Quiz, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
var quizes []Quiz
r := csv.NewReader(f)
for {
line, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
quizes = append(quizes, Quiz{
strings.TrimSpace(line[0]),
strings.TrimSpace(line[1]),
})
}
return quizes, nil
}

func quiz(records []Quiz, timeout int) (*Stat, error) {
var stat Stat
reader := bufio.NewReader(os.Stdin)

timer := time.NewTimer(time.Second * time.Duration(timeout))
errs := make(chan error)

go func() {
for _, quiz := range records {
fmt.Print(quiz.question, ":")

ans, err := reader.ReadString('\n')
if err != nil {
errs <- err
}
stat.all++
if strings.TrimRight(ans, "\r\n") == quiz.answer {
stat.correct++
} else {
stat.incorrect++
}
}
}()

select {
case <-errs:
return nil, <-errs
case <-timer.C:
fmt.Println("\ntime's up!")
}

return &stat, nil
}

func main() {
f := flag.String("f", "problems.csv", "input file in csv format")
t := flag.Int("t", 30, "timeout for the quiz, in seconds")
flag.Parse()
recs, err := readCSV(*f)
if err != nil {
log.Fatal(err)
}
stat, err := quiz(recs, *t)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nQuestion answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect)
}

0 comments on commit 821688a

Please sign in to comment.