Different solution for Gophercise 1

This commit is contained in:
2025-05-31 13:14:20 -04:00
parent b22ab92d79
commit 1da6e395bf
56 changed files with 3374 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
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
1 5+5 10
2 7+3 10
3 1+1 2
4 8+3 11
5 1+2 3
6 8+6 14
7 3+1 4
8 1+4 5
9 5+1 6
10 2+3 5
11 3+3 6
12 2+4 6
13 5+2 7

View File

@@ -0,0 +1,87 @@
package main
import (
"bufio"
"encoding/csv"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
// DEFAULTFILE is the quiz file expected to be load by default
const DEFAULTFILE = "problems.csv"
func main() {
// Flag block
file := flag.String("f", DEFAULTFILE, "specify the path to file")
timeout := flag.Int("t", 0, "specify the number of seconds for timeout")
flag.Parse()
// Failed if the number of seconds is negative
if *timeout < 0 {
flag.PrintDefaults()
os.Exit(1)
}
// Open file
f, err := os.Open(*file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Run the main logic
total, correct, err := run(f, *timeout)
if err != nil {
log.Println(err)
}
fmt.Printf("Number of questions: %v\nNumber of correct answers: %v\n", total, correct)
}
// run is the main function to execute quiz app
func run(qinput io.Reader, timeout int) (total, correct int, err error) {
// Reading csv file and parse it to the records var
r := csv.NewReader(qinput)
records, err := r.ReadAll()
if err != nil {
return total, correct, err
}
// Two channels. One for answers, another for timeout
answerChan := make(chan string)
timerChan := make(chan time.Time, 1)
// Iterate over the records
for _, record := range records {
question, expected := record[0], record[1]
fmt.Printf("Question: %v. Answer: ", question)
total++
// Listening for input in the separate goroutine
go getAnswer(answerChan)
// Run the timer in separate goroutine if the timeout is specified
if timeout > 0 {
go func() { timerChan <- <-time.After(time.Duration(timeout) * time.Second) }()
}
// Main select block
select {
case answer := <-answerChan:
if answer == expected {
correct++
}
case <-timerChan:
fmt.Println()
return total, correct, errors.New("Timeout reached!")
}
}
return total, correct, nil
}
// getAnswer func is for listening input from user
func getAnswer(answerChan chan string) {
reader := bufio.NewReader(os.Stdin)
answer, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
answerChan <- strings.Replace(answer, "\n", "", -1)
}