Code golf: Reverse Polish notation (postfix) evaluator
Posted
by Dario
on Stack Overflow
See other posts from Stack Overflow
or by Dario
Published on 2009-10-08T16:31:30Z
Indexed on
2010/06/06
20:32 UTC
Read the original article
Hit count: 401
code-golf
|rosetta-stone
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.
© Stack Overflow or respective owner