I'm trying to take a 3 digit number then generate children in the following order: a. 1 is subtracted from the first digit b. 1 is added to the first digit c. 1 is subtracted from the second digit d. 1 is added to the second digit e. 1 is subtracted from the third digit f.1 is added to the third digit
Don't subtract if 0 and don't add if 9.
Here's the code I'm using to do it where p is an array of 3 digits
def generate_children(p):
children = []
i=0
for x in p:
if x != 0:
child1 = p
child1[i] = child1[i]-1
children.append(child1)
print(children)
if x != 9:
child2 = p
child2[i] = child2[i] + 1
children.append(child2)
print(children)
i+=1
return children
If I give this a parent of 345 it should return [245,445,335,355,344,346] at the end. However when I run it with the print statements it gives me below. Why does every entry in the array become the same after being appended?
[[2, 4, 5]]
[[3, 4, 5], [3, 4, 5]]
[[3, 3, 5], [3, 3, 5], [3, 3, 5]]
[[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]
[[3, 4, 4], [3, 4, 4], [3, 4, 4], [3, 4, 4], [3, 4, 4]]
[[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]