Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Moore's Neighbourhood solution in Clear category for Moore Neighbourhood by kochkinael
def count_neighbours(grid, row, col):
count = 0 # variable for counting neighbours
neighbours = ((-1, -1), (-1, 0), (-1, 1), (0, -1),(0, 1), (1, -1), (1, 0), (1, 1))
for diff in neighbours:
n_row = row + diff[0]
n_col = col + diff[1]
if 0 <= n_row < len(grid) and 0 <= n_col < len(grid[n_row]) and grid[n_row][n_col]:
count+=1
return count
Aug. 9, 2015