I need help understanding this task:
Modify the ArrayStack implementation so that the stack’s capacity is limited to maxlen elements, where maxlen is an optional parameter to the constructor (that defaults to None). If push is called when the stack is at full capacity, throw a Full exception (defined similarly to Empty).
This is what I am fiddling with
from exceptions import Empty
from exceptions import Full
class ArrayStack:
def __init__(self):
self._data = [] # nonpublic list instance
def __len__(self):
return len(self._data)
def is_empty(self):
return len(self._data) == 0
def is_full(self):
return len(self.data) == n-1
def push(self, e):
self._data.append(e) # new item stored at end of list
if self.is_full():
raise Full('Stack is full')
return self._data[n-1]
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()