Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9 lines total solution in Creative category for ADFGVX Cipher by CDG.Axel
def encode(message: str, secret_alphabet: str, keyword: str):
kw = list(dict.fromkeys(keyword))
cipher = ['ADFGVX'[i] for m in message.lower() for i in divmod(secret_alphabet.find(m), 6) if m.isalnum()]
return ''.join(m for k in sorted(kw) for i, m in enumerate(cipher) if i % len(kw) == kw.index(k))
def decode(message, secret_alphabet, keyword):
kw, itr = list(dict.fromkeys(keyword)), iter(range(lm := len(message)))
index_map = {j: next(itr) for i in map(kw.index, sorted(kw)) for j in range(i, lm, len(kw))}
itr = iter('ADFGVX'.find(message[index_map[i]]) for i in index_map.values())
return ''.join(secret_alphabet[next(itr)*6 + next(itr)] for _ in range(lm//2))
"""
# not too many iterators alternative
def decode(message, secret_alphabet, keyword):
kw, itr = list(dict.fromkeys(keyword)), iter(range(len(message)))
index_map = {j: next(itr) for i in map(kw.index, sorted(kw)) for j in range(i, len(message), len(kw))}
cipher = ['ADFGVX'.find(message[index_map[i]]) for i in index_map.values()]
return ''.join(secret_alphabet[x*6 + y] for x, y in zip(cipher[::2], cipher[1::2]))
"""
Oct. 29, 2021
Comments: