From c98d3d95134951b1e92aeb34c254bdb6f5c9f5aa Mon Sep 17 00:00:00 2001 From: Aman Tahiliani Date: Sun, 15 Sep 2024 15:09:54 -0400 Subject: [PATCH] Gophercise 1. Quiz --- .gitignore | 2 + 1_Quiz_Game/go.mod | 3 ++ 1_Quiz_Game/quiz.csv | 3 ++ 1_Quiz_Game/quiz.go | 109 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 1_Quiz_Game/go.mod create mode 100644 1_Quiz_Game/quiz.csv create mode 100644 1_Quiz_Game/quiz.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e27209c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +bin/ \ No newline at end of file diff --git a/1_Quiz_Game/go.mod b/1_Quiz_Game/go.mod new file mode 100644 index 0000000..94a0652 --- /dev/null +++ b/1_Quiz_Game/go.mod @@ -0,0 +1,3 @@ +module quiz + +go 1.22.0 diff --git a/1_Quiz_Game/quiz.csv b/1_Quiz_Game/quiz.csv new file mode 100644 index 0000000..a4c0567 --- /dev/null +++ b/1_Quiz_Game/quiz.csv @@ -0,0 +1,3 @@ +Question,Answer +5+15,20 +7+2,9 \ No newline at end of file diff --git a/1_Quiz_Game/quiz.go b/1_Quiz_Game/quiz.go new file mode 100644 index 0000000..3d1a368 --- /dev/null +++ b/1_Quiz_Game/quiz.go @@ -0,0 +1,109 @@ +// Authored by Aman Tahiliani +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "log" + "os" + "strconv" + "time" +) + +// Initial Algorithm design by Aman + +// Read and Parse CSV +// Initialize Score variables +// Initialize Loop +// Print out Queston On to the terminal +// One Channel to Count time and return time over when done. +// One Channel to take in Input and return it. +// We either get the answer back or a timeout. +// If answer, then check for the correctness of the answer and update the score accordingly. +var score int = 0 + +func readCsv(filename string) ([][]string, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + + defer file.Close() + reader := csv.NewReader(file) + records, err := reader.ReadAll() + if err != nil { + return nil, err + } + + fmt.Println(records) + return records, nil +} + +func exit(msg string) { + fmt.Println(msg) + os.Exit(1) +} + +func scanAnswer(scanChan chan int) { + var i int + fmt.Scan(&i) + scanChan <- i + close(scanChan) +} + +func timesUp(n int, timeChan chan string) { + time.Sleep(time.Duration(n) * time.Second) + timeChan <- "Times Up!! Moving on." +} + +func checkAnswer(correctAnswer int, inputAnswer int) { + if inputAnswer == correctAnswer { + score++ + fmt.Println("Correct Answer!") + } else { + fmt.Println("Wrong Answer :(") + } + fmt.Println("") +} + +func main() { + log.Println("Starting the Quiz") + + csvFileName := flag.String("csv", "quiz.csv", "A CSV file in the format of 'question, answer'") + flag.Parse() + + records, err := readCsv(*csvFileName) + + if err != nil { + exit(fmt.Sprintf("Error occurred while reading records: %s", err.Error())) + } + fmt.Println("Number of records: ", len(records)) + score = 0 + + for index, record := range records[1:] { + // startTime := time.Now().Format(time.RFC822) + question := record[0] + answer := record[1] + + fmt.Println("Question Number", index+1) + fmt.Println(question, "?") + + answer_int, _ := strconv.Atoi(answer) + + scanChan := make(chan int) + timeChan := make(chan string) + + go scanAnswer(scanChan) + go timesUp(7, timeChan) + + select { + case msg1 := <-scanChan: + checkAnswer(answer_int, msg1) + case msg2 := <-timeChan: + fmt.Println(msg2) + } + + } + fmt.Println("Your score is :", score) +}