diff --git a/students/mirekwalczak/problems.csv b/students/mirekwalczak/problems.csv new file mode 100644 index 0000000..32ec1e9 --- /dev/null +++ b/students/mirekwalczak/problems.csv @@ -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 diff --git a/students/mirekwalczak/quiz.go b/students/mirekwalczak/quiz.go new file mode 100644 index 0000000..85a9628 --- /dev/null +++ b/students/mirekwalczak/quiz.go @@ -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) +}