Created
August 13, 2013 05:20
-
-
Save TGOlson/6218116 to your computer and use it in GitHub Desktop.
Reverse Polish Calculator - Version 1 (much better!)
This file contains 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
# version 2 of 2 | |
# updated to handle negative numbers, removed while loop, and uses send method | |
class RPNCalculator | |
def evaluate(string) | |
array = string.split | |
output = [] | |
array.each do |x| | |
if not x =~ /-?\d/ #if current item is not a digit | |
holder = output.pop(2) | |
x = holder[0].send(x, holder[1]) | |
end | |
output << x.to_i | |
end | |
output.pop | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment