5. Postfix to Infix expression
In [1]:
Copied!
expression1 = "a b + e f / *"
expression1 = expression1.split()
expression1 = "a b + e f / *"
expression1 = expression1.split()
In [2]:
Copied!
def postfix_to_infx(expression):
operator = ["+", "-", "*", "/", "^"]
stack = []
for i in expression:
if i in operator:
a = stack.pop()
b = stack.pop()
exp = b + i + a
stack.append(exp)
else:
stack.append(i)
print(stack)
def postfix_to_infx(expression):
operator = ["+", "-", "*", "/", "^"]
stack = []
for i in expression:
if i in operator:
a = stack.pop()
b = stack.pop()
exp = b + i + a
stack.append(exp)
else:
stack.append(i)
print(stack)
In [3]:
Copied!
postfix_to_infx(expression1)
postfix_to_infx(expression1)
['a+b*e/f']
In [ ]:
Copied!