Looking at various Python implementations of Conway's game of life.

I came across one on rosetta using defaultdict.

http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Python

Just looking for your opinion on style would you write it like this continually 
calling range or would you use enumerate instead, or neither (something far 
better) ?

import random
from collections import defaultdict
 
printdead, printlive = '-#'
maxgenerations = 3
cellcount = 3,3
celltable = defaultdict(int, {
 (1, 2): 1,
 (1, 3): 1,
 (0, 3): 1,
 } ) # Only need to populate with the keys leading to life
 
##
## Start States
##
# blinker
u = universe = defaultdict(int)
u[(1,0)], u[(1,1)], u[(1,2)] = 1,1,1
 
for i in range(maxgenerations):
    print "\nGeneration %3i:" % ( i, )
    for row in range(cellcount[1]):
        print "  ", ''.join(str(universe[(row,col)])
                            for col in range(cellcount[0])).replace(
                                '0', printdead).replace('1', printlive)
    nextgeneration = defaultdict(int)
    for row in range(cellcount[1]):
        for col in range(cellcount[0]):
            nextgeneration[(row,col)] = celltable[
                ( universe[(row,col)],
                  -universe[(row,col)] + sum(universe[(r,c)]
                                             for r in range(row-1,row+2)
                                             for c in range(col-1, col+2) )
                ) ]
    universe = nextgeneration

Just finished watching ned batchelders talk and wondering how far I should take 
his advice.

http://nedbatchelder.com/text/iter.html

Thanks

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to