Code golf: Reverse Polish notation (postfix) evaluator
- by Dario
After having had code-golf contests on ordinary mathematical expressions, I just thought it would also be quite interesting how short an evaluation function for postfix notation (RPN) can be.
Examples for RPN:
1 2 + == 3
1 2 + 3 * == 9
0 1 2 3 + + - 2 6 * + 3 / 1 - == 1
3 2 / 2.0 + == 3.5
To make things shorter, only +, -, * and /, all just in their binary version, must be supported. Operators/Operands are delimited by one space, divsions by zero don't have to be handled.
The resulting code should be a function that takes the postfix string as input and returns the resulting number.