Question: PYTHON Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands
PYTHON
Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands (e.g., 3 4 +). It is different from infix notation in which operators are placed between its operands (e.g., 3 + 4).
The algorithm to evaluate any postfix expression uses a Stack object in the following way:
- Initialise an empty stack.
- For every token in the postfix expression (scanned from left to right):
- If the token is an operand (a number), push it onto the stack.
- Otherwise, if the token is an operator (or function):
- Check if the stack contains the sufficient number of values (usually two) for the given operator.
- If there are not enough values, finish the algorithm with an error.
- Pop the appropriate number of values from the stack.
- Evaluate the operator using the popped values and push the single result onto the stack.
- If the stack contains only one value, print the final result of the calculation.
- Otherwise, finish the algorithm with an error.
Consider the following postfix expression:
3 4 * 6 / 3 +
Assume that the Stack class has been created. Complete the following statements which evaluate the above postfix expression. You should assume that the Stack object has been created and is named postfix_stack. The initial two statements which push the first two values onto the postfix_stack stack have been done.
Note: You can assume that the expression is a valid postfix expression and you do not need to include any checking steps in your answer.
For example:
| Result |
| 5.0 |
postfix_stack = Stack() postfix_stack.push(3) postfix_stack.push(4)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
