Skip to content

Instantly share code, notes, and snippets.

@kg86
Created August 30, 2014 04:20
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 kg86/3e94f1581e2605ede836 to your computer and use it in GitHub Desktop.
Save kg86/3e94f1581e2605ede836 to your computer and use it in GitHub Desktop.
#! -*- coding: utf-8-unix -*-
import sys
if __name__=='__main__':
lines = [x.strip() for x in sys.stdin.readlines() if x != '' and x != '\n']
for line in lines:
cs = line.split()
stack = []
res = 0.0
for c in cs:
res = ''
if not(c == '-' or c == '+' or c == '*' or c == '/'):
res = float(c)
else:
s1, s2 = stack.pop(), stack.pop()
if c == '-':
res = s2 - s1
elif c == '+':
res = s2 + s1
elif c == '*':
res = s2 * s1
elif c == '/':
res = s2 / s1
stack.append(res)
assert(len(stack) == 1)
print stack[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment