Skip to content

Instantly share code, notes, and snippets.

@sangwon090
Last active November 9, 2019 07:08
Show Gist options
  • Save sangwon090/5b711ecd144684fcb35f8f5d27ee3fb4 to your computer and use it in GitHub Desktop.
Save sangwon090/5b711ecd144684fcb35f8f5d27ee3fb4 to your computer and use it in GitHub Desktop.
[Python] Reverse Polish Notation
stack = []
for i in input().split(' '):
if i.isdigit():
stack.append(int(i))
else:
a = stack.pop()
b = stack.pop()
if i == '+':
stack.append(a + b)
elif i == '-':
stack.append(b - a)
elif i == '*':
stack.append(a * b)
elif i == '/':
stack.append(b / a)
elif i == '%':
stack.append(b % a)
print(stack.pop())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment