Created
September 14, 2013 14:25
-
-
Save kirikiriyamama/6562465 to your computer and use it in GitHub Desktop.
逆ポーランド記法
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, separator = '') | |
stack = [] | |
expression.split(separator).each do |token| | |
if token =~ /[\d.]+/ | |
stack.push token.to_f | |
else | |
rhs = stack.pop | |
lhs = stack.pop | |
stack.push lhs.send(token, rhs) | |
end | |
end | |
stack.pop | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment