# Question 5 Incorrec Mark 038 100 Postfix Notation Also Known Reverse Polish Notation Rpn S Q30962822

In python language thanks~!

Question 5 Incorrec Mark 0.38 out of 1.00 Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: 1. Initialize empty stack 2. For every token in the postfix expression (scanned from left to right) Flag question 1. If the token is an operand (number). push it on the stack 2. Otherwise, if the token is an operator (or function) 1. Check if the stack contains the sufficient number of values (usually two) for given operator 2. If there are not enough values, finish the algorithm with an error 3. Pop the appropriate number of values from the stack 4. Evaluate the operator using the popped values and push the single result on the stack 3. Return the final result of the calculation Implement a function def eval_postfix(expression_str) that evaluates and returns the result of a postfrix expression. The postfix expression to be evaluated is passed to the function as a string parameter. Each component of the postfix expression string is separated by a single space. Your function needs to support the following operations: “+, “, “RI’, “””. “%”, and ‘ox”. Your function should also work with values consisting of more than one digit. For example Test Result print(eval_postfix(“2 3 * 4 1 print(eval_postfix(“2 3 4 *”)) 14 Answer (penalty regime: 0 %) 1def eval postfix(expression_str): s my_module.Stack() Show transcribed image text Question 5 Incorrec Mark 0.38 out of 1.00 Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: 1. Initialize empty stack 2. For every token in the postfix expression (scanned from left to right) Flag question 1. If the token is an operand (number). push it on the stack 2. Otherwise, if the token is an operator (or function) 1. Check if the stack contains the sufficient number of values (usually two) for given operator 2. If there are not enough values, finish the algorithm with an error 3. Pop the appropriate number of values from the stack 4. Evaluate the operator using the popped values and push the single result on the stack 3. Return the final result of the calculation Implement a function def eval_postfix(expression_str) that evaluates and returns the result of a postfrix expression. The postfix expression to be evaluated is passed to the function as a string parameter. Each component of the postfix expression string is separated by a single space. Your function needs to support the following operations: “+, “, “RI’, “””. “%”, and ‘ox”. Your function should also work with values consisting of more than one digit. For example Test Result print(eval_postfix(“2 3 * 4 1 print(eval_postfix(“2 3 4 *”)) 14 Answer (penalty regime: 0 %) 1def eval postfix(expression_str): s my_module.Stack()

0 replies