output enter an infix equation a b c symbol stack postfix c c c b cb cb a cba cba abc infix abc prefix infix2postfixabc doctest normalizewhitespace symbol stack postfix a a a b ab ab c abc abc abc abc infix2postfix1a2b doctest normalizewhitespace symbol stack postfix 1 1 1 1 1 1 a 1a 1a 1a 2 1a2 1a2 b 1a2b 1a2b 1a2b 1a2b infix2postfix symbol stack postfix infix2postfix traceback most recent call last valueerror invalid expression infix2postfix traceback most recent call last indexerror list index out of range print table header for output infix2prefixabc doctest normalizewhitespace symbol stack postfix c c c b cb cb a cba cba abc infix2prefix1a2b doctest normalizewhitespace symbol stack postfix b b b 2 b2 b2 b2 a b2a b2a b2a b2a b2a 1 b2a1 b2a1 1a2b infix2prefix symbol stack postfix infix2prefix traceback most recent call last indexerror list index out of range infix2prefix traceback most recent call last valueerror invalid expression call infix2postfix on infix return reverse of postfix infix_2_postfix a b c doctest normalize_whitespace symbol stack postfix a a a b ab ab c abc abc abc abc infix_2_postfix 1 a 2 b doctest normalize_whitespace symbol stack postfix 1 1 1 1 1 1 a 1a 1a 1a 2 1a 2 1a 2 b 1a 2 b 1a 2 b 1a 2 b 1a 2 b infix_2_postfix symbol stack postfix infix_2_postfix traceback most recent call last valueerror invalid expression infix_2_postfix traceback most recent call last indexerror list index out of range priority of each operator print table header for output if x is alphabet digit add it to postfix if x is push to stack if x is pop stack until is encountered close bracket without open bracket pop stack add the content to postfix if stack is empty push x to stack while priority of x is not priority of element in the stack pop stack add to postfix push x to stack output in tabular format while stack is not empty open bracket with no close bracket pop stack add to postfix output in tabular format return postfix as str infix_2_prefix a b c doctest normalize_whitespace symbol stack postfix c c c b cb cb a cb a cb a a bc infix_2_prefix 1 a 2 b doctest normalize_whitespace symbol stack postfix b b b 2 b2 b2 b2 a b2a b2a b2a b2a b2a 1 b2a 1 b2a 1 1 a2b infix_2_prefix symbol stack postfix infix_2_prefix traceback most recent call last indexerror list index out of range infix_2_prefix traceback most recent call last valueerror invalid expression reverse the infix equation change to change to call infix_2_postfix on infix return reverse of postfix input an infix equation remove spaces from the input
def infix_2_postfix(infix: str) -> str:
    
    stack = []
    post_fix = []
    priority = {
        "^": 3,
        "*": 2,
        "/": 2,
        "%": 2,
        "+": 1,
        "-": 1,
    }  
    print_width = max(len(infix), 7)

    
    print(
        "Symbol".center(8),
        "Stack".center(print_width),
        "Postfix".center(print_width),
        sep=" | ",
    )
    print("-" * (print_width * 3 + 7))

    for x in infix:
        if x.isalpha() or x.isdigit():
            post_fix.append(x)  
        elif x == "(":
            stack.append(x)  
        elif x == ")":  
            if len(stack) == 0:  
                raise IndexError("list index out of range")

            while stack[-1] != "(":
                post_fix.append(stack.pop())  
            stack.pop()
        else:
            if len(stack) == 0:
                stack.append(x)  
            else:  
                while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:
                    post_fix.append(stack.pop())  
                stack.append(x)  

        print(
            x.center(8),
            ("".join(stack)).ljust(print_width),
            ("".join(post_fix)).ljust(print_width),
            sep=" | ",
        )  

    while len(stack) > 0:  
        if stack[-1] == "(":  
            raise ValueError("invalid expression")

        post_fix.append(stack.pop())  
        print(
            " ".center(8),
            ("".join(stack)).ljust(print_width),
            ("".join(post_fix)).ljust(print_width),
            sep=" | ",
        )  

    return "".join(post_fix)  


def infix_2_prefix(infix: str) -> str:
    
    reversed_infix = list(infix[::-1])  

    for i in range(len(reversed_infix)):
        if reversed_infix[i] == "(":
            reversed_infix[i] = ")"  
        elif reversed_infix[i] == ")":
            reversed_infix[i] = "("  

    
    return (infix_2_postfix("".join(reversed_infix)))[::-1]


if __name__ == "__main__":
    from doctest import testmod

    testmod()

    Infix = input("\nEnter an Infix Equation = ")  
    Infix = "".join(Infix.split())  
    print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)")