Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using for loop solution in Clear category for Moore Neighbourhood by c555
def count_neighbours(grid, row, col):
total = 0
r1 = max(0, row -1)
r2 = min(len(grid) -1, row +1) +1
c1 = max(0, col -1)
c2 = min(len(grid[0]) -1, col +1) +1
for r in range(r1, r2):
for c in range(c1, c2):
total += grid[r][c]
return total - grid[row][col]
Oct. 29, 2014
Comments: