8 queen/game of life

Peter Immarco android at micron.net
Wed Jun 14 23:34:44 EDT 2000


Prolog's excellent for this kind of problem too.  (Sorry about the null-answer on
doing it in Python. :)

Peter.

Trent Mick wrote:

> On Thu, Jun 15, 2000 at 10:44:03AM +0800, root wrote:
> > newbie question.
> >
> > has anyone wrote the 8 non-attacking queen problem and conway's game of life
> > in python? i try but face a few newbie problems.
> >
> > 1. conway's game of life:
> >    how to have a matrix in python?
> >    the following is not good;
> >
> >            r0=[0,0,0]
> >          r1=[0,0,0]
> >          ........
> >
> >          m=[r0,r1,....]
> >
> >    the matrix is fixed, which is not good. my solution use dictionary (i
> >    know this is not the right way). the key is a tuple of the matrix
> >    indices. the value is '0'. then base on game of life rule, update the
> >    dictionary and print for next generation.
> >
> >          m={}
> >          m[(0,0)] = 0, m[(0,1)] = 1,....
>
> How about this:
>
> >>> m =[]
> >>> for i in range(5):
> ...     m.append([])
> ...     for j in range(5):
> ...             m[i].append(i*j)
> ...
> >>> m
> [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12], [0, 4,
> 8, 12, 16]]
> >>> m[0][0]
> 0
> >>> m[1][4]
> 4
> >>> m[2][4]
> 8
>
> However, if you are doing heavy linear algebra I believe you should check out
> NumPy.
>
> Trent
>
> --
> Trent Mick
> trentm at activestate.com




More information about the Python-list mailing list