-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappyLittleTree.py
62 lines (46 loc) · 1.42 KB
/
happyLittleTree.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
import math
DEBUG = 0
def decrypt(littleTree):
dims = int(math.log2(len(littleTree[0][0])))
binary_tuples = [['',] * dims]
for level in littleTree:
new_binary_tuples = []
if DEBUG:
print('level:', level)
for i in range(len(level)):
node = level[i]
binary_tuple = binary_tuples[i]
if DEBUG:
print(' current node:', node, binary_tuple)
for pos in range(len(node)):
if int(node[pos]):
new_binary_tuple = binary_tuple.copy()
# transformar posicion en binario de dims bits y agregar a la tupla
bin_pos = format(pos, f'0{dims}b')
for k in range(dims):
new_binary_tuple[k] += bin_pos[k]
new_binary_tuples.append(new_binary_tuple)
if DEBUG:
print(f' 1 found on position {pos} ({bin_pos}), built {new_binary_tuples[-1]}')
binary_tuples = new_binary_tuples
if DEBUG:
print('tuples:', binary_tuples, '\n')
# convertir binarios a enteros
pairs = []
for binary_tuple in binary_tuples:
pairs.append([int(binary, 2) for binary in binary_tuple])
return pairs
def parseTree():
littleTree = []
level = input()
# leer hasta que haya un input vacío
while level:
littleTree.append(level[level.find(':')+1:].strip().split(' '))
level = input()
return littleTree
def main():
while True:
print(decrypt(parseTree()), '\n')
return
#print('holi puh')
main()