Last active
February 18, 2019 21:54
-
-
Save szepnapot/aacdfcfbec48f737a763895d7e73ed49 to your computer and use it in GitHub Desktop.
Recover a secret string from random triplets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain | |
def get_behind_chars(c, pool): | |
behind = [] | |
for item in pool: | |
if c in item: | |
chars = item[item.index(c):] | |
[behind.append(_) for _ in chars if _ != c] | |
return list(set(behind)) | |
def get_pos(word, triplets): | |
levels = [] | |
for level in range(len(triplets[0]) + 1): | |
if not levels: | |
levels.append(get_behind_chars(word, triplets)) | |
else: | |
levels.append([c for char in levels[level - 1] for c in get_behind_chars(char, triplets)]) | |
return len(set(chain(*levels))) | |
def recoverSecret(triplets): | |
flat = set(c for triplet in triplets for c in triplet) | |
words = {word: get_pos(word, triplets) for word in flat} | |
order = sorted(words.items(), key=lambda kv: kv[1]) | |
return ''.join([tup[0] for tup in order][::-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment