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