Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Neighbourhood solution in Clear category for Moore Neighbourhood by Alexey_Megley
def count_neighbours(grid, row, col):
neighbors = ((-1, -1), (-1, 0), (-1, 1), (0, -1),(0, 1), (1, -1), (1, 0), (1, 1))
count = 0
for d in neighbors:
n_row = row + d[0]
n_col = col + d[1]
if 0 <= n_row < len(grid) and 0 <= n_col < len(grid[n_row]):
if grid[n_row][n_col] == 1:
count += 1
return count
June 19, 2016