Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear-Moore Neighbourhood solution in Clear category for Moore Neighbourhood by dientu
import math
def count_neighbours(coordinate_tuple,y_coordinates, x_coordinates):
original_cordinate = (y_coordinates, x_coordinates)
# Get the value's point == 1, and push them to list_coordinate in coordinate format.
list_coordinate= []
for i in range(len(coordinate_tuple)):
for j in range (len(coordinate_tuple[0])):
if coordinate_tuple[i][j] == 1:
list_coordinate.insert(0,(i,j))
# Before counting chip, remove (y-coordinates, x-coordinates) out of tuple_coordinate
system_cordinate = []
for k in range (len(list_coordinate)):
if list_coordinate[k] != original_cordinate:
system_cordinate.insert(0,list_coordinate[k])
# Compare (y-coordinates, x-coordinates) with all tuples in system_cordinate
count = 0
for k in range(len(system_cordinate)):
if ((abs((system_cordinate[k][0])-original_cordinate [0]) <=1) & (abs((system_cordinate[k][1])-original_cordinate [1])<=1)):
count += 1
return count
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),), 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"
July 15, 2015
Comments: