0
a = ['a','b','c','d']
res = []
def combinations(i,comb):
    if i>=len(a):
        res.append(comb.copy())
        return
    res.append(comb.copy())
    comb.append(a[i])
    combinations(i+1,comb)
    comb.pop()
combinations(0,[])
print(res)

This gives output as: [[], ['a'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'd']] what should I change to get all the possible combinations ?

Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • Is there a reason not to use `itertools.combinations` ? – match Jan 14 '22 at 13:29
  • 2
    Is there any reason to use global `a` and `res` ? makes it kindof hard to reuse – Patrick Artner Jan 14 '22 at 13:31
  • 2
    What do you mean by "all possible combinations"? Do you mean "permutations"? Do you mean a powerset => [https://docs.python.org/3/library/itertools.html#itertools-recipes](https://docs.python.org/3/library/itertools.html#itertools-recipes) – Patrick Artner Jan 14 '22 at 13:32
  • It's not at all obvious why you thought that this code would produce all combinations. Could you please explain, with words, the logic behind the algorithm you implemented? Then we can find out whether the logic of the algorithm is wrong, or the logic of the algorithm is correct but the python implementation is not faithful to that logic. – Stef Jan 14 '22 at 13:39
  • actually in this code I have got all the combinations using ' a ' , but I'm struggling what to add in this code to work for all combinations....and I cant understand If my process is wrong. Being a beginner in RECURSION & BACKTRACKING I just want to implement combinations using my own logic, But I am stuck in here @Stef – swapnil saha Jan 14 '22 at 13:46
  • Not permutations@PatrickArtner , I mean if [a,b,c] , it will return [],[a],[a,b],[a,c],[a,b,c],[b],[b,c],[c] – swapnil saha Jan 14 '22 at 13:52
  • Yes, you've correctly identified that you have all the combinations using 'a', and miss all the combinations not using 'a'. So this is what will need to be fixed. Using your own logic is good, but please describe that logic explicitly. – Stef Jan 14 '22 at 13:53
  • As a beginner in Recursion I just want to implement it myself @match – swapnil saha Jan 14 '22 at 13:53
  • when I'm calling the function I am passing index 0 (i=0) and an empty list. so as it goes into it it will append the empty list(comb) into my result array(res). and then it will append a[0] which is 'a' into the comb and will again recursively call itself with parameters as ( 1, ['a']). Then it will eventually call ( 2,['a','b']), (3,['a','b','c']) and (4,['a','b','c','d'] @Stef – swapnil saha Jan 14 '22 at 13:56
  • Why do you say that you have all combinations using 'a', when you are clearly missing out ('a', 'c') for example. Is there a specific pattern you are aiming for? If yes, then please write your final expected output. if not then I believe you are expecting an output that contains 2^4 = 16 lists including empty list. Right? – Trilokinath Modi Jan 14 '22 at 14:16
  • sorry, You are right, I am missing ('a','c'), I just want all the combinations @TrilokinathModi – swapnil saha Jan 14 '22 at 14:19
  • Okay then one part is clear, now based on comments it feels like you are aiming to develop this without any package. I will try to not use any package, but do you only want a solution that has recursion or any solution without package is good? – Trilokinath Modi Jan 14 '22 at 14:21
  • @TrilokinathModi Since I want to learn the Recursion, I am prefering the Recursion – swapnil saha Jan 14 '22 at 14:25

0 Answers0