postfix evaluation Algorithm

The postfix evaluation algorithm, also known as Reverse Polish Notation (RPN), is a mathematical notation method used for evaluating arithmetic expressions without the need for parentheses to indicate the order of operations. This algorithm simplifies the process of evaluating expressions by eliminating the need to keep track of precedence rules, and it is particularly well-suited for use in computer programs and calculators. In postfix notation, operators are placed after their operands, and the evaluation proceeds from left to right, applying the operators to the appropriate operands as soon as they become available. To evaluate a postfix expression, one typically uses a stack data structure. The algorithm iterates through the expression, examining each character. When an operand (number) is encountered, it is pushed onto the stack. When an operator is encountered, the required number of operands are popped from the stack (typically two for binary operators), the operation is performed on these operands, and the result is pushed back onto the stack. Once the entire expression has been processed, the final result, which is the only remaining item on the stack, is popped and returned as the result of the evaluation. This algorithm is efficient and easy to implement, making it a popular choice for expression evaluation in many programming languages and computing systems.
"""
Output:

Enter a Postfix Equation (space separated) = 5 6 9 * +
 Symbol  |    Action    | Stack
-----------------------------------
       5 | push(5)      | 5
       6 | push(6)      | 5,6
       9 | push(9)      | 5,6,9
         | pop(9)       | 5,6
         | pop(6)       | 5
       * | push(6*9)    | 5,54
         | pop(54)      | 5
         | pop(5)       |
       + | push(5+54)   | 59

        Result =  59
"""

import operator as op


def Solve(Postfix):
    Stack = []
    Div = lambda x, y: int(x / y)  # noqa: E731 integer division operation
    Opr = {
        "^": op.pow,
        "*": op.mul,
        "/": Div,
        "+": op.add,
        "-": op.sub,
    }  # operators & their respective operation

    # print table header
    print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
    print("-" * (30 + len(Postfix)))

    for x in Postfix:
        if x.isdigit():  # if x in digit
            Stack.append(x)  # append x to stack
            # output in tabular format
            print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | ")
        else:
            B = Stack.pop()  # pop stack
            # output in tabular format
            print("".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | ")

            A = Stack.pop()  # pop stack
            # output in tabular format
            print("".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | ")

            Stack.append(
                str(Opr[x](int(A), int(B)))
            )  # evaluate the 2 values popped from stack & push result to stack
            # output in tabular format
            print(
                x.rjust(8),
                ("push(" + A + x + B + ")").ljust(12),
                ",".join(Stack),
                sep=" | ",
            )

    return int(Stack[0])


if __name__ == "__main__":
    Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ")
    print("\n\tResult = ", Solve(Postfix))

LANGUAGE:

DARK MODE: