-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardnode.py
More file actions
41 lines (33 loc) · 1.03 KB
/
cardnode.py
File metadata and controls
41 lines (33 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import math
import copy
class CardNode:
def __init__(self, value: int):
self.children: list['CardNode'] = list()
self.value: int = value
self.level: int = None
self.is_leaf: bool = False
def update_level(self, level: int):
self.level = level
def add_card(self, child):
self.children.append(copy.deepcopy(child))
self.is_leaf = False
def print_card(self):
print('Card: ' + str(self.value))
def get_paths_sizes(self) -> list[int]:
paths = []
self._dfs(self, [], paths)
return list(map(len, paths))
def get_paths_to_leaves(self) -> list[list[int]]:
paths = []
self._dfs(self, [], paths)
return paths
def _dfs(self, node, path, paths):
if node is None:
return
path.append(node.value)
if not node.children:
paths.append(list(path))
else:
for child in node.children:
self._dfs(child, path, paths)
path.pop()