Skip to content

Commit ebf2a6c

Browse files
Adding doctests in subset_generation.py
1 parent 8c612ac commit ebf2a6c

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

dynamic_programming/subset_generation.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,51 @@
33

44
def combination_util(arr, n, r, index, data, i):
55
"""
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
6+
Generate and print all combinations of 'r' elements from the input list 'arr'.
7+
8+
Args:
9+
arr (list): The input list from which combinations are generated.
10+
n (int): The total number of elements in the input list 'arr'.
11+
r (int): The size of the combinations to be generated.
12+
index (int): The current index in the 'data' array.
13+
data (list): Temporary array to store the current combination being generated.
14+
i (int): The current index in the input list 'arr'.
15+
16+
Returns:
17+
None: This function prints the combinations but does not return a value.
18+
19+
Examples:
20+
>>> arr = [1, 2, 3, 4]
21+
>>> n = len(arr)
22+
>>> r = 2
23+
>>> data = [0] * r
24+
>>> combination_util(arr, n, r, 0, data, 0)
25+
1 2
26+
1 3
27+
1 4
28+
2 3
29+
2 4
30+
3 4
1231
"""
1332
if index == r:
1433
for j in range(r):
1534
print(data[j], end=" ")
1635
print(" ")
1736
return
18-
# When no more elements are there to put in data[]
1937
if i >= n:
2038
return
21-
# current is included, put next at next location
2239
data[index] = arr[i]
2340
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)
2741
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()
42+
3143

3244

3345
def print_combination(arr, n, r):
34-
# A temporary array to store all combination one by one
3546
data = [0] * r
36-
# Print all combination using temporary array 'data[]'
3747
combination_util(arr, n, r, 0, data, 0)
3848

3949

4050
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
51+
import doctest
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)