Created
May 30, 2016 02:38
-
-
Save cmccormack/ce066bd93329b690aab3af933acf38d1 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
def calc(expr): | |
operators = ['+', '-', '*', '/'] | |
stack = list() | |
for item in expr.split(' '): | |
if item in operators: | |
right, left = [str(i) for i in (stack.pop(), stack.pop())] | |
stack.append(eval(left + item + right)) | |
else: | |
stack.append(eval(item + '+ 0')) | |
return stack[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment