Open
Conversation
CalculatorWhat We're Looking For
|
CheezItMan
reviewed
Feb 11, 2018
| def parse(input) | ||
| # Removes white space, converts string into an array split by the possible operators (but retaining the operators). | ||
| # Removes empty strings from the array. I'm actually not sure where these are coming from, but I noticed them when the user enters a negative number. | ||
| parsed_input = input.gsub(/\s+/,"").split(/(\+)|(-)|(\*)|(\/)|(\^)|(%)|(add)|(subtract)|(multiply)|(divide)|(exponent)|(modulo)/).reject { |x| x.empty? } |
There was a problem hiding this comment.
Nice regular expression to split the input string.
|
|
||
| # Informs user of the limits of the calculator, the possible operators, and prompts user for their calculation. | ||
| puts "Welcome to my (very limited) calculator. This calculator can perform basic arithmetic operations between two numbers." | ||
| puts "\nThe accepted operators are: +, -, *, /, ^, or %, or the words add, subtract, multiply, divide, exponent, or modulo." |
There was a problem hiding this comment.
I would suggest an example of valid input. At first I was entering just the operator (-,+,/,*, etc).
| puts "Hmm. It doesn't look like the operation you've entered is valid for this calculator." | ||
| # If the array length is less than 3, the user likely didn't enter enough information, | ||
| # or didn't enter it in a manner that the parser could handle. Returns nil. | ||
| elsif (parsed_array[0] =~ /^[-+]?\d+(\.\d+)?$/).nil? || (parsed_array[2] =~ /^[-+]?\d+(\.\d+)?$/).nil? |
There was a problem hiding this comment.
Nice regular expression, I would suggest to use /^[-+]?\d*(\.\d+)?$/ instead of what you have there. Yours requires at least 1 digit to the left of the decimal so .25 wouldn't work.
| parsed_input[3] = parsed_input[2..3].join | ||
| parsed_input.slice!(2) | ||
| end | ||
| return parsed_input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions