Permutations of columns in a matrix
Question:
I had a quick question! I have a matrix that has say 8 columns and about 20 rows. Each index of the matrix is filled with some letter. I was wondering if there’s an efficient way to produce a permutation (columns) of the matrix? I’m working on a Column Transposition Cipher, and I wanted to essentially test out every column permutation (swapping entire columns) so I can solve the cipher.
Is there an efficient way of doing this using itertools in python or any other technique I’m unaware of? Your help is greatly appreciated!
I first initialize the array doing this:
LMATRIX = [['' for x in xrange(8)] for x in xrange(53)]
Then later on fill it with letters…
E.g
Before Permutation:
0 1 2 3 4 5 6 7
B C R H L M N O
J F K A B C D R
After ONE iteration of the permutation:
**1 0** 2 3 4 5 6 7
**C B** R H L M N O
**F J** K A B C D R
Thanks again!
Answers:
I haven’t played much with this solution but it seems to work for simple cases. The matrix is smaller for readability purpose. The idea is that itertools.permutations
will produce the same permutations for each row you’ll then need to zip
them together to rebuild each permuted matrix. You’ll need to generalize this code below for bigger matrixes. All read the itertools.permutations
carefully to make sure the permutations are the “same” for all potential input
In [1]: import string
In [2]: import random
In [3]: LMATRIX = [[random.choice(string.ascii_uppercase) for y in xrange(3)] for x in xrange(2)]
In [4]: def print_mat(m):
...: for row in m:
...: print row
...:
The original matrix is:
In [5]: print_mat(LMATRIX)
['V', 'E', 'E']
['G', 'X', 'T']
In [6]: from itertools import permutations
In [7]: for perm in zip(permutations(LMATRIX[0]), permutations(LMATRIX[1])):
...: print_mat(perm)
...: print "n"
...:
('V', 'E', 'E')
('G', 'X', 'T')
('V', 'E', 'E')
('G', 'T', 'X')
('E', 'V', 'E')
('X', 'G', 'T')
('E', 'E', 'V')
('X', 'T', 'G')
('E', 'V', 'E')
('T', 'G', 'X')
('E', 'E', 'V')
('T', 'X', 'G')
I had a quick question! I have a matrix that has say 8 columns and about 20 rows. Each index of the matrix is filled with some letter. I was wondering if there’s an efficient way to produce a permutation (columns) of the matrix? I’m working on a Column Transposition Cipher, and I wanted to essentially test out every column permutation (swapping entire columns) so I can solve the cipher.
Is there an efficient way of doing this using itertools in python or any other technique I’m unaware of? Your help is greatly appreciated!
I first initialize the array doing this:
LMATRIX = [['' for x in xrange(8)] for x in xrange(53)]
Then later on fill it with letters…
E.g
Before Permutation:
0 1 2 3 4 5 6 7
B C R H L M N O
J F K A B C D R
After ONE iteration of the permutation:
**1 0** 2 3 4 5 6 7
**C B** R H L M N O
**F J** K A B C D R
Thanks again!
I haven’t played much with this solution but it seems to work for simple cases. The matrix is smaller for readability purpose. The idea is that itertools.permutations
will produce the same permutations for each row you’ll then need to zip
them together to rebuild each permuted matrix. You’ll need to generalize this code below for bigger matrixes. All read the itertools.permutations
carefully to make sure the permutations are the “same” for all potential input
In [1]: import string
In [2]: import random
In [3]: LMATRIX = [[random.choice(string.ascii_uppercase) for y in xrange(3)] for x in xrange(2)]
In [4]: def print_mat(m):
...: for row in m:
...: print row
...:
The original matrix is:
In [5]: print_mat(LMATRIX)
['V', 'E', 'E']
['G', 'X', 'T']
In [6]: from itertools import permutations
In [7]: for perm in zip(permutations(LMATRIX[0]), permutations(LMATRIX[1])):
...: print_mat(perm)
...: print "n"
...:
('V', 'E', 'E')
('G', 'X', 'T')
('V', 'E', 'E')
('G', 'T', 'X')
('E', 'V', 'E')
('X', 'G', 'T')
('E', 'E', 'V')
('X', 'T', 'G')
('E', 'V', 'E')
('T', 'G', 'X')
('E', 'E', 'V')
('T', 'X', 'G')