Created
February 21, 2013 02:28
-
-
Save nimamehanian/5001487 to your computer and use it in GitHub Desktop.
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
class Rpn | |
def evaluate(expression) | |
operators = ['+', '-', '*'] | |
stack = [] | |
symbols = expression.split(' ') | |
symbols.each do |symbol| | |
if operators.include?(symbol) | |
number_two = stack.pop | |
number_one = stack.pop | |
stack.push(number_one.send(symbol, number_two)) | |
else | |
stack.push(symbol.to_i) | |
end | |
end | |
p stack.pop | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment