Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by rynant
def count_neighbours(grid, row, col):
row_min = max(0, row - 1)
row_max = min(len(grid), row + 2)
col_min = max(0, col - 1)
col_max = min(max(len(x) for x in grid), col + 2)
count = 0
for r in range(row_min, row_max):
for c in range(col_min, col_max):
if r != row or c != col:
count += grid[r][c]
return count
May 20, 2015