Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by iv22
def count_neighbours(grid, row, col):
neighbours = ((-1, -1), (-1, 0), (-1, 1), (0, -1),
(0, 1), (1, -1), (1, 0), (1, 1))
count = 0
for n in neighbours:
n_row = row + n[0]
n_col = col + n[1]
if n_row >= 0 and n_row < len(grid) and n_col >= 0 and n_col < len(grid[n_row]):
if grid[n_row][n_col] > 0:
count += 1
return count
Dec. 22, 2015
Comments: