|
| 1 | +package quiz |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/csv" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +//Number of Questions to ask |
| 16 | +const totalQuestions = 5 |
| 17 | + |
| 18 | +//Question struct that stores question with answer |
| 19 | +type Question struct { |
| 20 | + question string |
| 21 | + answer string |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + filename, timeLimit := readArguments() |
| 26 | + f, err := openFile(filename) |
| 27 | + if err != nil { |
| 28 | + return |
| 29 | + } |
| 30 | + questions, err := readCSV(f) |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + // err := fmt.Errorf("Error in Reading Questions") |
| 34 | + fmt.Println(err.Error()) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if questions == nil { |
| 39 | + return |
| 40 | + } |
| 41 | + score, err := askQuestion(questions, timeLimit) |
| 42 | + if err != nil { |
| 43 | + fmt.Println(err) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + fmt.Printf("Your Score %d/%d\n", score, totalQuestions) |
| 48 | +} |
| 49 | + |
| 50 | +func readArguments() (string, int) { |
| 51 | + filename := flag.String("filename", "problem.csv", "CSV File that conatins quiz questions") |
| 52 | + timeLimit := flag.Int("limit", 30, "Time Limit for each question") |
| 53 | + flag.Parse() |
| 54 | + return *filename, *timeLimit |
| 55 | +} |
| 56 | + |
| 57 | +func readCSV(f io.Reader) ([]Question, error) { |
| 58 | + // defer f.Close() // this needs to be after the err check |
| 59 | + allQuestions, err := csv.NewReader(f).ReadAll() |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + numOfQues := len(allQuestions) |
| 65 | + if numOfQues == 0 { |
| 66 | + return nil, fmt.Errorf("No Question in file") |
| 67 | + } |
| 68 | + |
| 69 | + var data []Question |
| 70 | + for _, line := range allQuestions { |
| 71 | + ques := Question{} |
| 72 | + ques.question = line[0] |
| 73 | + ques.answer = line[1] |
| 74 | + data = append(data, ques) |
| 75 | + } |
| 76 | + |
| 77 | + return data, nil |
| 78 | +} |
| 79 | + |
| 80 | +func openFile(filename string) (io.Reader, error) { |
| 81 | + return os.Open(filename) |
| 82 | +} |
| 83 | +func getInput(input chan string) { |
| 84 | + for { |
| 85 | + in := bufio.NewReader(os.Stdin) |
| 86 | + result, err := in.ReadString('\n') |
| 87 | + if err != nil { |
| 88 | + log.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + input <- result |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func askQuestion(questions []Question, timeLimit int) (int, error) { |
| 96 | + totalScore := 0 |
| 97 | + timer := time.NewTimer(time.Duration(timeLimit) * time.Second) |
| 98 | + done := make(chan string) |
| 99 | + |
| 100 | + go getInput(done) |
| 101 | + |
| 102 | + for i := range [totalQuestions]int{} { |
| 103 | + ans, err := eachQuestion(questions[i].question, questions[i].answer, timer.C, done) |
| 104 | + if err != nil && ans == -1 { |
| 105 | + return totalScore, nil |
| 106 | + } |
| 107 | + totalScore += ans |
| 108 | + |
| 109 | + } |
| 110 | + return totalScore, nil |
| 111 | +} |
| 112 | + |
| 113 | +func eachQuestion(Quest string, answer string, timer <-chan time.Time, done <-chan string) (int, error) { |
| 114 | + fmt.Printf("%s: ", Quest) |
| 115 | + |
| 116 | + for { |
| 117 | + select { |
| 118 | + case <-timer: |
| 119 | + return -1, fmt.Errorf("Time out") |
| 120 | + case ans := <-done: |
| 121 | + score := 0 |
| 122 | + if strings.Compare(strings.Trim(strings.ToLower(ans), "\n"), answer) == 0 { |
| 123 | + score = 1 |
| 124 | + } else { |
| 125 | + return 0, fmt.Errorf("Wrong Answer") |
| 126 | + } |
| 127 | + |
| 128 | + return score, nil |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments