|
1 |
| -# Print all subset combinations of n element in given set of r element. |
2 |
| - |
3 |
| - |
4 |
| -def combination_util(arr, n, r, index, data, i): |
| 1 | +def subset_combinations(elements: list[int], n: int) -> list: |
5 | 2 | """
|
6 |
| - Current combination is ready to be printed, print it |
7 |
| - arr[] ---> Input Array |
8 |
| - data[] ---> Temporary array to store current combination |
9 |
| - start & end ---> Staring and Ending indexes in arr[] |
10 |
| - index ---> Current index in data[] |
11 |
| - r ---> Size of a combination to be printed |
| 3 | + Compute n-element combinations from a given list using dynamic programming. |
| 4 | + Args: |
| 5 | + elements: The list of elements from which combinations will be generated. |
| 6 | + n: The number of elements in each combination. |
| 7 | + Returns: |
| 8 | + A list of tuples, each representing a combination of n elements. |
| 9 | + >>> subset_combinations(elements=[10, 20, 30, 40], n=2) |
| 10 | + [(10, 20), (10, 30), (10, 40), (20, 30), (20, 40), (30, 40)] |
| 11 | + >>> subset_combinations(elements=[1, 2, 3], n=1) |
| 12 | + [(1,), (2,), (3,)] |
| 13 | + >>> subset_combinations(elements=[1, 2, 3], n=3) |
| 14 | + [(1, 2, 3)] |
| 15 | + >>> subset_combinations(elements=[42], n=1) |
| 16 | + [(42,)] |
| 17 | + >>> subset_combinations(elements=[6, 7, 8, 9], n=4) |
| 18 | + [(6, 7, 8, 9)] |
| 19 | + >>> subset_combinations(elements=[10, 20, 30, 40, 50], n=0) |
| 20 | + [()] |
| 21 | + >>> subset_combinations(elements=[1, 2, 3, 4], n=2) |
| 22 | + [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] |
| 23 | + >>> subset_combinations(elements=[1, 'apple', 3.14], n=2) |
| 24 | + [(1, 'apple'), (1, 3.14), ('apple', 3.14)] |
| 25 | + >>> subset_combinations(elements=['single'], n=0) |
| 26 | + [()] |
| 27 | + >>> subset_combinations(elements=[], n=9) |
| 28 | + [] |
| 29 | + >>> from itertools import combinations |
| 30 | + >>> all(subset_combinations(items, n) == list(combinations(items, n)) |
| 31 | + ... for items, n in ( |
| 32 | + ... ([10, 20, 30, 40], 2), ([1, 2, 3], 1), ([1, 2, 3], 3), ([42], 1), |
| 33 | + ... ([6, 7, 8, 9], 4), ([10, 20, 30, 40, 50], 1), ([1, 2, 3, 4], 2), |
| 34 | + ... ([1, 'apple', 3.14], 2), (['single'], 0), ([], 9))) |
| 35 | + True |
12 | 36 | """
|
13 |
| - if index == r: |
14 |
| - for j in range(r): |
15 |
| - print(data[j], end=" ") |
16 |
| - print(" ") |
17 |
| - return |
18 |
| - # When no more elements are there to put in data[] |
19 |
| - if i >= n: |
20 |
| - return |
21 |
| - # current is included, put next at next location |
22 |
| - data[index] = arr[i] |
23 |
| - combination_util(arr, n, r, index + 1, data, i + 1) |
24 |
| - # current is excluded, replace it with |
25 |
| - # next (Note that i+1 is passed, but |
26 |
| - # index is not changed) |
27 |
| - combination_util(arr, n, r, index, data, i + 1) |
28 |
| - # The main function that prints all combinations |
29 |
| - # of size r in arr[] of size n. This function |
30 |
| - # mainly uses combinationUtil() |
| 37 | + r = len(elements) |
| 38 | + if n > r: |
| 39 | + return [] |
| 40 | + |
| 41 | + dp: list[list[tuple]] = [[] for _ in range(r + 1)] |
31 | 42 |
|
| 43 | + dp[0].append(()) |
32 | 44 |
|
33 |
| -def print_combination(arr, n, r): |
34 |
| - # A temporary array to store all combination one by one |
35 |
| - data = [0] * r |
36 |
| - # Print all combination using temporary array 'data[]' |
37 |
| - combination_util(arr, n, r, 0, data, 0) |
| 45 | + for i in range(1, r + 1): |
| 46 | + for j in range(i, 0, -1): |
| 47 | + for prev_combination in dp[j - 1]: |
| 48 | + dp[j].append(tuple(prev_combination) + (elements[i - 1],)) |
| 49 | + |
| 50 | + try: |
| 51 | + return sorted(dp[n]) |
| 52 | + except TypeError: |
| 53 | + return dp[n] |
38 | 54 |
|
39 | 55 |
|
40 | 56 | if __name__ == "__main__":
|
41 |
| - # Driver code to check the function above |
42 |
| - arr = [10, 20, 30, 40, 50] |
43 |
| - print_combination(arr, len(arr), 3) |
44 |
| - # This code is contributed by Ambuj sahu |
| 57 | + from doctest import testmod |
| 58 | + |
| 59 | + testmod() |
| 60 | + print(f"{subset_combinations(elements=[10, 20, 30, 40], n=2) = }") |
0 commit comments