I'm attempting to generate all n choose k combinations of a list (not checking for uniqueness) recursively by following the strategy of either include or not include an element for each recursive call. I can definitely print out the combinations but I for the life of me cannot figure out how to return the correct list in Python. Here are some attempts below:
In the above example I tried to append the result to an external list which didn't seem to work. I also tried implementing this by recursively constructing a list which is finally returned:
When I tested this out for the list [1,2,3] and k = 2, for both implementations, I kept getting back the result [[3,3],[3,3],[3,3]]. However, if I actually print out the 'current_combo' variable within the inner (current_combo_index == k) if statement, the correct combinations print out. What gives? I am misunderstanding something to do with variable scope or Python lists? Suppose that I have a 1-D list called
I want to write (or find built-in) a function called
I am not sure what (if anything) this is called in combinatorics, but it reminds me of the binomial coefficient: $${n \choose k} = \frac{n!}{k! (n-k)!}$$ which I remember being called the "n choose k" binomial coefficient. In the example I think that the formula for $n \choose k$ gives the number of configurations. It turns out that $${4 \choose 1} = 4$$ $${4 \choose 2} = 6$$ $${4 \choose 3} = 4$$ $${4 \choose 4} = 1$$ which can be seen from However, I don't just want the number of possible configurations for each k; instead, I want to actually generate the configurations themselves. Is there a simple and elegant -- or perhaps even built-in -- way to do this? Given an array of size n, generate and print all possible combinations of r elements in array. Examples: Input : arr[] = [1, 2, 3, 4], r = 2 Output : [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] Recommended: Please try your approach on {IDE} first, before moving on to the solution.This problem has existing recursive solution please refer Print all possible combinations of r elements in a given array of size n link. We will solve this problem in python using itertools.combinations() module. What does itertools.combinations() do ?It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Output: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. |