i have never used python before, but for a school unit i think i should learn it, i have a cypher text to break, and thanks to some help on here, i have gotten a brute force attack on a transosition to work :)
what i want to try and to next is instead of just trying all the possible grid sizes, i also now want to try out all the different column variations as well
ICBKAOREMDERAEAA
which is i am a code breaker,
to crack this by hand i had to set up a 4 x 4 grid
I C B K
A O R E
M D E R
A E A A
i can achieve this with the code :
s = "ICBKAOREMDERAEAA"
for i in range(1, len(s) + 1): # range doesn't include upper bound
rows = [s[x:x+i] for x in range(0, len(s), i)]
#print('\n'.join(rows))
columns = list(zip(*rows))
print columns
but what i want to do is now, try every column combination, so for the above it would also try :
C B K I
O R E A
D E R M
E A A A
so it would move the front column to the back etc
what would i need to add to my code for this to complete this task ?
Thanks guys
Use string slicing to get the separate lists.
import math
encoded_txt = "ICBKAOREMDERAEAA"
GRIDSIZE = int(math.sqrt(len(encoded_txt)))
columns = [encoded_txt[i::GRIDSIZE] for i in range(GRIDSIZE)]
>>> columns
['IAMA', 'CODE', 'BREA', 'KERA']
Then use itertools to find all permutations
>>>import itertools
>>>for permutation in itertools.permutations(columns):
print(''.join(permutation))
IAMACODEBREAKERA
IAMACODEKERABREA
IAMABREACODEKERA
IAMABREAKERACODE
IAMAKERACODEBREA
IAMAKERABREACODE
CODEIAMABREAKERA
CODEIAMAKERABREA
CODEBREAIAMAKERA
CODEBREAKERAIAMA
CODEKERAIAMABREA
CODEKERABREAIAMA
BREAIAMACODEKERA
BREAIAMAKERACODE
BREACODEIAMAKERA
BREACODEKERAIAMA
BREAKERAIAMACODE
BREAKERACODEIAMA
KERAIAMACODEBREA
KERAIAMABREACODE
KERACODEIAMABREA
KERACODEBREAIAMA
KERABREAIAMACODE
KERABREACODEIAMA
Now if you only want to get the permutations that are still in column order, I'd write a new bit of code:
def orderedpermutations(lst):
from copy import copy # possibly deepcopy, but unnecessary for this implementation
new_list = copy(lst)
for _ in range(len(new_list)):
new_list.append(new_list.pop(0))
yield new_list
Then you can use that as a generator:
for permutation in orderedpermutations(columns):
print(permutation)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments