From a0128d8f116e111e7c3e6ae6fb4b65ca62d57a03 Mon Sep 17 00:00:00 2001 From: Dmitry Inyutin Date: Wed, 12 Sep 2018 01:09:15 +0300 Subject: [PATCH 1/2] done --- students/inyutin/.idea/vcs.xml | 6 +++ students/inyutin/problems.csv | 12 ++++++ students/inyutin/quiz.go | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 students/inyutin/.idea/vcs.xml create mode 100644 students/inyutin/problems.csv create mode 100644 students/inyutin/quiz.go diff --git a/students/inyutin/.idea/vcs.xml b/students/inyutin/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/students/inyutin/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/students/inyutin/problems.csv b/students/inyutin/problems.csv new file mode 100644 index 0000000..11506cb --- /dev/null +++ b/students/inyutin/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/inyutin/quiz.go b/students/inyutin/quiz.go new file mode 100644 index 0000000..858039c --- /dev/null +++ b/students/inyutin/quiz.go @@ -0,0 +1,73 @@ +package main + +import ( + "bufio" + "encoding/csv" + "flag" + "fmt" + "io" + "log" + "os" + "strconv" + "time" +) + +type Line struct { + Question string + Answer string +} + +var ( + csvName = flag.String("csv", "problems.csv", "path to csv file with quiz(question,answer)") + limit = flag.Int("limit", 30, "the time limit for the quiz in seconds") +) + +func main() { + flag.Parse() + + csvFile, err := os.Open(*csvName) + if err != nil { + fmt.Println(err) + return + } + defer csvFile.Close() + + csvReader := csv.NewReader(bufio.NewReader(csvFile)) + var lines []Line + for { + line, error := csvReader.Read() + if error == io.EOF { + break + } else if error != nil { + log.Fatal(error) + } + lines = append(lines, Line{ + Question: line[0], + Answer: line[1], + }) + } + + reader := bufio.NewReader(os.Stdin) + count := 0 + + T := time.Duration(*limit) + timer := time.NewTimer(T * time.Second) + go func() { + <-timer.C + fmt.Println() + fmt.Println("You scored " + strconv.Itoa(count) + " out of " + strconv.Itoa(len(lines))) + os.Exit(0) + }() + + for idx, line := range lines { + fmt.Print("Question №" + strconv.Itoa(idx+1) + ": " + line.Question + " = ") + ans, _ := reader.ReadString('\n') + if ans == line.Answer+"\n" { + count++ + } + } + stop := timer.Stop() + if stop { + fmt.Println("You scored " + strconv.Itoa(count) + " out of " + strconv.Itoa(len(lines))) + } +} From 5da0700f97ec6af09fa44c5a0bb1d4c46f4b5792 Mon Sep 17 00:00:00 2001 From: Dmitry Inuytin Date: Wed, 12 Sep 2018 01:09:54 +0300 Subject: [PATCH 2/2] Delete vcs.xml --- students/inyutin/.idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 students/inyutin/.idea/vcs.xml diff --git a/students/inyutin/.idea/vcs.xml b/students/inyutin/.idea/vcs.xml deleted file mode 100644 index b2bdec2..0000000 --- a/students/inyutin/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file