Question: Please write the functions in python Also include explanations for each line of code in each function Also include screenshots for each function Question 1

Please write the functions in python Also include explanations for each line of code in each function Also include screenshots for each function

Question 1 Define functions using the Stack ADT Stack implementations are provided - do 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(). a) Write a function called get_top_of_stack(a_stack) which takes a Stack object as a parameter, and returns the value that is on the top of the stack. If the parameter stack is empty, the function should return None

Test Result
s = Stack() s.push(100) s.push(101) s.push(102) s.push(103) s.push(104) x = get_top_of_stack(s) print(x) print(s)
104 Stack: [100, 101, 102, 103, 104]
s = Stack() x = get_top_of_stack(s) print(x)
None

b) Write a function called is_balanced_brackets(text) which takes a string as a parameter, and returns a boolean. The function checks whether brackets are correctly balanced. There are three different types of brackets: ( ) , [ ] and <>. The function should return True if every opening bracket is matched by an appropriately-placed closing bracket of the same type, and False otherwise.

Test Result
print(is_balanced_brackets('()(())()'))
True
print(is_balanced_brackets('x(y)z'))
True
print(is_balanced_brackets('([x)](())()'))
False
print(is_balanced_brackets('x[y)(]z'))
False

c) Write a function named merge_two_stacks(stack1, stack2) which takes two SORTED (i.e. smallest element at the top) stacks as parameters and returns a new stack. The function should merge the two parameters stack into a new one, such that the elements become arranged in reverse sorted order (i.e. largest element at the top). Note: the size of the two parameter stacks may differ.

Test Result
s1 = Stack() s1.push_list([9, 7, 3, 2]) s2 = Stack() s2.push_list([6, 5, 4, 1]) print(s1) print(s2) print(merge_two_stacks(s1, s2))
Stack: [9, 7, 3, 2] Stack: [6, 5, 4, 1] Stack: [1, 2, 3, 4, 5, 6, 7, 9]
s1 = Stack() s1.push_list([9, 7]) s2 = Stack() s2.push_list([6, 5, 4, 1]) print(merge_two_stacks(s1, s2))
Stack: [1, 4, 5, 6, 7, 9]

d) Write a function named reverse_integers(numbers) which takes a list of numbers as a parameter and returns a new list. The function should reverse the order of the numbers in the parameter list using a stack.

Test Result
a_list = [1, 2, 3, 4.5] result = reverse_integers(a_list ) print(a_list) print(result)
[1, 2, 3, 4.5] [4.5, 3, 2, 1]

e) 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: 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. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!