python - Unable to do a simple cycle and list interaction to replace values on an array (list of lists) -


basically want switch every number above, below, left , right of number 1, in each iteration. (consider # number one, , ' ' number 0:

--------------------- |   |   |   |   |   | --------------------- |   |   |   |   |   | --------------------- |   |   | # |   |   | --------------------- |   |   |   |   |   | --------------------- |   |   |   |   |   | ---------------------   --------------------- |   |   |   |   |   | --------------------- |   |   | # |   |   | --------------------- |   | # | # | # |   | --------------------- |   |   | # |   |   | --------------------- |   |   |   |   |   | ---------------------  --------------------- |   |   | # |   |   | --------------------- |   | # | # | # |   | --------------------- | # | # | # | # | # | --------------------- |   | # | # | # |   | --------------------- |   |   | # |   |   | ---------------------  --------------------- |   | # | # | # |   | --------------------- | # | # | # | # | # | --------------------- | # | # | # | # | # | --------------------- | # | # | # | # | # | --------------------- |   | # | # | # |   | ---------------------  --------------------- | # | # | # | # | # | --------------------- | # | # | # | # | # | --------------------- | # | # | # | # | # | --------------------- | # | # | # | # | # | --------------------- | # | # | # | # |   | --------------------- 

everything goes until cant switch number 0 in position (4,4). cant seem find wrong, know code sloppy, doing stopping last number changing?

linhas=5  matrix =[] in range(linhas):     linha1=[]     j in range(colunas):         linha1.append(0)     matrix.append(linha1) pl=[] cont=0 matrix[2][2]=1 while len(pl)!= colunas*linhas:     cont+=1     pl=[]     print(matrix)     in range(len(matrix)):         j in range(len(matrix[i])):             if matrix[i][j]==1:                 lista=[i,j]                 pl.append(lista)      l in range(len(pl)):         i= pl[l][0]         j= pl[l][1]          #this wrong, corrected below         #if (j>=0 , j+1<=len(matrix)-1) , (i>=0 , i+1<=len(matrix)-1):             #if matrix[i][j-1]==0:                 #matrix[i][j-1]=1             #if matrix[i][j+1]==0:                 #matrix[i][j+1]=1             #if matrix[i-1][j]==0:                 #matrix[i-1][j]=1             #if matrix[i+1][j]==0:                 #matrix[i+1][j]=1 correction, remove # text , substitute this:      if j-1>=0:         if matrix[i][j-1]==0:             matrix[i][j-1]=1     if j+1<colunas:         if matrix[i][j+1]==0:             matrix[i][j+1]=1     if i-1>=0:         if matrix[i-1][j]==0:             matrix[i-1][j]=1     if i+1<linhas:         if matrix[i+1][j]==0:             matrix[i+1][j]=1 

j column index. i row index.

j >= 0 true valid column indices. (but note matrix[i][j-1] matrix[i][-1] if j 0. may not want happen...)

j+1<=len(matrix)-1 means column has on less maximum possible column index.

so (j>=0 , j+1<=len(matrix)-1) means j can not in last column.

similarly, (i>=0 , i+1<=len(matrix)-1) means i can in last row.

to flip matrix[4][4] 0 1, (i,j) must either (4,3) or (3,4) both of these tuples rejected if-condition since (4,3) in last row , (3,4) in last column.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -