i got surprised by the output from the following code code:
asal_list=list()
sol_list=[]
given_list=[1,3,6,7]
target=7
def calc(given_list,i,sol_list,target):
if i<=len(given_list):
if sum(sol_list)==target:
global asal_list
print(sol_list)
print(asal_list)
asal_list.append(sol_list)
print(asal_list)
elif i<len(given_list):
for k in range(len(given_list)):
sol_list.append(given_list[k])
if sum(sol_list)<=target:
calc(given_list,k,sol_list,target)
sol_list.pop()
else:
sol_list.pop()
calc(given_list,0,sol_list,target)`
Output:
[1, 1, 1, 1, 1, 1, 1]
[]
[[1, 1, 1, 1, 1, 1, 1]]
[1, 1, 1, 1, 3]
[[1, 1, 1, 1, 3]]
[[1, 1, 1, 1, 3], [1, 1, 1, 1, 3]]
[1, 1, 1, 3, 1]
[[1, 1, 1, 3, 1], [1, 1, 1, 3, 1]]
[[1, 1, 1, 3, 1], [1, 1, 1, 3, 1], [1, 1, 1, 3, 1]]
[1, 1, 3, 1, 1]
[[1, 1, 3, 1, 1], [1, 1, 3, 1, 1], [1, 1, 3, 1, 1]]
[[1, 1, 3, 1, 1], [1, 1, 3, 1, 1], [1, 1, 3, 1, 1], [1, 1, 3, 1, 1]]
[1, 3, 1, 1, 1]
[[1, 3, 1, 1, 1], [1, 3, 1, 1, 1], [1, 3, 1, 1, 1], [1, 3, 1, 1, 1]]
[[1, 3, 1, 1, 1], [1, 3, 1, 1, 1], [1, 3, 1, 1, 1], [1, 3, 1, 1, 1], [1, 3, 1, 1, 1]]
[1, 3, 3]
[[1, 3, 3], [1, 3, 3], [1, 3, 3], [1, 3, 3], [1, 3, 3]]
[[1, 3, 3], [1, 3, 3], [1, 3, 3], [1, 3, 3], [1, 3, 3], [1, 3, 3]]
[1, 6]
[[1, 6], [1, 6], [1, 6], [1, 6], [1, 6], [1, 6]]
[[1, 6], [1, 6], [1, 6], [1, 6], [1, 6], [1, 6], [1, 6]]
[3, 1, 1, 1, 1]
[[3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1]]
[[3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1], [3, 1, 1, 1, 1]]
[3, 1, 3]
[[3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3]]
[[3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3], [3, 1, 3]]
[3, 3, 1]
[[3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1]]
[[3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1], [3, 3, 1]]
[6, 1]
[[6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1]]
[[6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1]]
[7]
[[7], [7], [7], [7], [7], [7], [7], [7], [7], [7], [7]]
[[7], [7], [7], [7], [7], [7], [7], [7], [7], [7], [7], [7]]
After running this code, i am unable to conclude how on earth asal_list has got its value added even before appending, if some body know about this type of behaviour , I hope i can get some help and i dont understand why on earth the list even after adding keyword global , is not taking values inside Thank you if you add some clarity to my confusion.