Oplossing extra

Deze pagina bevat uitvoerbare code.
def copy_board(b):
 return [r[:] for r in b]
def count_neighbors(b,r,c):
 return sum(b[i][j] for i in range(max(0,r-1),min(len(b),r+2)) for j in range(max(0,c-1),min(len(b[0]),c+2)) if (i,j)!=(r,c))
def next_cell(b,r,c):
 n=count_neighbors(b,r,c)
 return int(n==3 or (b[r][c]==1 and n==2))
def next_generation(b):
 result=copy_board(b)
 for r in range(len(b)):
  for c in range(len(b[0])): result[r][c]=next_cell(b,r,c)
 return result
bord=[[0,0,0,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,0,0,0]]
origineel=copy_board(bord)
assert count_neighbors([[1,1,0],[0,0,1],[0,0,0]],0,0)==1
assert next_generation(bord)==[[0,0,0,0,0],[0,0,0,0,0],[0,1,1,1,0],[0,0,0,0,0],[0,0,0,0,0]]
assert bord==origineel