Skip to content

Instantly share code, notes, and snippets.

@cmccormack
Created May 30, 2016 02:38
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 cmccormack/ce066bd93329b690aab3af933acf38d1 to your computer and use it in GitHub Desktop.
Save cmccormack/ce066bd93329b690aab3af933acf38d1 to your computer and use it in GitHub Desktop.
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