class RPNCalculator
def initialize
end
def evaluate(string)
input = string.split(' ')
while (input.size > 1)
op_ind = input.index {|e| e =~ /[+*-]/ }
if op_ind.nil?
return input
else
operator = input.delete_at(op_ind)
first_arg = input[op_ind - 2].to_i
second_arg = input[op_ind - 1].to_i
output = first_arg.send(operator, second_arg)
input[op_ind - 2] = output.to_s
input.delete_at(op_ind - 1)
end
end
input
end
end
calc = RPNCalculator.new
puts calc.evaluate('5 1 2 + 4 * + 3 -')