Welcome! First of all, fork This Repository to your Github Account.
After that git clone it to your machine.
Create a git branch using the name pattern test/<your-github-username>
Do the developing on this branch.
Requires python >= 3.8
Create an virtual environment:
This tutorial snippets assumes you uses Linux. For installation on other OS check python documentation on venv.
python3 -m venv venv
Activate it:
source venv/bin/activate
Install pytest:
pip install pytest
That's it you are done! To run the tests run:
pytest
One of our clients had an series of documents that may depend on others. He wants us to everytime he deletes an document all children documents are deleted as well. You've been designated to do this task. For this example, we're going to call the document as an Node.
We're going to implement an Node class that will have an id and parent, and an NodeManager that will have an List of instance of the Nodes. This Manager will have the remove method that removes an single Node from the nodes member, and remove_cascade method that will removes all Nodes that has it as an parent, and all the Nodes that have been removed, must remove all Nodes that has it as an parent, and so on.
The requirements for the test are below. As you code, make sure that you run pytest to see your current progress. When all tests are passing, go ahead to the finishing part.
Inside nodes/node.py you must write the Node class.
Here are the requirements for an Node:
- The
Nodeclass must declare theidandparentmembers. They Must be type hinted asintegers. - An node
parentcannot be greater than it'sid. If this condition is not satisfied you should raise anValueErrorexception; - An node
parentcannot be the same as it's ownid. If this condition is not satisfied you should raise anValueErrorexception; - An
Nodeshould be instanced giving 2 integers like:Node(2, 1); If this condition is not satisfied you should raise anValueErrorexception; - For debugging purposes we'll like that the
Noderepresentation is equals to the way we instance it. Done in the__repr__ method;
Inside nodes/manager.py you must write the NodeManager class.
Here are the requirements for the NodeManager:
- Each
NodeManagermust declare thenodesmember. Must be type hinted as anListofNode. - It must receive an
ListofNodeto be instanced, thenodesmember should receive thisList. If this condition is not satisfied you should raise anValueErrorexception; - This object must not mutate the received
List; - We should be able to see the number of nodes in the
nodesmember through theNodeManagerinstance. Done in the__len__ method. - We should be able to get an
Nodeobject from thenodesmember through theNodeManagerinstance like an list. Done in the__getitem__ method - It must implement two methods
removeandremove_cascade. They both receives anNodeobject to be removed from thenodesmember and should returnNone. - The
removeandremove_cascademethod should raise anValueErrorif the receivedNodedoes not exist on thenodesmember. - The
removemethod must not remove its childrens. - The
remove_cascademethod should remove allNodefrom thenodesmember that have the parent equal to the receivedNode, this action must be performed again on the granchildren of the removedNode. Example:
Given the List[Node] below:
[
Node(1, 0),
Node(2, 1),
Node(3, 2),
Node(4, 2),
Node(5, 2),
Node(6, 5),
Node(7, 6),
Node(8, 7),
Node(9, 0),
]
If you call remove_cascade on the manager instance for the Node(2, 1) the nodes member must look like below:
[
Node(1, 0),
Node(9, 0),
]
This is expected because when we excluded the Node with id 2, the Nodes that has ids 3, 4, 5 as parent must be excluded, and the same is for the Node with id 6, that has the Node with 5 as parent and so on..
After you finish, push your local branch to your github repository and create a pull request to our master branch.