Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
zip_longest solution in Clear category for Scytale Encryption by Sim0000
from typing import Optional
from itertools import zip_longest
def scytale_decipher(ciphertext: str, crib: str) -> Optional[str]:
n = len(ciphertext)
result = None
for k in range(1, n + 1):
t = [ciphertext[i:i+k] for i in range(0, n, k)]
p = ''.join((map(''.join, zip_longest(*t, fillvalue=' ')))).replace(' ', '')
if crib in p:
if result is not None: return None
result = p
return result
if __name__ == "__main__":
print("Example:")
print(scytale_decipher("aaaatctwtkdn", "dawn"))
# These "asserts" are used for self-checking and not for an auto-testing
assert scytale_decipher("aaaatctwtkdn", "dawn") == "attackatdawn"
assert scytale_decipher("hdoeerlallrdow", "world") == "hellodearworld"
assert (
scytale_decipher("totetshpmeecisendysescwticsriasraytlaegphet", "sicret")
== None
), "Crib is not in plaintext"
assert (
scytale_decipher("aaaatctwtkdn", "at") == None
), "More than one possible decryptions"
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 22, 2022
Comments: