Prefix to Postfix Conversion

Prefix to Postfix Conversion: The program must accept a string S representing a prefix expression as the input. The program must convert the prefix expression to the corresponding postfix expression and print it as the output.

Boundary Condition(s):
3 <= Length of S <= 50

Example Input/Output 1:
Input:
*/+AB-CDE

Output:
AB+CD-/E*

Example Input/Output 2:
Input:
*/+A+BD-EFG

Output:
ABD++EF-/G*

s=input().strip()
stack=[]
operators=set(['+','-','*','/','^'])
s=s[::-1]
for i in s:
    if i in operators:
        a=stack.pop()
        b=stack.pop()
        temp=a+b+i
        stack.append(temp)
    else:
        stack.append(i)
print(*stack)
Previous Article

Multiple Interfaces - Department and College

Next Article

function splitEvenLengthWords

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *