Skip to content

Instantly share code, notes, and snippets.

@darkf
Created September 4, 2012 08:57
Show Gist options
  • Select an option

  • Save darkf/3618747 to your computer and use it in GitHub Desktop.

Select an option

Save darkf/3618747 to your computer and use it in GitHub Desktop.
Dumb pure RPN calculator in Python
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