0

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.

  • Could you at least label the printed arrays with `print("Name of array:", nameOfArray, sep=" ")`? As you can guess, it gets really confusing. – code Feb 15 '22 at 07:16

1 Answers1

0

You are getting your result because you are appending a list to asal_list and lists are mutable so instead of appending a new list every time, you append the same list. To get your intended result, you should append a copy of the list like:

sol_list.copy()

The code will become:

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.copy())
              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)`
Advay168
  • 607
  • 6
  • 10