Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IMPORTANT: For this exercise, you will be defining a function which USES the Stack ADT. A stack implementation is provided to you as part of
IMPORTANT: For this exercise, you will be defining a function which USES the Stack ADT. A stack implementation is provided to you as part of this exercise - you should not define your own Stack class. Instead, your code can make use of any of the Stack ADT methods: Stack().push(), pop(), peek(), size () and is_empty(). Write a function called get_postfix_expression () which takes a string, infix_expression as a parameter. The parameter string represents a mathematical expression in standard infix notation. For example: 2 * ( 6 + 3). The function should convert this infix expression to postfix. In this example, the result would be: 2 6 3 + * You can assume that the only operators used will be "+", "-", and "/". Note that the input string will have a single space surrounding each number and each bracket (except for the very first and very last elements in the string). Here are a few more examples: II_111111 2 * 3 + 4 2 + 3 * 4 1 * 3 + 2 * 4 ( 1 + 2 ) * ( 3 + 4 ) 2 3 * 4 + 2 3 4 * + 1 3 * 24 * + 1 2 + 3 4 + * For example: Test Result print(get_postfix_expression( '2 * ( 6 + 3)')) 2 6 3 + * print(get_postfix_expression( '3 + 4 * 7')) 3 4 7 * + Answer: (penalty regime: 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) Reset answer IMPORTANT: For this exercise, you will be defining a function which USES the Stack ADT. A stack implementation is provided to you as part of this exercise - you should not define your own Stack class. Instead, your code can make use of any the Stack ADT methods: Stack(), push(), pop(), peek(), size() and is_empty(). Write a function called evaluate_postfix (postfix_list) which take a list of strings consisting of operators and operands representing a valid postfix expression, and the function should evaluate it. For example, the postfix expression: 243* - which would be represented by the input list of strings: ["2", "4", "3", "*", "-"] would evaluate to -10. Note: 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. you can assume that the parameter list represents a valid postfix expression and that the only operators that appear will be "+", "-", "*", ", and "/" 1 Hint: you will probably find it useful to use the compute(number1, number2, operator) function provided in the answer box. The function takes two operands and an operator as parameters and returns the result of applying the operator to the operands. For example: Test Result 12 print(evaluate_postfix(['2', '10', '+'])) print(evaluate_postfix(['2', '4', '3', '*', '-'])) -10 print(evaluate_postfix( ['10', '4', '2', 15', '*', '+', '3', '-'])) 17 Answer: (penalty regime: 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) Reset answer 1 def evaluate_postfix(postfix_list)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started