Question: Function in python please and include explanations and a screenshot The code MUST use of the Stack ADT methods: i.e. Stack(), push(), pop(), peek(), size()
Function in python please and include explanations and a screenshot The code MUST use of the Stack ADT methods: i.e. Stack(), push(), pop(), peek(), size() and is_empty(). Write a function called evaluate_postfix(postfix_list) which take a list of operators and operands (all strings), representing a valid postfix expression, and the function should evaluate it. For example, the postfix expression: 2 4 3 * - which would be represented by the input list of strings: ['2', '4', '3', '*', '-'] would evaluate to -10.
Note: although the parameter list consist of strings, you should convert all operands into integers. When implementing division, you should use integer division (i.e. the // operator) so that any expression will evaluate to an integer value.
Hint: it may be useful to use the compute(number1, number2, operator) function. The function takes two operands and an operator as parameters and returns the result of applying the operator to the operands.
def compute(number1, number2, operator): if operator == '+': return number1 + number2 elif operator == '-': return number1 - number2 elif operator == '*': return number1 * number2 else: return number1 // number2
| Test | Result |
print(evaluate_postfix(['2', '10', '+'])) | 12 |
print(evaluate_postfix(['2', '4', '3', '*', '-'])) | -10 |
print(evaluate_postfix(['10', '4', '2', '-', '5', '*', '+', '3', '-'])) | 17 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
