Skip to content

Commit

Permalink
add kannanenator solution (gophercises#16)
Browse files Browse the repository at this point in the history
* quiz exercise part 1 and 2

* remove todo

* EOF newline

* actually EOF newline
  • Loading branch information
kannanenator authored and joncalhoun committed Jul 23, 2019
1 parent 6ef43f9 commit 92549a1
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions students/kannanenator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import "os"
import "fmt"
import "log"
import "flag"
import "time"
import "encoding/csv"
import "bufio"
import "strings"

func main() {

filenamePtr := flag.String("filename", "problems.csv", "file containing the set of problems")
limitPtr := flag.Int("limit", 30, "quiz time limit")

flag.Parse()

file, err := os.Open(*filenamePtr)
handleError(err)
defer file.Close()

csvReader := csv.NewReader(file)
rows, err := csvReader.ReadAll()
handleError(err)

numQs := len(rows)
numCorrect := 0

timer := time.NewTimer(time.Second * time.Duration(*limitPtr))
go func() {
<- timer.C
// when the timer ends, we kill the quiz
fmt.Println("\nTime is up")
os.Exit(0)
}()

consoleReader := bufio.NewReader(os.Stdin)
for idx, element := range rows {
q, a := element[0], element[1]
fmt.Print("Problem #", idx+1 ,": ", q, " = ")
input, _ := consoleReader.ReadString('\n')

// compare w/o whitespace
if strings.TrimSpace(input) == strings.TrimSpace(a) {
numCorrect++
}
}

fmt.Println("You got", numCorrect, "out of", numQs, "correct")
}

func handleError(err error){
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 92549a1

Please sign in to comment.