Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by rolandp
def count_neighbours(grid, row, col):
rows = range(max(0, row-1), min(row+1, len(grid)-1)+1)
cols = range(max(0, col-1), min(col+1, len(grid[0])-1)+1)
return sum([grid[r][c] for r in rows for c in cols]) - grid[row][col]
Sept. 26, 2015