How to generate (enumerate) 2**N tuples representing all vertices of unit hypercube in N-dimensional hyperspace ?
Claudio Grondi
claudio.grondi at freenet.de
Wed Jan 4 02:34:36 EST 2006
Heiko Wundram wrote:
> Paul Rubin wrote:
>
>>def perm(n):
>> return [tuple([(1,-1)[(t>>i)%2] for i in xrange(n)])
>> for t in xrange(2L**n)]
>
>
> or replace that with:
>
> def perm(n):
> return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
> for t in xrange(2L**n))
>
> to get a generator like in Paul's first example. Only works with Python 2.4+
> though.
>
> --- Heiko.
Isn't this kind of coding beeing the result of suffering from the
post-pyContest illness syndrom?
Or is there another reason behind writing it that way sacrificing
readability like usage of less CPU time to run the code?
Or am I alone having trouble to read this kind of code because not
experienced enough in writing Python scripts?
Claudio
More information about the Python-list
mailing list