Created
August 30, 2014 04:20
-
-
Save kg86/3e94f1581e2605ede836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! -*- 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