Is it ok, if i don't get IndexError when indexes are -1?
def count_neighbours(grid, row, col):
dirs = ((-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1))
n = 0
for dir in dirs:
try:
n += grid[row+dir[0]][col+dir[1]]
except IndexError:
continue
return n
Created at: 2016/08/01 14:55; Updated at: 2016/08/01 16:03