-
-
Save alexanderlarson/6259233 to your computer and use it in GitHub Desktop.
Working on an RPN calculator
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
def rpn(expression) | |
operands = [] | |
while expression.length > 0 | |
token = expression.shift # puts first value of expression into variable "token" | |
unless token =~ /^\d+$/ # Finding operators | |
right = operands.pop | |
left = operands.pop | |
token = eval "#{left} #{token} #{right}" #eval is really handy! | |
end | |
operands << token # push the evaluated value into the operands array | |
end | |
operands.pop # returns last value from operands array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment