Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an array-based bounded stack #7

Open
11 tasks
mwermelinger opened this issue Jan 6, 2024 · 0 comments
Open
11 tasks

Add an array-based bounded stack #7

mwermelinger opened this issue Jan 6, 2024 · 0 comments
Labels
effort: medium These issues require some work priority: low type: enhancement Improve an existing feature

Comments

@mwermelinger
Copy link
Member

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:

  • Create a class StaticArrayStack as a copy of DynamicArrayStack
  • Add the new class to the __all__ list.
  • Update the docstring of the new class, including the doctest.
  • Modify __init__:
    • Add an integer parameter capacity. Raise ValueError if capacity is negative or smaller than the length of parameter sequence. Update the doctest to use this parameter.
    • The _members array is created with length equal to capacity and filled with the items from sequence. Any unfilled positions have None.
    • Add a new instance variable _size that keeps track of the current size of the stack, which is also the first unfilled position.
  • Add a new method capacity that returns the fixed length of the array. Use this method in the doctest.
  • Change the stack operations so that they use _size, e.g. push must put the item at position _size and then increment _size.
  • Change method push to raise ValueError when the stack is full. Update the docstring accordingly.

In file tests/test_stack.py:

  • Add StaticArrayStack to the import statement (line 6), the type definition (line 10) and the Stack fixtures (line 26)
  • Betweentest_push and test_pop, add a function test_push_full(sequence: Sequence) that pushing doesn't change the stack's capacity and that ValueError is raised when pushing on a full stack. (See the check_is_empty function for how to do that.)
@mwermelinger mwermelinger added effort: medium These issues require some work priority: low type: enhancement Improve an existing feature labels Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: medium These issues require some work priority: low type: enhancement Improve an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant