-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc_0431_nary_to_binary.py
169 lines (128 loc) · 4.79 KB
/
lc_0431_nary_to_binary.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""431. Encode N-ary Tree to Binary Tree"""
import operator
import unittest
from typing import Optional
class NAryNode:
def __init__(self, val=None, children=None):
self.val = val
self.children = children or []
def __eq__(self, other):
if not hasattr(other, "val") or not hasattr(other, "children"):
return False
if self.val != other.val:
return False
if len(self.children) != len(other.children):
return False
return all(map(operator.eq, self.children, other.children))
def __len__(self):
return 1 + sum(map(len, self.children))
def __str__(self):
parts = [str(x) for x in (self.val, *self.children)]
return parts[0] if len(parts) == 1 else f"({', '.join(parts)})"
def __repr__(self):
return f"NAryNode(val={repr(self.val)}, children={self.children})"
def tuple_to_nary(data):
match data:
case (val, *kids):
return NAryNode(val, [tuple_to_nary(kid) for kid in kids])
case val:
return NAryNode(val)
assert tuple_to_nary((4, 6)) == NAryNode(4, [NAryNode(6)])
assert tuple_to_nary((4, (6, 7))) == NAryNode(4, [NAryNode(6, [NAryNode(7)])])
assert str(tuple_to_nary((4, (6, 7)))) == str((4, (6, 7)))
assert eval(repr(tuple_to_nary(("3", "5")))) == NAryNode("3", [NAryNode("5")])
class BinaryNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __eq__(self, other):
if (
not hasattr(other, "val")
or not hasattr(other, "left")
or not hasattr(other, "right")
):
return False
if self.val != other.val:
return False
if not self.left and other.left:
return False
if self.left and not self.left == other.left:
return False
if not self.right and other.right:
return False
if self.right and not self.right == other.right:
return False
return True
def __len__(self):
return 1 + len(self.right or []) + len(self.left or [])
def __str__(self):
l = str(self.left) if self.left else ""
r = str(self.right) if self.right else ""
return f"({l} {self.val} {r})"
assert BinaryNode(5, BinaryNode(1)) == BinaryNode(5, BinaryNode(1))
assert BinaryNode(5, BinaryNode(1)) != BinaryNode(5, BinaryNode(2))
assert BinaryNode(5, BinaryNode(1)) != BinaryNode(5, None, BinaryNode(1))
def go_a_viking(vikings: [NAryNode]):
match vikings:
case []:
return None
case [kid]:
return BinaryNode(-1, to_binary(kid))
case [*brothers, left, right]:
node = BinaryNode(-2, to_binary(left), to_binary(right))
while brothers:
node = BinaryNode(node.val - 1, to_binary(brothers.pop()), node)
return node
def to_binary(node: Optional[NAryNode]):
match node and (node.children or [None]):
case [firstborn, *vikings]:
return BinaryNode(node.val, to_binary(firstborn), go_a_viking(vikings))
def to_nary(node: Optional[BinaryNode]):
if not node:
return
def gen():
ship = node
while ship:
yield ship.left
ship = ship.right
if not ship:
return
if ship.val >= -2:
assert -ship.val + (not ship.right) == 2
if ship.val == -2:
yield ship.left
yield ship.right
return
return NAryNode(node.val, node.left and list(map(to_nary, gen())))
example = tuple_to_nary([1, [3, 5, 6], 2, 4])
def debug_out(nary):
binary = to_binary(nary)
nary2 = to_nary(binary)
print("✅" if nary == nary2 else "❌", end=" ")
print(nary, binary, nary2, sep=" → ", end=" ")
print("{", len(nary), "→", len(binary), "binary nodes", "}")
class MyTestCase(unittest.TestCase):
def test_empty(_):
assert to_binary(None) is None
assert to_nary(None) is None
def test_leaf(_):
assert to_nary(BinaryNode(5)) == NAryNode(5)
assert to_binary(NAryNode(10)) == BinaryNode(10)
def test_simple(_):
for n in range(10):
simple = NAryNode(n**3, [NAryNode(i**2 + 5) for i in range(n)])
try:
assert to_nary(to_binary(simple)) == simple
except AssertionError:
debug_out(simple)
raise
def test_example(_):
assert to_nary(to_binary(example)) == example
def test_negatives(_):
for i in (-1, -2):
negative = tuple_to_nary([i, [i, [i], [i]], [i], [i]])
assert to_nary(to_binary(negative)) == negative
if __name__ == "__main__":
debug_out(example)
unittest.main()