nested lists as arrays

Terry Reedy tjreedy at udel.edu
Mon Feb 14 18:06:24 EST 2005


<benjamin.cordes at blawrc.de> wrote in message 
news:1108402043.941832.34110 at f14g2000cwb.googlegroups.com...
>    def setRandomState(self):
>       # container for the elements to pick from
>       container = [1,2,3,4,5,6,7,8,-1]
>
>       # create elements of puzzle randomly
>       i = 0
>       j = 0
>       while i <= self.dim-1:
>           while j <= self.dim-1:
>               if len(container) > 0:
>                   randomindex = random.randint(0,len(container)-1)
>                   self.elements[j][i] = container[randomindex]
>                   del container[randomindex]
>                   j=j+1
>               else:
>                   break
>           j=0
>           i=i+1

Without reading closely, I believe that the above can generate any possible 
position.  Are you aware that half are unsolvable?  If that matters, you 
need to either find a book or site that explains the parity test for 
solvability or generate the start position from the goal position by a 
series of random moves.

Terry J. Reedy







More information about the Python-list mailing list