forked from gophercises/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add another student solution to Part 1. * Refactor code. * Refactor code. * Part2: add timer. * Remove unnecessary code.
- Loading branch information
1 parent
be5fe8d
commit 821688a
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |