Created
October 20, 2017 19:19
-
-
Save ferrata/248c85047ff4ae80184f3b2cd79837f5 to your computer and use it in GitHub Desktop.
This file contains 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
ops = { | |
"+": (lambda a, b: a + b), | |
"-": (lambda a, b: a - b) | |
} | |
def eval(tokens): | |
stack = [] | |
for token in tokens: | |
#print token, stack | |
if token in ops: | |
arg2 = stack.pop() | |
arg1 = stack.pop() | |
#print ' ', arg2, token, arg1, | |
result = ops[token](arg1, arg2) | |
stack.append(result) | |
#print '->', result | |
#print ' ', stack | |
else: | |
stack.append(int(token)) | |
#print stack | |
return stack.pop() | |
print eval("7 2 3 + -".split()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment