Skip to content

Instantly share code, notes, and snippets.

@ferrata
Created October 20, 2017 19:19
Show Gist options
  • Save ferrata/248c85047ff4ae80184f3b2cd79837f5 to your computer and use it in GitHub Desktop.
Save ferrata/248c85047ff4ae80184f3b2cd79837f5 to your computer and use it in GitHub Desktop.
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