Skip to content

Instantly share code, notes, and snippets.

@kirikiriyamama
Created September 14, 2013 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirikiriyamama/6562465 to your computer and use it in GitHub Desktop.
Save kirikiriyamama/6562465 to your computer and use it in GitHub Desktop.
逆ポーランド記法
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