diff --git a/main.go b/main.go new file mode 100644 index 0000000..1a735a5 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "os" + "strings" +) + +func main() { + csvFilename := flag.String("csv", "problems.csv", "a csv file in the format of 'question,answer'") + flag.Parse() + + file, err := os.Open(*csvFilename) + if err != nil { + exit(fmt.Sprintf("Failed to open the CSV file: %s\n", *csvFilename)) + } + r := csv.NewReader(file) + lines, err := r.ReadAll() + if err != nil { + exit("Failed to parse the provided CSV file.") + } + problems := parseLines(lines) + + correct := 0 + for i, p := range problems { + fmt.Printf("Problem #%d: %s = \n", i+1, p.q) + var answer string + fmt.Scanf("%s\n", &answer) + if answer == p.a { + correct++ + } + } + + fmt.Printf("You scored %d out of %d.\n", correct, len(problems)) +} + +func parseLines(lines [][]string) []problem { + ret := make([]problem, len(lines)) + for i, line := range lines { + ret[i] = problem{ + q: line[0], + a: strings.TrimSpace(line[1]), + } + } + return ret +} + +type problem struct { + q string + a string +} + +func exit(msg string) { + fmt.Println(msg) + os.Exit(1) +} diff --git a/students/sinetoami/main.go b/students/sinetoami/main.go new file mode 100644 index 0000000..054e78a --- /dev/null +++ b/students/sinetoami/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/csv" + "flag" + "fmt" + "os" +) + +type Problem struct { + question, answer string +} + +func main() { + filename := flag.String("csv", "problems.csv", "CSV file") + file, err := os.Open(*filename) + + if err != nil { + fmt.Errorf("could not open file: %v", err) + } + defer file.Close() + + lines, err := csv.NewReader(file).ReadAll() + if err != nil { + fmt.Errorf("could not read file: %v", err) + } + + problems := []Problem{} + for _, line := range lines { + problems = append(problems, Problem{line[0], line[1]}) + } + + score := 0 + for i, problem := range problems { + userAnswer := "" + fmt.Printf("Problem #%d: %s = ", i+1, problem.question) + fmt.Scan(&userAnswer) + if userAnswer == problem.answer { + score++ + } + } + + fmt.Printf("You scored %d out of %d.", score, len(problems)) +}