left brain and right brain permutations

pataphor pataphor at gmail.com
Sun Jun 14 21:14:55 CEST 2009


left brain:

Generate permutations by index, see previous newsgroup posts. Code not 
now available here.

They are very pragmatic and practical, can start right away, and can be 
efficiently spread over many independent computing cores.

right brain:

from itertools import izip, chain
from math import factorial

def expand(i,n):
     for _ in xrange(i+1):
         yield i+1
     for _ in xrange(n-i):
         yield i

def firstrow(n):
     for i in xrange(n+1):
         for _ in xrange(factorial(n)):
             yield i

def otherrow(x,n):
     return chain(*izip(*(expand(i,n) for i in x)))

def nextperm(L,n):
     yield firstrow(n)
     for x in L:
         yield otherrow(x,n)

def perm(n):
     L = [[0]]
     for i in xrange(1,n):
         L = nextperm(L,i)
     return izip(*L)


They are very hard to understand but can yield deep insights, once one 
gets into them they hint at hidden regularities and symmetries, maybe 
even parallel worlds. Unfortunately they are very impractical because 
they generate their future out of previous results. Until someone 
invents a quantum computer. From that point on though they leave every 
left brain piece of code behind as if that old code would stand still on 
its spot.

So, its not like I don't understand the functional mind, believe me, I 
would like nothing more than to go that way, but until quantum computing 
is there this stuff won't be able to compete.

(Background, ignore this post, it is just for future reference, comments 
are still welcome though)

P.



More information about the Python-list mailing list