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
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())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.