[Tutor] Tutor Digest, Vol 40, Issue 38

János Juhász janos.juhasz at VELUX.com
Sat Jun 16 07:24:12 CEST 2007

Hi Andy,

> The code works great, Thanks for the speedy response. The only problem
> which I can see is that the code scales very bad with the size of n.

> So, as  I want a small subsection of the data (i.e lines where there are
> only 4 1s, number in the code below) for a system where n is large(>20).
> The idea is that this  would reduce the number of iterations dramatic
> despite the individual loop taking longer to operate.(see example data).
> I've modified the bit_list_maker to allow for this but it has started to
> produce rows which are identical.
> Can anyone make any suggestion/improvements to the code

I feel that you would use this table for something else than simple print 
It is probably a decision table.
As each cell of this table can be calculated anytime, I think to not store 
it in
any big table with a lot of integers in each cell, but simple calculate it 
at need.
You can save a lot of memory in that way.
If it is a decision table, I don't mind to starting the permutation on the 
first col
instead of the last. It doesn't change the permutation itself, just its 

def Perm_matrix(row, col):
    if (row & (2**col)): return 1
    return 0

n = 4

for row in range(2**n):
    for col in range(n):
        print Perm_matrix(row, col),
    print ';'

It is easy to turn it into a class.

class Perm:
    def __init__(self, num):
        self.rownum = 2**num
        self.colnum = num

    def Perm_matrix(self, row, col):
        if (row & (2**col)): return 1
        return 0

    def __getitem__(self,(row,col)):
            return self.Perm_matrix(row,col)

m = Perm(4)

for row in range(m.rownum):
    for col in range(m.colnum):
        print m[row, col],
    print ''


More information about the Tutor mailing list