Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second (PEP8) solution in Clear category for AMSCO Cipher by ssk8
def decode_amsco(message, key):
""" First generate key_matrix with index of the original message:"""
key = [int(x)-1 for x in list(str(key))]
key_list = [x for x in range(len(message))]
key_matrix, row = [], 0
while key_list:
key_matrix.append([])
for column in range(len(key)):
key_matrix[-1].append([])
if key_list:
key_matrix[-1][-1].append(key_list.pop(0))
if key_list and ((not row % 2 and column % 2) or
(row % 2 and not column % 2)):
key_matrix[-1][-1].append(key_list.pop(0))
row += 1
""" Apply the key and transformation to the key matrix,
effectively encoding it: """
rx = [[row[key.index(i)] for i in range(len(key))] for row in key_matrix]
t_matrix = [[rx[j][i] for j in range(len(rx))] for i in range(len(rx[0]))]
""" Turn the encoded key matrix into t_key,
a key file to map the encoded message to: """
t_key = []
[[t_key.extend(elm) for elm in col] for col in t_matrix]
""" Return the message mapped to the key and then sorted """
return ''.join(c[1] for c in sorted(zip(t_key, list(message))))
Jan. 2, 2017