I'm working on an RPN Calculator and think I'm almost there, except it's returning the expression not the solution.
def evaluate(expression)
expression = expression.split
operators = expression.select { |v| v =~ /\W/}
operands = expression.select { |v| v =~ /\d/}
new_expression = operands.zip(operators)
eval = new_expression.join
end
This should return -7
:
puts evaluate('5 8 + 4 - 5 *')
#=> 5+8-4*5