-
Notifications
You must be signed in to change notification settings - Fork 5
Fix Issue #15 #28
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
base: master
Are you sure you want to change the base?
Fix Issue #15 #28
Changes from all commits
374ac7c
178ee8b
cd04421
9aa8541
444aded
c747d50
c79a361
6fd8ed0
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 |
|---|---|---|
|
|
@@ -107,6 +107,16 @@ def itsright | |
| puts "Correct!".colorize(:green) | ||
| end | ||
|
|
||
| def cheater | ||
| @num_wrong += 1 | ||
| puts "Please provide an answer "\ | ||
| "that operates on the given values. ".colorize(:light_red) | ||
| puts | ||
| puts "Try not to directly reference the value directly, ".colorize(:light_red) | ||
|
Collaborator
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. Line is too long. [84/80] |
||
| puts | ||
| puts "Instead, try accessing the value by its index or key!".colorize(:light_red) | ||
|
Collaborator
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. Line is too long. [87/80] |
||
| end | ||
|
|
||
| def prompter(answer) | ||
| print "Write ruby code to find the following value".colorize(:light_blue) | ||
| print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green) | ||
|
|
@@ -155,6 +165,45 @@ def check_answer(current, input, target) | |
| end | ||
| end | ||
|
|
||
| # deletes the target from the current data structure | ||
| def deep_delete(current, target) | ||
| return current unless current.is_a? Enumerable | ||
| current. | ||
| reject {|value| value == target}. | ||
|
Collaborator
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. Unnecessary spacing detected. |
||
| map {|value| deep_delete(value, target)} | ||
|
Collaborator
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. Unnecessary spacing detected. |
||
| end | ||
|
|
||
| # creates an array of values for which any given answer should fail | ||
| def tests(current, target) | ||
| [ nil, | ||
|
Collaborator
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. Space inside square brackets detected. |
||
| Array.new, | ||
| Hash.new, | ||
| RandomDataStructure.new.generate, | ||
| deep_delete(current, target) | ||
| ] | ||
| end | ||
|
|
||
| # test user input against various tests, for each of which the | ||
| # input should fail. Returns true only if the user input fails | ||
| # each of the tests. | ||
| def test_answer(current, input, target) | ||
| tests(current, target).all? do |test| | ||
| !run_test(test, input, target) | ||
| end | ||
| end | ||
|
|
||
| # test whether the answer provided by the given | ||
|
Collaborator
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. Trailing whitespace detected. |
||
| # routine matches the target answer | ||
| def run_test(current, input, target) | ||
|
Collaborator
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. Unused method argument - |
||
| routine = lambda { eval(input) } | ||
|
Collaborator
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. The use of |
||
| begin | ||
| output = routine.call | ||
| output == target | ||
| rescue Exception | ||
|
Collaborator
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. Avoid rescuing the |
||
| false | ||
| end | ||
| end | ||
|
|
||
| def generate_data_structure | ||
| rds = RandomDataStructure.new | ||
| current = rds.generate | ||
|
|
@@ -175,8 +224,12 @@ def play_round # new, exit or check if right/wrong | |
| return | ||
| else | ||
| if check_answer(current, input, target) | ||
| itsright | ||
| correct = true | ||
| if !test_answer(current, input, target) | ||
| cheater | ||
| else | ||
| itsright | ||
| correct = true | ||
| end | ||
| else | ||
| itswrong(target) | ||
| end | ||
|
|
||
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.
Do not write to stdout. Use Rails' logger if you want to log.