Skip to content

Instantly share code, notes, and snippets.

@astagi
Created August 30, 2012 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astagi/3525765 to your computer and use it in GitHub Desktop.
Save astagi/3525765 to your computer and use it in GitHub Desktop.
RPN calculation using Python
import re
def RPN(operation):
s = []
for el in operation.split(" "):
if el.isdigit():
s.append(el)
elif re.match("[+,-,*,/]", el) and len(s) > 1:
s.append(eval("%s %s %s" % ( s.pop(), el , s.pop() )))
else:
break
return s.pop()
operation = "5 10 2 * +"
print RPN(operation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment