-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.py
More file actions
57 lines (44 loc) · 1.41 KB
/
ex1.py
File metadata and controls
57 lines (44 loc) · 1.41 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 338 Lab 5 Exercise 1
# Note for video: to run, put test value '(+ 1 5)' - output should be 6
import sys
def evaluate(symbols):
stack = []
operators = {'+', '-', '*', '/'}
for symbol in reversed(symbols):
if symbol in operators:
op1 = stack.pop()
op2 = stack.pop()
if symbol == '+':
stack.append(op1 + op2)
elif symbol == '-':
stack.append(op1 - op2)
elif symbol == '*':
stack.append(op1 * op2)
elif symbol == '/':
stack.append(op1 // op2)
else:
stack.append(int(symbol))
return stack.pop()
def parse_expression(expr):
expr = expr.replace('(', ' ( ').replace(')', ' ) ')
symbols = expr.split()
return process_symbols(symbols)
def process_symbols(symbols):
stack = []
for symbol in symbols:
if symbol == ')':
sub_expr = []
while stack and stack[-1] != '(':
sub_expr.append(stack.pop())
stack.pop()
stack.append(evaluate(sub_expr[::-1]))
else:
stack.append(symbol)
return stack.pop()
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python ex1.py '<expression>'")
sys.exit(1)
expression = sys.argv[1]
result = parse_expression(expression)
print(result)