Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

here is the stacks.py code class Empty(Exception): pass class Stack(object): \"\

ID: 3904810 • Letter: H

Question

here is the stacks.py code

class Empty(Exception):
pass

class Stack(object):
"""Array based LIFO Stack data structure."""

def __init__(self):
self._data = []

def __len__(self):
return len(self._data)

def is_empty(self):
return len(self) == 0

def push(self, e):
self._data.append(e)

def top(self):
if self.is_empty():
raise Empty("Stack is empty.")
return self._data[-1]

def pop(self):
if self.is_empty():
raise Empty("Stack is empty.")
return self._data.pop()

if __name__ == "__main__":
pass
# your test can be done here

Implement a recursive method for removing all the elements from a stack. Test your implementation your method by creating a stack from Stacks-py and running

Explanation / Answer

class Empty(Exception): pass class Stack(object): """Array based LIFO Stack data structure.""" def __init__(self): self._data = [] def __len__(self): return len(self._data) def is_empty(self): return len(self) == 0 def push(self, e): self._data.append(e) def top(self): if self.is_empty(): raise Empty("Stack is empty.") return self._data[-1] def pop(self): if self.is_empty(): raise Empty("Stack is empty.") return self._data.pop() def clean_stack(s): if not s.is_empty(): s.pop() clean_stack(s) if __name__ == "__main__": s = Stack() s.push(5) s.push(2) s.push(1) s.push(8) print(s.is_empty()) clean_stack(s) print(s.is_empty())