-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-recover_secret_triplets.py
More file actions
41 lines (36 loc) · 981 Bytes
/
4-recover_secret_triplets.py
File metadata and controls
41 lines (36 loc) · 981 Bytes
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
def add_helper(char_set, key, val):
if key not in char_set.keys():
char_set[key] = set()
char_set[key].add(val)
def recoverSecret(triplets):
fwd = {}
bwd = {}
retval = []
for triplet in triplets:
add_helper(fwd, triplet[0], triplet[1])
add_helper(fwd, triplet[1], triplet[2])
add_helper(bwd, triplet[1], triplet[0])
add_helper(bwd, triplet[2], triplet[1])
fwd_keys = set(fwd.keys())
first = (fwd_keys - set(bwd.keys())).pop()
retval.append(first)
while first in fwd_keys:
tmp_first = None
for e in fwd[first]:
bwd[e].remove(first)
if len(bwd[e]) == 0:
tmp_first = e
first = tmp_first
retval.append(first)
return ''.join(retval)
secret = "whatisup"
triplets = [
['t','u','p'],
['w','h','i'],
['t','s','u'],
['a','t','s'],
['h','a','p'],
['t','i','s'],
['w','h','s']
]
print(recoverSecret(triplets), secret)