-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.py
More file actions
28 lines (26 loc) · 1.06 KB
/
day19.py
File metadata and controls
28 lines (26 loc) · 1.06 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
def evaluate_postfix(expression):
stack = []
# Iterate through each token in the postfix expression
for token in expression:
# If the token is a number, push it to the stack
if token.isdigit():
stack.append(int(token))
else:
# Pop the top two numbers from the stack
operand2 = stack.pop()
operand1 = stack.pop()
# Apply the operator and push the result back to the stack
if token == '+':
stack.append(operand1 + operand2)
elif token == '-':
stack.append(operand1 - operand2)
elif token == '*':
stack.append(operand1 * operand2)
elif token == '/':
stack.append(operand1 / operand2)
elif token == '^':
stack.append(operand1 ** operand2)
return stack.pop()
expression = "231*+9-"
print("The result of the postfix expression is:", evaluate_postfix(expression))
""" push operands onto the stack.pop the required number of operands off the stack"""