From fc5618f967b755ab1d35c0ebc9e1696de1b8813e Mon Sep 17 00:00:00 2001 From: OM15JQ Date: Wed, 3 Oct 2018 13:28:17 +0200 Subject: [PATCH 1/5] Add another student solution to Part 1. --- students/mirekwalczak/problems.csv | 12 +++++ students/mirekwalczak/quiz.go | 72 ++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 students/mirekwalczak/problems.csv create mode 100644 students/mirekwalczak/quiz.go 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..a09708f --- /dev/null +++ b/students/mirekwalczak/quiz.go @@ -0,0 +1,72 @@ +package main + +import ( + "bufio" + "encoding/csv" + "flag" + "fmt" + "io" + "log" + "os" + "strings" +) + +// 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 + } + 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 main() { + var f string + flag.StringVar(&f, "f", "problems.csv", "input file in csv format") + flag.Parse() + reader := bufio.NewReader(os.Stdin) + recs, err := readCSV(f) + if err != nil { + log.Fatal(err) + } + var stat Stat + for _, elem := range recs { + fmt.Print(elem.question, ":") + ans, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + stat.all++ + if strings.TrimRight(ans, "\r\n") == elem.answer { + stat.correct++ + } else { + stat.incorrect++ + } + } + fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) + +} From 752362199c3813d1dc3b7c4418e448f3595dddc2 Mon Sep 17 00:00:00 2001 From: OM15JQ Date: Wed, 3 Oct 2018 14:52:43 +0200 Subject: [PATCH 2/5] Refactor code. --- students/mirekwalczak/quiz.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/students/mirekwalczak/quiz.go b/students/mirekwalczak/quiz.go index a09708f..d327a78 100644 --- a/students/mirekwalczak/quiz.go +++ b/students/mirekwalczak/quiz.go @@ -26,6 +26,7 @@ func readCSV(file string) ([]Quiz, error) { if err != nil { return nil, err } + defer f.Close() var quizes []Quiz r := csv.NewReader(f) for { @@ -44,21 +45,14 @@ func readCSV(file string) ([]Quiz, error) { return quizes, nil } -func main() { - var f string - flag.StringVar(&f, "f", "problems.csv", "input file in csv format") - flag.Parse() - reader := bufio.NewReader(os.Stdin) - recs, err := readCSV(f) - if err != nil { - log.Fatal(err) - } +func quiz(records []Quiz) (*Stat, error) { var stat Stat - for _, elem := range recs { + reader := bufio.NewReader(os.Stdin) + for _, elem := range records { fmt.Print(elem.question, ":") ans, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) + return nil, err } stat.all++ if strings.TrimRight(ans, "\r\n") == elem.answer { @@ -67,6 +61,20 @@ func main() { stat.incorrect++ } } - fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) + return &stat, nil +} +func main() { + var f string + flag.StringVar(&f, "f", "problems.csv", "input file in csv format") + flag.Parse() + recs, err := readCSV(f) + if err != nil { + log.Fatal(err) + } + stat, err := quiz(recs) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) } From d1bab9ebcae14a3e59d17a27ad6a6453b4a49c19 Mon Sep 17 00:00:00 2001 From: OM15JQ Date: Wed, 3 Oct 2018 14:52:43 +0200 Subject: [PATCH 3/5] Refactor code. --- students/mirekwalczak/quiz.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/students/mirekwalczak/quiz.go b/students/mirekwalczak/quiz.go index a09708f..d327a78 100644 --- a/students/mirekwalczak/quiz.go +++ b/students/mirekwalczak/quiz.go @@ -26,6 +26,7 @@ func readCSV(file string) ([]Quiz, error) { if err != nil { return nil, err } + defer f.Close() var quizes []Quiz r := csv.NewReader(f) for { @@ -44,21 +45,14 @@ func readCSV(file string) ([]Quiz, error) { return quizes, nil } -func main() { - var f string - flag.StringVar(&f, "f", "problems.csv", "input file in csv format") - flag.Parse() - reader := bufio.NewReader(os.Stdin) - recs, err := readCSV(f) - if err != nil { - log.Fatal(err) - } +func quiz(records []Quiz) (*Stat, error) { var stat Stat - for _, elem := range recs { + reader := bufio.NewReader(os.Stdin) + for _, elem := range records { fmt.Print(elem.question, ":") ans, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) + return nil, err } stat.all++ if strings.TrimRight(ans, "\r\n") == elem.answer { @@ -67,6 +61,20 @@ func main() { stat.incorrect++ } } - fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) + return &stat, nil +} +func main() { + var f string + flag.StringVar(&f, "f", "problems.csv", "input file in csv format") + flag.Parse() + recs, err := readCSV(f) + if err != nil { + log.Fatal(err) + } + stat, err := quiz(recs) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) } From 031635ec018265c31cb96bab892722c1485efd97 Mon Sep 17 00:00:00 2001 From: mirekwalczak Date: Thu, 4 Oct 2018 12:18:48 +0200 Subject: [PATCH 4/5] Part2: add timer. --- students/mirekwalczak/quiz.go | 52 +++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/students/mirekwalczak/quiz.go b/students/mirekwalczak/quiz.go index d327a78..83229b4 100644 --- a/students/mirekwalczak/quiz.go +++ b/students/mirekwalczak/quiz.go @@ -9,6 +9,7 @@ import ( "log" "os" "strings" + "time" ) // Quiz is structure for questions and answers @@ -45,36 +46,53 @@ func readCSV(file string) ([]Quiz, error) { return quizes, nil } -func quiz(records []Quiz) (*Stat, error) { +func quiz(records []Quiz, timeout int) (*Stat, error) { var stat Stat reader := bufio.NewReader(os.Stdin) - for _, elem := range records { - fmt.Print(elem.question, ":") - ans, err := reader.ReadString('\n') - if err != nil { - return nil, err - } - stat.all++ - if strings.TrimRight(ans, "\r\n") == elem.answer { - stat.correct++ - } else { - stat.incorrect++ + + timer := time.NewTimer(time.Second * time.Duration(timeout)) + finish := make(chan bool) + 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++ + } } + finish <- true + }() + + select { + case <-errs: + return nil, <-errs + case <-timer.C: + fmt.Println("\ntime's up!") } + return &stat, nil } func main() { - var f string - flag.StringVar(&f, "f", "problems.csv", "input file in csv format") + 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) + recs, err := readCSV(*f) if err != nil { log.Fatal(err) } - stat, err := quiz(recs) + stat, err := quiz(recs, *t) if err != nil { log.Fatal(err) } - fmt.Printf("Question answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) + fmt.Printf("\nQuestion answered: %v, Correct: %v, Incorrect: %v\n", stat.all, stat.correct, stat.incorrect) } From 23681bd46cf2b848d498bfdc619c1749574b059e Mon Sep 17 00:00:00 2001 From: mirekwalczak Date: Thu, 4 Oct 2018 12:45:45 +0200 Subject: [PATCH 5/5] Remove unnecessary code. --- students/mirekwalczak/quiz.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/students/mirekwalczak/quiz.go b/students/mirekwalczak/quiz.go index 83229b4..85a9628 100644 --- a/students/mirekwalczak/quiz.go +++ b/students/mirekwalczak/quiz.go @@ -51,7 +51,6 @@ func quiz(records []Quiz, timeout int) (*Stat, error) { reader := bufio.NewReader(os.Stdin) timer := time.NewTimer(time.Second * time.Duration(timeout)) - finish := make(chan bool) errs := make(chan error) go func() { @@ -69,7 +68,6 @@ func quiz(records []Quiz, timeout int) (*Stat, error) { stat.incorrect++ } } - finish <- true }() select {