My recursive solution in Python adds and replaces all current elements in the answer list with the current list

I am trying to implement a Python solution to program the search for all subsets in an integer array. I have to return an array that contains all subsets of the integer array without duplicates and sorted.

def subsetHelper(cur_set, index, A, ans):
    if index >= len(A):
        print "appending ", cur_set
        ans.append(cur_set)
        print "ans: ", ans
        return
    # don't include current number
    subsetHelper(cur_set, index+1, A, ans)

    # include current number
    cur_set.append(A[index])
    subsetHelper(cur_set, index+1, A, ans)
    cur_set.pop()

def subsets(A):
    A.sort()
    ans = []
    cur_set = []
    # dont include current number
    subsetHelper(cur_set, 0, A, ans)
    return ans

Implementing the same logic in C ++ results in a correct return value. However, when I do this in Python, all I get is a set of empty lists at the very end, and during iteration it copies the same current list to all the elements in the list, although the listing shows that it is every time adds the correct subset Why is this happening? Here is the result:

print subsets([1,2,3])
appending  []
ans:  [[]]
appending  [3]
ans:  [[3], [3]]
appending  [2]
ans:  [[2], [2], [2]]
appending  [2, 3]
ans:  [[2, 3], [2, 3], [2, 3], [2, 3]]
appending  [1]
ans:  [[1], [1], [1], [1], [1]]
appending  [1, 3]
ans:  [[1, 3], [1, 3], [1, 3], [1, 3], [1, 3], [1, 3]]
appending  [1, 2]
ans:  [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
appending  [1, 2, 3]
ans:  [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[], [], [], [], [], [], [], []]
+4
2

: , ans.extend ans.append, , , , ans cur_set, , , , , list(cur_set), .

def subsetHelper(cur_set, index, A, ans):
    if index >= len(A):
        print "appending ", cur_set
        ans.append(list(cur_set))  # <--- here was the problem
        print "ans: ", ans
        return
    # don't include current number
    subsetHelper(cur_set, index+1, A, ans)

    # include current number
    cur_set.append(A[index])
    self.subsetHelper(cur_set, index+1, A, ans)
    cur_set.pop()

def subsets(A):
    A.sort()
    ans = []
    cur_set = []
    # dont include current number
    subsetHelper(cur_set, 0, A, ans)
    return ans

>>> subsets([1,2,3])
appending  []
ans:  [[]]
appending  [3]
ans:  [[], [3]]
appending  [2]
ans:  [[], [3], [2]]
appending  [2, 3]
ans:  [[], [3], [2], [2, 3]]
appending  [1]
ans:  [[], [3], [2], [2, 3], [1]]
appending  [1, 3]
ans:  [[], [3], [2], [2, 3], [1], [1, 3]]
appending  [1, 2]
ans:  [[], [3], [2], [2, 3], [1], [1, 3], [1, 2]]
appending  [1, 2, 3]
ans:  [[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
[[], [3], [2], [2, 3], [1], [1, 3], [1, 2], [1, 2, 3]]
>>> 
+1

, cur_set , , cur_set ans, susetHelper , , cur_set.pop() . , cur_set .

def subsetHelper(cur_set, index, A, ans):
    if index >= len(A):
        print "appending ", cur_set
        ans.append(cur_set[:])
        print "ans: ", ans
        return
    # don't include current number
    subsetHelper(cur_set, index+1, A, ans)

    # include current number
    cur_set.append(A[index])
    subsetHelper(cur_set, index+1, A, ans)
    cur_set.pop()

def subsets(A):
    A.sort()
    ans = []
    cur_set = []
    # dont include current number
    subsetHelper(cur_set, 0, A, ans)
    return ans

print subsets([12, 13])

, . , . , ( ), . .

0

Source: https://habr.com/ru/post/1649974/


All Articles