# Demo entry 6333202

Spartan

Submitted by anonymous on Dec 03, 2016 at 19:29
Language: Python 3. Code size: 3.0 kB.

```from math import *

Dictionnaire=['A','ANS','AU','AUSSI','AVEC', 'BIEN', 'BONJOUR', 'CE', 'CELUI', 'CES', 'CETTE', 'COMME',
'DANS', 'DE', 'DEPUIS', 'DES', 'DEUX', 'DONC', 'DU', 'ELLE', 'EN', 'ENCORE', 'EST', 'ET', 'FAIT', 'IL',
'ILS', 'JE', 'LA', 'LE', 'LES', 'LEUR', 'MAIS', 'MOINS', 'NE', 'NOUS', 'NOUS', 'ON', 'ONT', 'OU', 'PAR',
'PAS', 'PEUT', 'PLUS', 'POUR', 'QUE', 'QUI', 'SA', 'SANS', 'SE', 'SES', 'SI', 'SON', 'SONT', 'SUIS', 'SUR',
'TEMPS', 'TOUJOURS', 'TOUS', 'TOUT', 'UN', 'UNE', 'VOUS']

def Spartan(M,key):
message=M.upper()
C=['']*key
# creation of a list with as many elements as the chosen key

for i in range(key):
pointer = i
# creation of a variable which will move forward among the letters of the list

while pointer < len(message):
C[i] = C[i] + message[pointer]
pointer = pointer + key
# the following letter is 'pointer + key' further

return(''.join(C))
# allows to return the message without the quotation marks

def breakSpartanWithKey(C,key):
nb_columns = int(ceil(len(C) / float(key)))
# calculation of the number of columns: division of the nb of characters by the key

nb_rows = key
nb_empty = (nb_columns*nb_rows) - len(C)
# some cases of the table can stay empty

D=['']*nb_columns

column = 0
row = 0

for i in C:
D[column] = D[column] + i
column = column + 1
if (column == nb_columns) or (column == nb_columns - 1 and row >= nb_rows - nb_empty):
column = 0
row = row + 1
# if all the columns are full, we go to the next line

print (''.join(D))

def breakSpartan(C):
message=C.upper()
possibilities = {}
# creation of a dictionary to keep track of the tests

length=len(message)

# this loop will test all the possible key until the nb of characters in the message is divided by 2
for i in range(1, length//2):
nb_columns = int(ceil(length/float(i)))
nb_rows = i
nb_empty = (nb_columns*nb_rows) - length
decrypted_rough = [''] * nb_columns

column = 0
row = 0

for character in message:
decrypted_rough[column] = decrypted_rough[column] + character
column = column + 1

if (column == nb_columns) or (column == nb_columns - 1 and row >= nb_rows - nb_empty):
column = 0
row = row + 1

decrypted = ''.join(decrypted_rough)

prob = 0
for mot in Dictionnaire:
if mot in decrypted:
prob = prob + 1
# we check if the decrypted message contains English words

possibilities[i] = prob

max_prob = max(possibilities, key = possibilities.get)
return(breakSpartanWithKey(C,max_prob))
# return the message with the most English words

```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.