Practical No - 9
Practical No - 9
STD:SYCS B
ROLL NO:5378
Practical no 9
OUTPUT:
NAME:KAUSHIK RODPALKAR
STD:SYCS B
ROLL NO:5378
b) Postfix Evaluation:
Code:
def postfix_evaluation(s):
s = s.split()
n = len(s)
stack = []
for i in range(n):
if s[i].isdigit():
stack.append(int(s[i]))
elif s[i] == "+":
a = stack.pop()
b = stack.pop()
stack.append(int(a) + int(b))
elif s[i] == "*":
a = stack.pop()
b = stack.pop()
stack.append(int(a) * int(b))
elif s[i] == "/":
a = stack.pop()
b = stack.pop()
stack.append(int(b) / int(a))
elif s[i] == "-":
a = stack.pop()
b = stack.pop()
stack.append(int(b) - int(a))
return stack.pop()
s = "10 2 8 * + 3 -"
val = postfix_evaluation(s)
print("Post-fix Evaluation Value:", val)
OUTPUT: