I wrote a recursive function to get all the possible combinations of a given list. Below is the code.
It takes a list argument and prints all possible combinations.
def getCombinations(a):
a2 = []
ans = ['' for i in range(len(a))]
helper(a,ans,0)
def helper(a,ans,i):
if i == len(ans):
print (ans)
else:
ans[i] = ''
helper(a,ans,i+1)
ans[i] = a[i]
helper(a,ans,i+1)
So If we call getCombinations([1,2,3])
, it prints as below:
['', '', '']
['', '', 3]
['', 2, '']
['', 2, 3]
[1, '', '']
[1, '', 3]
[1, 2, '']
[1, 2, 3]
My question is how do I store the above result in a list. I have tried to look for solution, and I know the issue is function doesn't technically return anything, but even if I try replacing return
with print(...)
it doesn't work as expected and returns nothing.