-
Notifications
You must be signed in to change notification settings - Fork 0
/
Moore_Neighbourhood.py
29 lines (27 loc) · 1.27 KB
/
Moore_Neighbourhood.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def count_neighbours(grid, row, col):
zxc = []
for i in [x for x in range(row-1, row+2) if (x >= 0 and x < len(grid))]:
for o in [y for y in range(col-1, col+2) if (y >= 0 and y < len(grid[0]))]:
if i == row and o == col:
continue
else:
zxc.append((i, o))
return sum([grid[a][b] for (a, b) in zxc])
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert count_neighbours(((1, 0, 0, 1, 0),
(0, 1, 0, 0, 0),
(0, 0, 1, 0, 1),
(1, 0, 0, 0, 0),
(0, 0, 1, 0, 0),), 1, 2) == 3, "1st example"
assert count_neighbours(((1, 0, 0, 1, 0),
(0, 1, 0, 0, 0),
(0, 0, 1, 0, 1),
(1, 0, 0, 0, 0),
(0, 0, 1, 0, 0),), 0, 0) == 1, "2nd example"
assert count_neighbours(((1, 1, 1),
(1, 1, 1),
(1, 1, 1),), 0, 2) == 3, "Dense corner"
assert count_neighbours(((0, 0, 0),
(0, 1, 0),
(0, 0, 0),), 1, 1) == 0, "Single"