I am trying to recursivley navigate through a 2d array jumping to a row with the index of a coloumn in which I can find a value.
# 0 1 2 3
sample_array_of_arrays = [[0, 1, 1, 0], #row 0
[0, 0, 1, 1], #row 1
[0, 0, 0, 0], #row 2
[0, 0, 0, 0]] #row 3
This means for the above example: There is a value in row0 at position 1. So I go to row1. I find a value at position 2 so I go to row 2. I dont find any values in row 2 so I terminate. I do this for all possible combinations and end up with following:
row0 -> row1 -> row2
row0 -> row1 -> row3
row0 -> row2
I have tried a variety of different recursive approaches, but I cant figure it out. This works for one combination (row0 -> row1 -> row2)
def rec_go_through_grpah(row,index):
if sum(sample_array_of_arrays[row])==0:
print("row " +str(row) + " reached dead end")
return
else:
while index <= len(sample_array_of_arrays[row]):
current_element = sample_array_of_arrays[row][index]
if current_element==0:
rec_go_through_grpah(row, index+1)
else:
print ("row "+str(row) + "->")
rec_go_through_grpah(index,0)
if __name__=="__main__":
sample_array_of_arrays = [[0, 1, 1, 0], # row 0
[0, 0, 1, 1], # row 1
[0, 0, 0, 0], # row 2
[0, 0, 0, 0]] # row 3
rec_go_through_grpah(0,0)
It is an infinite loop and the output is
row 0->
row 1->
row 2 reached dead end
row 1->
row 2 reached dead end
row 1->
row 2 reached dead end
row 1->
row 2 reached dead end
...