Skip to content

Instantly share code, notes, and snippets.

@nimamehanian
Created February 21, 2013 02:28
Show Gist options
  • Save nimamehanian/5001487 to your computer and use it in GitHub Desktop.
Save nimamehanian/5001487 to your computer and use it in GitHub Desktop.
RPN Calculator
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