Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by jani
import itertools
def count_neighbours(grid, row, col):
count = 0
size_x = len(grid[0])
size_y = len(grid)
for dx,dy in itertools.product((-1,0,1), repeat=2):
if dx==0 and dy==0: continue
if 0 <= col + dx < size_x and 0 <= row + dy < size_y:
count += grid[row + dy][col + dx]
return count
July 27, 2015