How to generate (enumerate) 2**N tuples representing all vertices of unit hypercube in N-dimensional hyperspace ?

Heiko Wundram modelnine at bit-bukket.org
Wed Jan 4 04:31:35 EST 2006


Claudio Grondi wrote:
> Heiko Wundram wrote:
>> def perm(n):
>>    return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
>>            for t in xrange(2L**n))
> 
> Isn't this kind of coding beeing the result of suffering from the
> post-pyContest illness syndrom?

I don't think what Paul Rubin posted is the sign of pyContest illness, as I
use two-level generator expressions such as this quite often in my code.
Why I didn't give this as the reponse to the OP was because it seems that
he's not that familiar with Python to be able to read this efficiently and
grap the concept behind the implementation immediately, that's why I
thought an explicit loop was in order.

But for any sufficiently advanced Python coder, I don't think this is
unreadable in the least.

--- Heiko.



More information about the Python-list mailing list