mirror of
https://github.com/AmanTahiliani/gophercises.git
synced 2026-08-08 04:06:16 -04:00
Different solution for Gophercise 1
This commit is contained in:
12
1. Quiz Game Sol2/students/inyutin/problems.csv
Normal file
12
1. Quiz Game Sol2/students/inyutin/problems.csv
Normal file
@@ -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
|
||||
|
73
1. Quiz Game Sol2/students/inyutin/quiz.go
Normal file
73
1. Quiz Game Sol2/students/inyutin/quiz.go
Normal file
@@ -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)))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user