Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by lancelote
def count_neighbours(grid, row, col):
# 1 2 3
# 4 + 5
# 6 7 8
cells = [(row - 1, col - 1), (row - 1, col), (row - 1, col + 1),
(row, col - 1), (row, col + 1),
(row + 1, col - 1), (row + 1, col), (row + 1, col + 1)]
result = 0
for i, j in cells:
if i >= 0 and j >= 0:
try:
result += grid[i][j]
except:
pass
return result
May 12, 2015
Comments: