Reverse Polish notation You must evaluate a string written in Reverse Polish notation and output the result.

The program must accept an input and return the output. For programming languages that do not have functions to receive input/output, you can assume functions like readLine/print.

You are not allowed to use any kind of "eval" in the program.

Numbers and operators are separated by one or more spaces.

You must support at least the +, -, * and / operators.

You need to add support to negative numbers (for example, -4 is not the same thing as 0 4 -) and floating point numbers.

You can assume the input is valid and follows the rules above



Test Cases

Input:

-4 5 +


Output:

1




Input:

5 2 /


Output:

2.5




Input:

5 2.5 /


Output:

2




Input:

5 1 2 + 4 * 3 - +


Output:

14




Input:

4 2 5 * + 1 3 2 * + /


Output:

2
Ruby - 95 77 characters

a=[]
gets.split.each{|b|a&lt;&lt;(b=~/\d/?b.to_f: (j,k=a.pop 2;j.send b,k))}
p a[0]


Takes input on stdin.

Testing code

[
  "-4 5 +",
  "5 2 /",
  "5 2.5 /",
  "5 1 2 + 4 * 3 - +",
  "4 2 5 * + 1 3 2 * + /",
  "12 8 3 * 6 / - 2 + -20.5 "
].each do |test|
  puts "[#{test}] gives #{`echo '#{test}' | ruby golf-polish.rb`}"
end


gives

[-4 5 +] gives 1.0
[5 2 /] gives 2.5
[5 2.5 /] gives 2.0
[5 1 2 + 4 * 3 - +] gives 14.0
[4 2 5 * + 1 3 2 * + /] gives 2.0
[12 8 3 * 6 / - 2 + -20.5 ] gives 10.0


Unlike the C version this returns the last valid result if there are extra numbers appended to the input it seems.