Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
m x n grid solution in Clear category for Moore Neighbourhood by Alexona
def count_neighbours(grid, row, col):
# grid dimensions: m x n
m = len(grid)
n = len(grid[0])
# coordinates with respect to chosen point (row,col)
neighbours = list([
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
])
count = 0
# translation to coordinates in the grid
for pair in neighbours:
x = row + pair[0]
y = col + pair[1]
# counting if (x,y) inside the grid
if m-1 >= x >= 0 and n-1 >= y >= 0:
count += grid[x][y]
return count
July 11, 2015