Question: In python please, include explanation and screenshot The code MUST use of the Stack ADT methods: i.e. Stack(), push(), pop(), peek(), size() and is_empty(). Write
In python please, include explanation and screenshot The code MUST use of the Stack ADT methods: i.e. Stack(), push(), pop(), peek(), size() and is_empty().
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] |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
