Search code examples
pythonpython-3.xalgorithmbacktracking

python class attribute update issue


I wrote a small program to print out all permutations of an array - say input is [1,2,3]. I am using backtracking to generate the solution.

My problem is that I can see the result getting updated to the desirable value (based on logs which print class attribute result) but it is not returned. The answer returned in-fact is [[],[],[],[],[],[]] – which is what I do not understand.

class Solution:
    def __init__(self):
        self.result = []
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        c = self.permuteHelper(nums, [])
        print("finally, result:", self.result)
        return self.result 

    def permuteHelper(self, nums, chosen):
        print("permuteHelper: nums", nums, "chosen:", chosen)
        if len(nums) == 0:
            print("returning:", chosen)
            self.result.append(chosen)
            print("result:", self.result)
            return self.result 
        index = 0 
        for n in nums:
            nums.pop(index)
            chosen.append(n)

            c = self.permuteHelper(nums, chosen)

            nums.insert(index, n)
            chosen.remove(n)
            index+=1

Here is the output that I am getting:

permuteHelper: nums [1, 2, 3] chosen: []
permuteHelper: nums [2, 3] chosen: [1]
permuteHelper: nums [3] chosen: [1, 2]
permuteHelper: nums [] chosen: [1, 2, 3]
returning: [1, 2, 3]
result: [[1, 2, 3]]
permuteHelper: nums [2] chosen: [1, 3]
permuteHelper: nums [] chosen: [1, 3, 2]
returning: [1, 3, 2]
result: [[1, 3, 2], [1, 3, 2]]
permuteHelper: nums [1, 3] chosen: [2]
permuteHelper: nums [3] chosen: [2, 1]
permuteHelper: nums [] chosen: [2, 1, 3]
returning: [2, 1, 3]
result: [[2, 1, 3], [2, 1, 3], [2, 1, 3]]
permuteHelper: nums [1] chosen: [2, 3]
permuteHelper: nums [] chosen: [2, 3, 1]
returning: [2, 3, 1]
result: [[2, 3, 1], [2, 3, 1], [2, 3, 1], [2, 3, 1]]
permuteHelper: nums [1, 2] chosen: [3]
permuteHelper: nums [2] chosen: [3, 1]
permuteHelper: nums [] chosen: [3, 1, 2]
returning: [3, 1, 2]
result: [[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
permuteHelper: nums [1] chosen: [3, 2]
permuteHelper: nums [] chosen: [3, 2, 1]
returning: [3, 2, 1]
result: [[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
finally, result: [[], [], [], [], [], []]

Solution

  • The easiest way to fix(add copy):

    def permuteHelper(self, nums, chosen):
        print("permuteHelper: nums", nums, "chosen:", chosen)
        if len(nums) == 0:
            print("returning:", chosen)
            self.result.append(copy.copy(chosen))  # note!
            print("result:", self.result)
            return self.result
        index = 0
        for n in nums:
            nums.pop(index)
            chosen.append(n)
    
            c = self.permuteHelper(nums, chosen)
            nums.insert(index, n)
            chosen.remove(n)
            print("see what happen in here:", self.result)  # error happens here
            index += 1
    

    This link may helps: How do I pass a variable by reference?