Skip to content

Commit 92549a1

Browse files
kannanenatorjoncalhoun
authored andcommitted
add kannanenator solution (#16)
* quiz exercise part 1 and 2 * remove todo * EOF newline * actually EOF newline
1 parent 6ef43f9 commit 92549a1

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

students/kannanenator/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import "os"
4+
import "fmt"
5+
import "log"
6+
import "flag"
7+
import "time"
8+
import "encoding/csv"
9+
import "bufio"
10+
import "strings"
11+
12+
func main() {
13+
14+
filenamePtr := flag.String("filename", "problems.csv", "file containing the set of problems")
15+
limitPtr := flag.Int("limit", 30, "quiz time limit")
16+
17+
flag.Parse()
18+
19+
file, err := os.Open(*filenamePtr)
20+
handleError(err)
21+
defer file.Close()
22+
23+
csvReader := csv.NewReader(file)
24+
rows, err := csvReader.ReadAll()
25+
handleError(err)
26+
27+
numQs := len(rows)
28+
numCorrect := 0
29+
30+
timer := time.NewTimer(time.Second * time.Duration(*limitPtr))
31+
go func() {
32+
<- timer.C
33+
// when the timer ends, we kill the quiz
34+
fmt.Println("\nTime is up")
35+
os.Exit(0)
36+
}()
37+
38+
consoleReader := bufio.NewReader(os.Stdin)
39+
for idx, element := range rows {
40+
q, a := element[0], element[1]
41+
fmt.Print("Problem #", idx+1 ,": ", q, " = ")
42+
input, _ := consoleReader.ReadString('\n')
43+
44+
// compare w/o whitespace
45+
if strings.TrimSpace(input) == strings.TrimSpace(a) {
46+
numCorrect++
47+
}
48+
}
49+
50+
fmt.Println("You got", numCorrect, "out of", numQs, "correct")
51+
}
52+
53+
func handleError(err error){
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
}

0 commit comments

Comments
 (0)