The goal of this assignment is to get some more practice writing recursive methods, as well as to get some practice with object-oriented programming concepts like inheritance.
Setup and Handin
In order to setup homework4, download the stencil code files. Place the
hw4 folder into your cs112 Project folder in pyCharm.
Handin the following files to gradescope when submitting the assignment:
Remember to include a README.txt when submitting your assignment!
Your friendly TAs and Professor are here to help with this assignment! You can find our hours schedule here.
We have provided the Linked List implementation from class, along with some
tests. We’d like you to add a method to remove nodes, then change the class to
have a more efficient
You should add a method called
remove, which removes a node by its
i.e if we have a->b->c->d and remove the 0th index it would become b->c->d.
You should implement
remove using a recursive helper method called
remove_from. Think about what the base case and recursive case should look
If the specified index isn’t in the list (i.e., if it is less than 0 or greater
than or equal to the list’s length), you should raise an exception (see the
nth for an example of what this might look like).
You should write good tests for
remove. Make sure to test error cases (see the
length more efficient
Right now, the
length() method runs in linear time in the length of the list,
since it has to look at every node. We can do this more efficiently by adding a
count to the
count represents the number of
nodes in the list. It should be initialized to 0 in
__init__, and be updated
remove. You can then modify
length to return
instead of looking at the nodes in the list.
Modify the linked list implementation (
append, and your
method) to track the
You don’t need to write or modify any tests for this part of the
assignment. Once you’ve fixed all of the methods, our test suite (as well as the
tests you wrote for
remove) should pass.
We’d like you to implement a (very) simplified version of the logic for a cloud-based document editing and sharing system (like Google Docs). You’ll implement two classes that handle document creation and permissions.
Take a look at the
Document class in
docs.py. This class represents
individual documents, which have:
- a name
- contents (i.e., text)
- a list of user IDs who can access the document
You’ll implement two classes:
User has a collection
of documents, some of which may be shared with other users. An
AdminUser is a
user who can access any document. Your
AdminUser class should inherit from
We have provided some commented-out tests in
test_docs.py. Once you have
AdminUser classes, you can uncomment these
tests. Note that you should write your own tests for all of your methods in
addition to the included tests.
The __init__ method should take in a user id (an integer). It should
initialize two fields: a
user_id field that records the user’s ID and a
documents field that holds an (initially empty) list of the documents the user
The create_document method should take in the name and contents of a
document. It should create a new document (i.e., a new instance of the
Document class) and add it to the user’s list of documents. The constructor
Document class takes a user ID; you should pass in the user’s ID for
The can_access method takes in a
Document and returns a boolean. It should
check whether the user’s ID is in the set of users who can access the document
(which you can get with
The get_document method takes in the index of a document and returns that
document (i.e., looks up that index in the user’s documents list). If there is
no such document in the list, your method should raise a
The AdminUser class should be a subclass of the
User class. You will need to
implement one method on this class. Note that
AdminUser should not implement
Admin users should be able to access any document, so this method should always
True. It should have the same signature as the
can_access method on
Write good tests for all of your methods–you know how to do this!
Include tests for exceptional cases–you can take a look at the included tests
test_docs.py for an example.
README.txt, please include answers to the following questions:
- Did you discuss this assignment with any other students? Please list their cs logins.
- How many late days are you using on this assignment?