-
Notifications
You must be signed in to change notification settings - Fork 729
solve it #12
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
solve it #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| 5+5,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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/csv" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| type recordtype struct { | ||
| question string | ||
| answer string | ||
| } | ||
|
|
||
| // ReadStringWithLimitTime - function read string from reader with time limit | ||
| func ReadStringWithLimitTime(limit int) (string, error) { | ||
| timer := time.NewTimer(time.Duration(limit) * time.Second).C | ||
| doneChan := make(chan bool) | ||
| answer, err := "", error(nil) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a huge fan of this line here. What you are doing isn't wrong by any means, so this is a readability/style comment. If it were me, I'd instead replace this with: var answer string
var err error
// or
var (
answer string
err error
)Or even as a third alternative, you could use named return variables (I like this one less personally, but it is readable): func ReadStringWithLimitTime(limit int) (answer string, err error) {Yes, the first two options I presented are more code but I think it becomes clearer that you are just initializing them with zero values. Building on that, you never actually use the return answer, errBut this is the only place you return it, and we know for certain that it is going to be nil at this point (at least I do after looking at the earlier code - it isn't as clear when reading line 35). You could probably replace this line with return answer, nilNow it is clear you are returning a nil error in this case and you don't need that |
||
| go func() { | ||
| fmt.Scanf("%s\n", &answer) | ||
| doneChan <- true | ||
| }() | ||
| for { | ||
| select { | ||
| case <-timer: | ||
| return "", errors.New("Timer expired") | ||
| case <-doneChan: | ||
| return answer, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ParseLines - parse lines from array of array of string to array of recordtype | ||
| func ParseLines(lines [][]string) []recordtype { | ||
| ret := make([]recordtype, len(lines)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Nice job allocating exactly how much memory you use. Its minor, but really cool to get in the habit of it when you know that info. |
||
| for i, line := range lines { | ||
| ret[i] = recordtype{ | ||
| question: line[0], | ||
| answer: strings.TrimSpace(line[1]), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you trimmed up these strings 😄 |
||
| } | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| func exit(msg string) { | ||
| fmt.Println(msg) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| func main() { | ||
|
|
||
| problemFileName := flag.String("csv", "./problems.csv", "a csv file in the format 'quastion,answer'") | ||
| limit := flag.Int("limit", 30, "the time limit for the quiz in seconds") | ||
| flag.Parse() | ||
|
|
||
| problemFile, err := os.Open(*problemFileName) | ||
| if err != nil { | ||
| exit(fmt.Sprintf("Failed to open the CSV file: %s\n", *problemFileName)) | ||
| } | ||
|
|
||
| defer problemFile.Close() // close CSV file | ||
|
|
||
| readerProblem := csv.NewReader(problemFile) | ||
| lines, err := readerProblem.ReadAll() | ||
| if err != nil { | ||
| exit("Failed to parse the provided CSV file.") | ||
| } | ||
|
|
||
| problems := ParseLines(lines) | ||
|
|
||
| successAnswerCount := 0 | ||
| for i, p := range problems { | ||
| fmt.Printf("Problem #%d: %s=", i+1, p.question) | ||
|
|
||
| answer, err := ReadStringWithLimitTime(*limit) | ||
| if err != nil { | ||
| println("Time expire!") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stick to |
||
| break | ||
| } | ||
| if strings.ToLower(strings.Trim(answer, "\n ")) == p.answer { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need the On another note - you do |
||
| successAnswerCount++ | ||
| } | ||
| } | ||
| println("You scored", successAnswerCount, "out of", len(problems)) | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I believe this should be
recordType.