1

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: [[], [], [], [], [], []]
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
SeattleOrBayArea
  • 2,808
  • 6
  • 26
  • 38

1 Answers1

1

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?

Hao Li
  • 1,593
  • 15
  • 27
  • Thank you!!!! That worked! But what is the reason it was 6 blank [] lists in a list in without copy. Since I am very new to python, please let me know what was missing / what concept I should read up on. It seems somehow got modified and doing a copy will allow modifying a copy vs the result..(based on reading about copy).. – SeattleOrBayArea Aug 09 '18 at 06:38
  • Sorry, I search a lot, but I do not find the name of concept in English(I am poor in English). I will try harder. – Hao Li Aug 09 '18 at 06:44
  • 1
    @GauravSinha I got it. `mutable` and `immutable`. ref: https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/ – Hao Li Aug 09 '18 at 06:46
  • ok, no problem. I think it is something to do with shallow copy / deep copy - found this video https://www.youtube.com/watch?v=aSR1IPh9kBs ? – SeattleOrBayArea Aug 09 '18 at 06:47