Moving a column around in python

user2292674

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

Adam Smith

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Moving a column around in python

From Dev

Python Pandas - Moving rows around

From Dev

Moving Around A Python Canvas Without Using Scrollbars

From Dev

Moving a JTextArea around a JPanel

From Dev

Moving checkpoints around in TensorFlow

From Dev

Moving a shadow around a circle

From Dev

Moving a cardboard around

From Dev

Moving a JTextArea around a JPanel

From Dev

ZFS moving drives around

From Dev

Moving a drawing around in openGL with mouse

From Dev

OpenGL moving a scene around the camera

From Dev

Drag/Moving a shape around jPanel

From Dev

Windows 7 - Moving windows around

From Dev

moving all home directories around

From Dev

Moving a canvas element around on webpage

From Dev

Picking up and moving objects around

From Dev

Turtle Mini-Project - Udacity - Python - Drawing / moving a shape around another shape

From Dev

moving every other row to a new column and group pandas python

From Dev

Moving row values contains specific string to new column in Python

From Dev

Unknow effect in XUbuntu and Ubuntu: moving the mouse, the screen is moving around

From Dev

How to stop images moving around on hover?

From Dev

Make div orbit around a moving div

From Dev

Snap SVG - Moving a loaded element around

From Dev

Moving a child around inside of it's parent

From Dev

rotate imagebutton around itself without moving it

From Dev

Moving <text> around within an svg element

From Dev

Moving gameobject in a smooth circle around the player object

From Dev

Snake-Like Moving Glow around an element

From Dev

Moving Windows folders around with Junction links (mklink)