Jul-08-2019, 02:42 PM
Hi Guys,
I have written a function in which I have created a local list of pair values.
In the end, I append it to a global list.
The local list is declared inside for loop.
So when, the control goes inside loop second time or third time, it is not able to reinitialize the list.
Rather it still shows the data which I inserted 1st time.
The function code is attached below.
I am also sharing output.
Note : #debug-1 ("Current : "+str(tempPath)) in second run of for loop and #debug-2 (("Final : "+str(path))) in 1st run of for loop
gives same output.
Look at the output below :
I have written a function in which I have created a local list of pair values.
In the end, I append it to a global list.
The local list is declared inside for loop.
So when, the control goes inside loop second time or third time, it is not able to reinitialize the list.
Rather it still shows the data which I inserted 1st time.
The function code is attached below.
I am also sharing output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
def shortest(maxHeight, currentRow, currHeight, allPaths) : print ( "\n\nROW : " + str (currentRow)) tempAllPaths = [] if (currHeight = = 0 ): for i in range ( len (currentRow)) : if (currentRow[i] = = 1 ): weight = abs ((i - in_pos)) + 1 alive = 1 tempP = in_pos,currHeight pos = in_pos list_of_coordinates = [] list_of_coordinates.append(tempP) inc = 1 if (i<in_pos) : inc = - 1 for j in range (in_pos,i,inc): tempP = j - 1 ,currHeight pos = j - 1 list_of_coordinates.append(tempP) print (i) print (list_of_coordinates) tempAllPaths.append((list_of_coordinates,weight,alive,pos)) currHeight + = 1 elif (currHeight> 0 and currHeight<maxHeight - 1 ): print ( "Number of Paths : " + str ( len (allPaths))) for a in range ( len (allPaths)): if (allPaths[a][ 2 ] = = 1 ): print ( "Path found alive" ) for i in range ( len (currentRow)) : if (currentRow[i] = = 1 ): tempPath = [] tempPath = allPaths[a][ 0 ] print (i) path = [] path = tempPath print ( "Current : " + str (tempPath)) #print (tempPath) curr_pos = allPaths[a][ 3 ] weight = abs (i - curr_pos) + 1 alive = 1 pos = curr_pos list_of_coordinates = [] tempP = curr_pos,currHeight path.append(tempP) inc = 1 if (i<curr_pos) : inc = - 1 for j in range (curr_pos,i,inc): tempP = j - 1 ,currHeight path.append(tempP) pos = j - 1 print ( "Final : " + str (path)) tempAllPaths.append((path,weight,alive,pos)) currHeight + = 1 allPaths = tempAllPaths if (currHeight = = maxHeight - 1 ): return 1 currentRow = amap[(maxHeight - currHeight) - 1 ] shortest(maxHeight, currentRow, currHeight, allPaths) |
gives same output.
Look at the output below :
Output:ROW : [0, 0, 1, 0, 0, 0]
2
[(3, 0), (2, 0)]
ROW : [1, 1, 0, 1, 0, 0]
Number of Paths : 1
Path found alive
0
Current : [(3, 0), (2, 0)]
Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1)]
1
Current : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1)]
Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1)]
3
Current : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1)]
Final : [(3, 0), (2, 0), (2, 1), (1, 1), (0, 1), (2, 1), (1, 1), (2, 1), (1, 1)]
Could some one help me fix this ?