Skip to content

Commit

Permalink
Added siredmar's solution (gophercises#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
siredmar authored and joncalhoun committed Jul 22, 2019
1 parent 2a121d4 commit 314ad91
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
108 changes: 108 additions & 0 deletions students/siredmar/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
)

var (
file = flag.String("csv", "problems.csv", "a csv file in the format 'question,answer'")
limit = flag.Int("limit", 30, "the time limit for the quiz in seconds")
)

func main() {
flag.Parse()

if *limit < 0 {
fmt.Println("Error: enter positive time limit")
os.Exit(1)
}

records, err := read(*file)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}

fmt.Println("Press enter to start. Time limit is", *limit, "seconds")
in := bufio.NewReader(os.Stdin)
in.ReadString('\n')

input := make(chan string)

quizdone := make(chan bool)
timeout := time.NewTicker(time.Duration(*limit) * time.Second)

go getInput(input)

var score int
var maxscore int

go func() {
maxscore = len(records)
for i, v := range records {
fmt.Print("Problem #", i+1, ": ", v[0], " = ")
text := <-input
if text == v[1] {
score++
}
}
quizdone <- true
}()

select {
case <-quizdone:
case <-timeout.C:
fmt.Println("\nThe time is up! Game over!")

}

fmt.Println("You scored", score, "out of", maxscore)
}

func read(filename string) ([][]string, error) {
dat, err := ioutil.ReadFile(filename)
r := csv.NewReader(strings.NewReader(string(dat)))
if err == nil {
var records [][]string
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
records = append(records, record)
}
return records, nil
}
return nil, err
}

func trim(s string) string {
t := strings.Trim(s, "\n")
t = strings.Trim(t, " ")
return t
}

func getInput(input chan<- string) {
for {
in := bufio.NewReader(os.Stdin)
result, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}

result = trim(result)
input <- result
}
}
14 changes: 14 additions & 0 deletions students/siredmar/problems.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"What is the favourite food?",pizza
5+5,10
7+3,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

0 comments on commit 314ad91

Please sign in to comment.