Last active
December 18, 2015 12:49
-
-
Save tlands/5785744 to your computer and use it in GitHub Desktop.
Here's a Ruby version of RPN calculator.
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
class RPNCalculator | |
def evaluate(str) | |
ary = [] | |
str.split.each do |i| | |
if i.match(/\d/) | |
ary << i.to_i | |
elsif i.match(/\-|\+|\*/) | |
operator = i.to_sym | |
ary << ary.pop(2).inject(operator) | |
end | |
end | |
return ary.join.to_i | |
end | |
end | |
calc = RPNCalculator.new | |
calc.evaluate('70 10 4 + 5 * -') # => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WHY DOES iT iNDENT iT SO AWFULLY