Created
August 30, 2012 10:26
-
-
Save astagi/3525765 to your computer and use it in GitHub Desktop.
RPN calculation using 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 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