-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding.py
127 lines (101 loc) · 3.04 KB
/
coding.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Exercise 1
def flatten_list(nested_list):
"""
Flatten an arbitrarily nested list
Parameters
----------
nested_list : nested list of int
List with item to be either integers or list
Example: [2,[[3,[4]], 5]]
Returns
-------
flat_list : list of int
A flattened list with only integers
Example: [2,3,4,5]
"""
# initialize output
flat_list = []
for item in nested_list:
# if element is list, recursively flatten and add items to output
if isinstance(item, list):
flat_list.extend(flatten_list(item))
# if element is int, append to output
elif isinstance(item, int):
flat_list.append(item)
# if any item or subitem is not list or int, raise error
else:
raise TypeError("List items must be of type int or list")
return flat_list
# Exercise 2
class Node:
"""
Each node has an integer value, and optionally has left and right children
"""
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if not isinstance(value, int):
raise ValueError("Node value must be an int")
self._value = value
@property
def left(self):
return self._left
@left.setter
def left(self, left):
if not isinstance(left, (Node, type(None))):
raise ValueError("Left child must be a Node")
self._left = left
@property
def right(self):
return self._right
@right.setter
def right(self, right):
if not isinstance(right, (Node, type(None))):
raise ValueError("Right child must be a Node")
self._right = right
def serialize_tree(root_node):
"""
Serializes a tree from top to bottom, left to right to a list of values
Parameters
----------
root_node : root node of a binary tree
The tree is not ordered or balanced, it's just a binary tree
Example:
1
/ \
2 3
/ \ / \
4 2
Returns
-------
serial_tree : A list of serialized values
Example: [1,2,3,None,4,2,None]
"""
# check type
if not isinstance(root_node, Node):
raise TypeError("root_node must be a Node object")
# initialize output
serial_tree = []
# initialize queue
queue = [root_node]
# process queue
while len(queue) > 0:
# process queue from left to right
node = queue.pop(0)
# if node is None, append to output and continue
if not node:
serial_tree.append(node)
continue
# if node is not None, append value to output
serial_tree.append(node.value)
# if node has children, append to queue from left to right
if node.left or node.right:
queue.append(node.left)
queue.append(node.right)
return serial_tree