Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calc_rev_pol(expr)
- return 0 if expr.empty?
- ary = expr.scan(/\d+\.?\d?|\D/).delete_if{|token| token == ' '}
- stack = []
- ary.each do |token|
- if %w(+ - / *).include?(token)
- val2 = stack.pop.to_f
- val1 = stack.pop.to_f
- stack.push(val1.send(token, val2))
- else
- stack.push(token)
- end
- end
- stack.pop.to_f
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement