Skip to content

completed the task #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions students/mustafa/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
)

type Quiz struct {
Question string
Answer string
}

func ReadFile(filePath string) ([]Quiz, error) {
if filePath == "" {
return nil, nil
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var quizzes []Quiz
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
parts := strings.Split(line, ",")
if len(parts) != 2 {
continue
}
quizzes = append(quizzes, Quiz{
Question: parts[0],
Answer: parts[1],
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return quizzes, nil
}
func getInput(input chan string) {
for {
in := bufio.NewReader(os.Stdin)
result, err := in.ReadString('\n')
if err != nil {
log.Fatal("Error reading input:", err)
continue
}
input <- result
}
}
func startQuiz(quiz []Quiz, duration time.Duration) int {
input := make(chan string)
timer := time.After(duration * time.Second)
go getInput(input)
fmt.Println("Starting Quiz...")
totalScore := 0
for _, q := range quiz {
score, err := checkAnswer(q.Question, q.Answer, input, timer)
if err != nil {
fmt.Println("Error:", err)
break
} else if score == 0 {
fmt.Println("Wrong Answer!")
} else {
totalScore += score
}
}
return totalScore
}
func checkAnswer(question string, answer string, input <-chan string, timer <-chan time.Time) (int, error) {
fmt.Println(question)
select {
case <-timer:
return 0, errors.New("time is up")
case ans := <-input:
if strings.Compare(strings.TrimSpace(ans), strings.TrimSpace(answer)) == 0 {
fmt.Println("Correct Answer!")
return 1, nil
} else {
return 0, nil
}
}
}

func init() {
flag.Int("time", 30, "Time limit for all questions in seconds")
flag.Int("s", 0, "Shuffle the question order (default: 0)")
flag.Parse()
fmt.Println("no. of flags", flag.NFlag())
if flag.NFlag() == 0 {
fmt.Println("No flags provided. Using default time limit of 30 seconds.")
}
if flag.Lookup("time") == nil {
fmt.Println("No time flag provided. Using default time limit of 30 seconds.")
} else {
fmt.Println("Time limit set to:", flag.Lookup("time").Value)
}

}

func main() {
quizzes, err := ReadFile("problems.csv")
if err != nil {
log.Fatal("Error reading file:", err)
return
}
if len(quizzes) == 0 {
log.Fatal("No quizzes found in the file.")
return
}
fmt.Println("Shuffle flag:", flag.Lookup("s").Value)
if flag.Lookup("s") != nil && flag.Lookup("s").Value.(flag.Getter).Get().(int) == 1 {
for i := range quizzes {
j := rand.Intn(i + 1)
quizzes[i], quizzes[j] = quizzes[j], quizzes[i]

}
}
totalScore := startQuiz(quizzes, time.Duration(flag.Lookup("time").Value.(flag.Getter).Get().(int)))
fmt.Println("Total Score:", totalScore)
}
13 changes: 13 additions & 0 deletions students/mustafa/problems.csv
Original file line number Diff line number Diff line change
@@ -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