Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
My first use of zip, zip_longest and accumulate. And useful slicing. solution in Clear category for ADFGVX Cipher by Phil15
from string import ascii_lowercase, digits
from itertools import zip_longest, accumulate
def clear(text):
return ''.join(ch for ch in text.lower()
if ch in ascii_lowercase + digits)
def no_duplicate(text):
return ''.join(ch for i,ch in enumerate(text)
if ch not in text[:i])
def use_alphabet(text, alphabet, encode=True):
res = ''
if encode:
for ch in text:
#ch is at row i column j in the "adfgvx" table.
i, j = divmod(alphabet.index(ch), 6)
res += 'ADFGVX'[i] + 'ADFGVX'[j]
else:
for ch1, ch2 in zip(text[::2], text[1::2]):
#For each pair of characters, we deduce a position
#in the "adfgvx", then a letter of the alphabet.
i, j = 'ADFGVX'.index(ch1), 'ADFGVX'.index(ch2)
res += alphabet[6*i+j]
return res
def use_key(text, word, encode=True):
N = len(word)
sorted_word = ''.join(sorted(word))
if encode:
#We split the text into columns in the 'word' order.
D = {ch: text[i::N] for i,ch in enumerate(word)}
#We arrange the columns in the 'sorted word' order.
list_text = [D[ch] for ch in sorted_word]
else:
#We will split the text into len(word) columns.
#How many rows ? q full rows, and maybe one row with r elements.
q, r = divmod(len(text), len(word))
nb = {ch: q+(i
July 20, 2018