-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree.py
More file actions
executable file
·32 lines (25 loc) · 806 Bytes
/
three.py
File metadata and controls
executable file
·32 lines (25 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python2
import sys
def process_arguments():
if len(sys.argv) != 2:
print "Usage:\n " , sys.argv[0] , "<input_file>\n"
sys.exit(1);
filename = sys.argv[1]
return filename
def main():
filename = process_arguments()
with open(filename) as filebuffer:
for line in filebuffer:
stack = []
for token in line.split():
if token == "+":
v = stack.pop() + stack.pop()
stack.append(v)
elif token == "*":
v = stack.pop() * stack.pop()
stack.append(v)
else:
stack.append(int(token))
print line[:-1], " == ", stack.pop() , "\n"
if __name__ == '__main__':
main()