-
Notifications
You must be signed in to change notification settings - Fork 0
/
codewars_triplets.py
43 lines (37 loc) · 1.03 KB
/
codewars_triplets.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
from itertools import combinations
secret = "whatisup"
triplets = [
['t','u','p'],
['w','h','i'],
['t','s','u'],
['a','t','s'],
['h','a','p'],
['t','i','s'],
['w','h','s']
]
def recoverSecret(triplets):
rules = set()
secret_set = set()
for trip in triplets:
for combi in combinations(trip, 2):
rules.add(combi)
for let in trip:
secret_set.add(let)
secret = [i for i in secret_set]
alpha_order = dict.fromkeys(secret)
for char in secret:
alpha_order[char] = []
for rule in rules:
if char == rule[1]:
alpha_order[char].append(rule[0])
while True:
swap_count = 0
for i in range(0, len(secret)):
for j in range(i + 1, len(secret)):
if secret[j] in alpha_order[secret[i]]:
secret[i], secret[j] = secret[j], secret[i]
swap_count += 1
if swap_count == 0:
break
return ''.join(secret)
print(recoverSecret(triplets))