Skip to content

Instantly share code, notes, and snippets.

@petehuang
Last active December 27, 2015 02:59
Show Gist options
  • Save petehuang/7256153 to your computer and use it in GitHub Desktop.
Save petehuang/7256153 to your computer and use it in GitHub Desktop.
Refactoring rpn interview solution
def evaluate_rpn(string)
OPERATORS = ["+", "-", "*", "/"]
chars = string.split(" ")
numbers = []
chars.each do |char|
if !OPERATORS.include?(char)
numbers << char
else
second = numbers.pop
first = numbers.pop
string = "#{first}#{char}#{second}"
output = eval string
numbers << output.to_s
end
end
numbers.first
end
@petehuang
Copy link
Author

Instead of building a string like this:

string += first
string += char
string += second

we can just use string interpolation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment