The neighbours() Function

Viewed 30

I am working on a new question. How am I supposed to develop the a solution for the following:

The neighbors() Function

In a 2D array, each element has up to eight neighbors - the cells immediately to the north, east, west, south, northeast, northwest, southeast, and southwest of it. Of course, elements on the boundary of the array have fewer neighbors (entries at the four corners only have three neighbors). Your task is to write a function called neighbors() that takes a 2D array named input, a row index, and a column index as input and returns the number of neighbors of array[row][column] that are 1's.

For example, if the 2D input array is

array = [ [0, 0, 0, 0], [1, 1, 0, 1], [0, 0, 0, 1] ]

representing the array

0__0__0__0

1__1__0__1

0__0__0__1

then here are some examples of the function being used:

>>> neighbors(array, 1, 1)

1

>>> neighbors(array, 2, 2)

3
0 Answers
Related