Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by suic
def count_neighbours(grid, row, col):
from itertools import product
ngcs = list(product((-1, 0, 1), repeat=2))
ngcs.remove((0, 0))
cell = (row, col)
max_row, max_col = len(grid), len(grid[0])
result = sum([grid[cell[0] + ngc[0]][cell[1] + ngc[1]] for ngc in ngcs
if all((cell[0] + ngc[0] > -1,
cell[1] + ngc[1] > -1,
cell[0] + ngc[0] < max_row,
cell[1] + ngc[1] < max_col))])
return result
Oct. 1, 2014
Comments: