Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

While finding subsets of given array through recursion, my code is returning the wrong answer

$
0
0

I have to return the subsets of the given array, I know the logic that while recurring we have to first include elements than backtrack and then exclude elements of the given array. But while implementing in python, I don't know what is going wrong, the return list is not containing anything

class Solution:    def subsets(self, A):        def sub(i, A, lis, ans):            if i==len(A):                ans.append(lis)                return            lis.append(A[i])            sub(i+1, A, lis, ans)            lis.remove(A[i])            sub(i+1, A, lis, ans)        ans = []        lis = []        n = len(A)        sub(0, A, lis, ans)        return ans

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>