-1

ignore the code its working perfectly but the problem is when that i have the t list that i need it value for a procedure so what i did is i made a new empty list (aux) and put t values inside of it so the original list doesn't get changed but it still does for some reason this is the code

def listeDesTermes(k):
    Un = 0 
    t = [1,1]
    i=2
    while Un < k:
        Un = t[i-1] + t[i-2]
        t.append(Un)
        i=i+1
    return(t[0:len(t)-1])
#---------------
def Maximal (t) : 
    Maximal = 0
    for i in range(1,len(t)) :
        if t[i] > t[Maximal] :
            Maximal = i
    return(Maximal)
#--------------- 
def DecompsitionK(k,aux):
    s= 0
    Tdecomp = []
    while not k == s :
        maximum = Maximal (aux)
        if s + aux[maximum] <= k :
            s = s+aux[maximum]
            Tdecomp.append(aux[maximum])
            aux[maximum] = -69
        else : 
            aux[maximum] = -69
    return(Tdecomp)
#--------------- 
def conctenationT(t,Tused):
    for i in range(len(t)):
        if t[i] in Tused :
            t[i] = str(t[i])+"1"
        else:
            t[i] = str(t[i])+"0"
#---------------    
t=listeDesTermes(50)
# problem starts here 
aux = t
print(t)
Tused=(DecompsitionK(50,aux))
conctenationT(t,Tused)
print(t)

1 Answers1

1

In python, variables are saved by reference and not by value. Therefore in order to create a copy of an immutable, in your particular case - a list, you must use the method of list - copy(), or the function copy from the module copy. (copy.copy, there is also copy.deepcopy which copies sub-lists as well). You need to edit your code to:

aux = t.copy()

And it should work!

Jonathan1609
  • 1,809
  • 1
  • 5
  • 22