Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Evita solution in Clear category for Moore Neighbourhood by webel
def count_neighbours(grid, row, col):
curr = grid[row][col]
# Where sr indicates "start of row" and mr indicates "max row", ie when, if, it'll be out of bounds
sr = row if not row else row - 1
mr = row if len(grid) is row + 1 else row + 1
# Same logic as above
sc = col if not col else col - 1
mc = col if len(grid[col]) is col + 1 else col + 1
val = 0
for i in range(sr, mr+1):
for j in range(sc, mc+1):
val += grid[i][j]
return val - curr
June 9, 2015