Skip to content

Commit 5d2e716

Browse files
authored
Postfix_Evaluation
Postfix expression: The expression of the form “a b operator” (ab+) i.e., when a pair of operands is followed by an operator.
1 parent 3474d0a commit 5d2e716

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Postfix_Evaluation

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def evaluate_postfix(expression):
2+
stack=[]
3+
for token in expression.split():
4+
if token.isdigit():
5+
stack.append(int(token))
6+
else:
7+
b=stack.pop()
8+
a=stack.pop()
9+
if token=='+':
10+
stack.append(a+b)
11+
elif token=='-':
12+
stack.append(a-b)
13+
elif token=='*':
14+
stack.append(a*b)
15+
elif token=='/':
16+
stack.append(a/b)
17+
return stack[0]
18+
expression="5 1 2 + 4 * + 3 -"
19+
print(evaluate_postfix(expression))

0 commit comments

Comments
 (0)