Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moore Neighbourhood by HeyTech
def count_neighbours(grid, row, col):
neighbours = 0
#all combinatios without (row, col)
allCombinatios= ((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))
for comp in allCombinatios:
x,y = comp
if x >= 0 and y >= 0:
try: neighbours += grid[x][y]
except: pass
return neighbours
Nov. 2, 2015
Comments: