Implement a Stack in Python

For this assignment, you will add the implementation of a stack to your data-structures repository.

Tasks

The Stack class should be in a file called stack.py and the class should be called Stack. This class should implement two public methods:

  • push(value) - Adds a value to the stack. The parameter is the value to be added to the stack.
  • pop() - Removes a value from the stack and returns that value. If the stack is empty, attempts to call pop should raise an appropriate Python exception with message.

In addition to these methods above, your implementation also needs to be able to interact with the built-in len() function. It should return the size of the stack.

Your class implementation should include a constructor that optionally accepts an iterable of values. When an iterable is passed, the result should be a stack which contains the values in the iterable.

Your implementation is not allowed to use the built-in Python list, tuple or dictionary. Nor are you allowed to use any of the native implementations of stack/queue/dequeue from the queue python standard library module.

Because you’ve probably already built much of this functionality already in the Linked List assignment, you may want to consider using inheritance or composition to build this data structure instead of constructing it from scratch.

Your implementation must include pytest unit tests. For each method on your stack class, including the constructor, write your tests first, then implement the methods that will support them. Be thorough in your tests, test failure modes as well as successes. Tests are as much a part of this assignment as the stack itself.

Update the repository README.md with information about the Stack you implemented, including any resources or collaborations you used. It is a requirement that for each method of this data structure, you include in the README a description of its time complexity in Big-O notation! Expect to lose credit if you don’t have this.

Submitting Your Work

Work on a branch in your GitHub repository. When you are done, and all tests are passing, open a pull request from your stack branch back to the master branch of your repository. Submit the URL for that pull request.

Once you have submitted the assignment, you may merge the pull request back to master. I’ll still be able to interact with it properly.

Use the comment feature to add any questions or comments you come up with while working on this assignment.