I am trying to write a code for a number of integer partitions and I have it down, checking my temporary variables my code is executing properly; however, I am encountering a strange bug where my master list (lst
) gets updated when my temporary list (temp
) gets updated. I am writing temp
into lst
, but that shouldn't change the values in lst
later if temp
gets updated.
This is my entire code (as an example n = 5
)
def partitions(n):
'''define a function which returns the number of integer partitions of n'''
flag = True
lst = []
lst.append(n)
while flag:
if isinstance(lst[len(lst)-1], list):
temp = lst[len(lst)-1]
else:
temp = [lst[len(lst)-1]]
place = len(temp) - 1
i = 2
while i > 1:
for j in reversed(temp):
if j > 1:
i = 1
temp[place] = temp[place] - 1
if place < len(temp) - 1:
temp[place+1] = temp[place+1]+1
else:
temp.append(1)
print("before changing place", lst)
place = place - 1
i = 1
if temp[0] != 1:
lst.append(temp)
else:
flag = False
print(lst)
return len(lst)
The output from this for n = 5
should be lst = [5, [4,1],[3,2],[3,1,1],[2,2,1],[2,1,1,1],[1,1,1,1,1]]
and len(lst) = 7
.
The error occurs when I am updating temp
from [4,1]
→ [3,2]
. When I update temp
it also mirrors that in lst
, even though I am NOT updating lst
in any way until I append. Here is the output from my print statements:
before changing place [5]
before changing place [5, [4, 1]]
before changing place [5, [3, 2]]
before changing place [5, [3, 1, 1], [3, 1, 1]]
before changing place [5, [2, 2, 1], [2, 2, 1]]
before changing place [5, [2, 2, 1], [2, 2, 1], [2, 2, 1]]
before changing place [5, [2, 1, 2], [2, 1, 2], [2, 1, 2]]
before changing place [5, [1, 2, 2], [1, 2, 2], [1, 2, 2]]
[5, [1, 2, 2], [1, 2, 2], [1, 2, 2]]`
I guess I am just confused about how a list should behave when you append a variable.