Question: class Stack: def __init__(self): self.__items = [] def is_empty(self): return self.__items == [] def push(self, item): self.__items.append(item) def pop(self): if self.is_empty(): raise IndexError('ERROR: The stack

 class Stack: def __init__(self): self.__items = [] def is_empty(self): return self.__items== [] def push(self, item): self.__items.append(item) def pop(self): if self.is_empty(): raise IndexError('ERROR:The stack is empty!') return self.__items.pop() def peek(self): if self.is_empty(): raise IndexError('ERROR:

class Stack: def __init__(self): self.__items = []

def is_empty(self): return self.__items == []

def push(self, item): self.__items.append(item)

def pop(self): if self.is_empty(): raise IndexError('ERROR: The stack is empty!') return self.__items.pop()

def peek(self): if self.is_empty(): raise IndexError('ERROR: The stack is empty!') return self.__items[len(self.__items) - 1] def size(self): return len(self.__items) def __str__(self): return f'Stack: {self.__items}' def clear(self): self.__items = []

Continuing on from the previous question, extend the Stack class by adding the multi_push(self, element, how_many) method which pushes the parameter element onto the stack the number of times specified by the parameter how_many. Submit the entire Stack class definition. You can assume that the how_many parameter will be > 0. Keep a copy of your solution to this task because you will be extending it step by step in subsequent tasks. For example: Test Result 6 d Stack: ['a', 'b', 'c', 'd', 'd', 'd'] my_stack = Stack() my_stack.push('a') my_stack.push('b') my_stack.push('c') my_stack.multi_push('d', 3) print(my_stack.size()) print(my_stack. peek()) print(my_stack) Continuing on from the pevious question, extend the Stack class by adding the push_list(self, a_list) method which pushes all elements from the parameter list onto the stack. Submit the entire Stack class definition in the answer box below. You can assume that the parameter is a valid list. Keep a copy of your solution to this task because you will be extending it step by step in subsequent tasks. For example: Test Result Stack: [4, 5, 6] 6 The stack contains 3 items. my_stack = Stack() mylist = [4,5,6] my_stack.push_list(mylist) print(my_stack) print(my_stack. peek()) print("The stack contains {} items.". format (my_stack.size())) my_stack = Stack() my_stack.push_list(['a']) print(my_stack. peek () ) print("The stack contains {} item.". format(my_stack.size())) a The stack contains 1 item. Continuing on from the previous question, extend the Stack class by adding the to_list(self) method which returns all elements in the stack as a list. The list must be a new Python list. Submit the entire Stack class definition. For example: Test Result Stack: [4, 5] [4, 5, 6] my_stack = Stack() for num in range(4,7): my_stack.push(num) result = my_stack.to_list() my_stack.pop() print(my_stack) print (result) Stack: ['a'] my_stack = Stack() my_stack.push('a') print(my_stack)

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!