Stack Lab (CodeGrade)

Close

Learning Goals


Key Vocab


Instructions

In the previous lesson, you learned what a Stack is and what methods they commonly include. In this lab, you will be building out an implementation of a Stack. You will be using a list as the underlying data structure, and calling on some built-in Python list methods to build your Stack class's functionality.

Start by forking and cloning this lab. You'll be writing your code in the lib/stack.py file. You can run the tests at any point using pytest -x to check your work.

Bonus

If you'd like an extra challenge, try implementing the additional functionality below. There are tests for these in the testing/stack_test.py file; uncomment the bonus methods section in the test file to try these out.

  1. Modify your Stack __init__() method to take an optional limit value and set that as an attribute.
  2. Update the Stack push() value to only push the passed-in value if there's still room in the Stack. If the Stack is full, the method should throw an error.
  3. Implement the following additional methods:

After you've made these changes, you might want to take another look through your code and see if there's any refactoring you can do.


Conclusion

In this lesson, we got some practice building a data structure from scratch by implementing a Stack class. Recall that the runtime of our data structure will depend on what data structure it uses under the hood. For this lab, we used a list as the underlying data structure, which means the runtime for the search() method is O(n), and the runtime for all of the other methods is O(1).


Resources