Add an array-based bounded stack #7
Labels
effort: medium
These issues require some work
priority: low
type: enhancement
Improve an existing feature
A bounded stack has a fixed capacity (maximum number of members). When the capacity is reached, the stack is full and no further items can be pushed.
A bounded stack can be implemented as follows. (Tick off the checkboxes as you progress.)
In file
stack.py
:StaticArrayStack
as a copy ofDynamicArrayStack
__all__
list.__init__
:capacity
. RaiseValueError
ifcapacity
is negative or smaller than the length of parametersequence
. Update the doctest to use this parameter._members
array is created with length equal to capacity and filled with the items fromsequence
. Any unfilled positions haveNone
._size
that keeps track of the current size of the stack, which is also the first unfilled position.capacity
that returns the fixed length of the array. Use this method in the doctest._size
, e.g.push
must put the item at position_size
and then increment_size
.push
to raiseValueError
when the stack is full. Update the docstring accordingly.In file
tests/test_stack.py
:StaticArrayStack
to the import statement (line 6), the type definition (line 10) and theStack
fixtures (line 26)test_push
andtest_pop
, add a functiontest_push_full(sequence: Sequence)
that pushing doesn't change the stack's capacity and thatValueError
is raised when pushing on a full stack. (See thecheck_is_empty
function for how to do that.)The text was updated successfully, but these errors were encountered: