I have a Two-Dimensional array (5x5) which mainly contains zeros (0) , so
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
then it is filled with ones (1) in random positions, to later assign a score, this score depends on the neighbors that have 1 and if it has four neighbors, a point is added
[[0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
in this case the score of my array would be 1 point since the 1 is surrounded by 4 zeros , I have tried this in two ways, but I don't know if there is a way to simplify this process
First form
def puntage(lista):
lenL = len(lista)
puntos = 0
for i in range(len(lista)):
for e in range(len(lista[i])):
element = lista[i][e-1]
beforeE = lista[i][e-2]
neighborUp = lista[i-1][e]
if i !=lenL -1 listai !=0:
neigborDown = lista[i+1][e]
if neigborDown == 0 and neighborUp == 0 and element == 0 and beforeE==0:
puntos +=1
if i == lenL -1:
if neighborUp == 0 and element == 0 and beforeE==0:
puntos +=1
if i == 0:
neigborDown = lista[i+1][e]
if neigborDown == 0 and element == 0 and beforeE==0: puntos +=1
return puntos
The second way I tried was separating each part of the 2D array, separating it by 'rows' and obtaining its score that way, however I only get the score referring to its lateral views, since only one 'row' is received
Second form (2 functions)
def checkRowsNeighbors(row):
fit = 0
for i in range(len(row)):
if i == 0: continue
if i == 1:
if row[i-1] == 0: fit+=1
continue
NLeft1 = row[i-1]
NLeft2 = row[i-2]
if NLeft1 == 0 and NLeft2 == 0: fit+=1
return fit
def NewPuntaje(lista):
fit = 0
for i in range(len(lista)):
if i == 0:
nextNeighbor = lista[i+1]
check = checkRowsNeighbors(nextNeighbor)
fit+= check
continue
if i == (len(lista)-1):
beforeNeighbor = lista[i-1]
check=checkRowsNeighbors(beforeNeighbor)
fit+= check
continue
nextNeighbor = lista[i+1]
c1=checkRowsNeighbors(nextNeighbor)
fit+=c1
beforeNeighbor = lista[i-1]
c2=checkRowsNeighbors(beforeNeighbor)
fit+=c2
return fit
My question is, is there a more simplified (or different) way to get the neighbors in an array? If not, how could I improve my code?
errors in your code
First of all, the code you've put in the "first form" is wrong. You have some syntax error, like in the line:
where the condition is not a valid Boolean expression, and also uses the undefined symbol
listai
. I understand that it should be:It also contains logic errors in the following
if
, since in them you should also check if the element is nonzero and you don't. That is, for example, this:should be
With those corrections your code already produces a result of 1, which is what I understand should be the correct result.
Another version
I think the code can be greatly simplified and made more readable with a couple of "tricks":
lista[i][e]
it's zero. In that case you executecontinue
, which will skip that element and go to the next iteration. Thus, the rest of the instructions no longer have to look to see if the element is different from zero.get_vecinos()
that returns a list with the 2, 3 or 4 neighbors that have a given coordinate. To know if all the neighbors are zero, it will suffice to makesum()
the list of neighbors.With these aids the function will be as simple as this:
It only remains to write the function that returns the list of neighbors. This too can be greatly simplified with the appropriate control structure:
Test:
Departure:
1