In Python I have tried to use the code below to fill a list with occurences of zeroes in rows of a matrix. These 0 must stand before all the non zero-numbers: for example in a row like [0,0,3,0] I would count only the first two zeroes.
occurence_of_zeroes=[0]*dim
for i in range(dim):
for j in range(dim):
while matrix[i][j]==0:
occurence_of_zeroes[i]=occurence_of_zeroes[i]+1
print("In row ",i+1," there are ",occurence_of_zeroes[i]," zeroes at the beginning")
print("Is this working?")
when I try to execute the code with previously defined dim and matrix, the console never spits out the result and freezes. Why?
>Solution :
The problem is that you don’t exit the while loop to increment j. Here’s a fix to your code.
occurence_of_zeroes=[0]*dim
for i in range(dim):
for j in range(dim):
if matrix[i][j]!=0:
break
occurence_of_zeroes[i]=occurence_of_zeroes[i]+1
print("In row ",i+1," there are ",occurence_of_zeroes[i]," zeroes at the beginning")
print("Is this working?")