From 165f52319c3fee9d856cb9281c0843cd2596c43b Mon Sep 17 00:00:00 2001 From: Pedro Henrique Date: Thu, 9 Mar 2023 20:40:56 -0300 Subject: [PATCH 1/2] Implement the NodeManager Class --- nodes/manager.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/nodes/manager.py b/nodes/manager.py index b3c1178..d504631 100644 --- a/nodes/manager.py +++ b/nodes/manager.py @@ -1,5 +1,31 @@ # Create here the NodeManager Class - +from typing import List +from nodes.node import Node class NodeManager: - pass + nodes: List[Node] + + def __init__(self, nodes:List[Node]) -> None: + if not isinstance(nodes, list): + raise ValueError("nodes must be a list of Node") + + self.nodes = nodes.copy() + + def __len__(self) -> int: + return len(self.nodes) + + def __getitem__(self, index:int) -> Node: + return self.nodes[index] + + def remove(self, node:Node) -> None: + if node not in self.nodes: + raise ValueError("node not found in nodes list") + self.nodes.remove(node) + + def remove_cascade(self, node:Node) -> None: + if node not in self.nodes: + raise ValueError("node not found in nodes list") + children = [n for n in self.nodes if n.parent == node.id] + for child in children: + self.remove_cascade(child) + self.nodes.remove(node) \ No newline at end of file From 37e1000a69f4c3b912e6b6ea6ca484cc2cfc35ae Mon Sep 17 00:00:00 2001 From: Pedro Henrique Date: Thu, 9 Mar 2023 20:41:25 -0300 Subject: [PATCH 2/2] Implement Node Class --- nodes/node.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/nodes/node.py b/nodes/node.py index 644ab4e..81ca97d 100644 --- a/nodes/node.py +++ b/nodes/node.py @@ -2,4 +2,20 @@ class Node: - pass + id : int + parent : int + + def __init__(self, id: int, parent: int) -> None: + if not isinstance(id,int): + raise ValueError("id must be an integer") + if not isinstance(parent, int): + raise ValueError("parent must be an integer") + if parent >= id: + raise ValueError("parent must be less than id") + + self.id = id + self.parent = parent + + def __repr__(self) -> str: + return f"Node({self.id},{self.parent})" + \ No newline at end of file