Code required Extending from the Stack class: class Stack: def __init__(self): s
ID: 3747967 • Letter: C
Question
Code required
Extending from the Stack class:
class Stack:
def __init__(self):
self.lst = []
def push(self, item):
self.lst.append(item)
def pop(self):
if self.is_empty():
raise IndexError('The stack is empty!')
return self.lst.pop()
def peek(self):
if self.is_empty():
raise IndexError('The stack is empty!')
return self.lst[-1]
def size(self):
return len(self.lst)
Explanation / Answer
class Stack: def __init__(self): self.lst = [] def push(self, item): self.lst.append(item) def pop(self): if self.is_empty(): raise IndexError('The stack is empty!') return self.lst.pop() def peek(self): if self.is_empty(): raise IndexError('The stack is empty!') return self.lst[-1] def size(self): return len(self.lst) def is_empty(self): return self.size() == 0 def __str__(self): string = 'Stack: [' for i in range(len(self.lst)): string += str(self.lst[i]) if i != len(self.lst) - 1: string += ", " string += "]" return string def push_list(self, lst): for item in lst: self.lst.append(item) def multi_pop(self, n): if nRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.