This may sound very simple, I tried searching for the solution but couldn't find it hence posting the question. Appreciate the help, Thanks in advance.
I am trying to print all the subsequences of a given list/string in python using recursion. Below is the output I expect:
Given Input list : [1,3,2]
Output List : [[],[1],[3],[2],[1,2],[1,3],[3,2],[1,3,2]]
This is what I have tried:
def printSubsequences(ind,ans,l,n):
final_ans = []
if ind == n:
return ans
else:
ans.append(l[ind])
final_ans.extend(printSubsequences(ind+1,ans,l,n))
ans.pop()
final_ans.extend(printSubsequences(ind+1,ans,l,n))
return final_ans
print(printSubsequences(0,[],[1,3,2],3))
Output of above code: [1, 3, 2, 1, 3, 1, 2, 1, 3, 2, 3, 2]
The output is correct partially as It does not contain []
(empty subset), but I want it in specified format as aforementioned. I also tried append
method but that is giving Output : [[[[], []], [[], []]], [[[], []], [[], []]]]
.
I don't understand what I am doing wrong here, Can someone explain - Why append
is failing and extend
is working, and How can I get my desired output ?