Created
September 4, 2012 08:57
-
-
Save darkf/3618747 to your computer and use it in GitHub Desktop.
Dumb pure RPN calculator in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import operator | |
| def do_op(op, lhs, rhs): | |
| return \ | |
| {'+': operator.add, | |
| '-': operator.sub, | |
| '*': operator.mul}[op](lhs, rhs) | |
| def rpn(lst, stack): | |
| if lst == []: | |
| return stack | |
| if lst[0] in '-+*': | |
| return rpn(lst[1:], [do_op(lst[0], stack[1], stack[0])] + stack[2:]) | |
| return rpn(lst[1:], [int(lst[0])] + stack) | |
| print rpn("90 34 12 33 55 66 + * - + -".split(), []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment