Skip to content
Snippets Groups Projects
Commit 5b315b9d authored by hyunchun's avatar hyunchun
Browse files

simplified rpn

parent 3b9c66de
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#!/usr/bin/env python3
import operator
ops = {
'+': operator.add,
'-': operator.sub,
}
def calculate(myarg):
stack = list()
for token in myarg.split():
if token == '+':
arg2 = stack.pop()
arg1 = stack.pop()
result = arg1 + arg2
stack.append(result)
elif token == '-':
try:
stack.append(int(token))
except ValueError:
arg2 = stack.pop()
arg1 = stack.pop()
result = arg1 - arg2
function = ops[token]
result = function(arg1, arg2)
stack.append(result)
else:
stack.append(int(token))
print(stack)
#print(stack)
return stack.pop()
def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment