From 1dfce0617e5b321179d3eb6876720d88e72a5dac Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 19:57:42 +0530 Subject: [PATCH 1/8] Adding doctest in subset_generation.py --- dynamic_programming/subset_generation.py | 44 ++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 819fd8106def..9d6f2785e81d 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,42 +3,50 @@ def combination_util(arr, n, r, index, data, i): """ - Current combination is ready to be printed, print it - arr[] ---> Input Array - data[] ---> Temporary array to store current combination - start & end ---> Staring and Ending indexes in arr[] - index ---> Current index in data[] - r ---> Size of a combination to be printed + Generate and print all combinations of 'r' elements from the input list 'arr'. + Args: + arr (list): The input list from which combinations are generated. + n (int): The total number of elements in the input list 'arr'. + r (int): The size of the combinations to be generated. + index (int): The current index in the 'data' array. + data (list): Temporary array to store the current combination being generated. + i (int): The current index in the input list 'arr'. + Returns: + None: This function prints the combinations but does not return a value. + Examples: + >>> arr = [1, 2, 3, 4] + >>> n = len(arr) + >>> r = 2 + >>> data = [0] * r + >>> combination_util(arr, n, r, 0, data, 0) + 1 2 + 1 3 + 1 4 + 2 3 + 2 4 + 3 4 """ if index == r: for j in range(r): print(data[j], end=" ") print(" ") return - # When no more elements are there to put in data[] if i >= n: return - # current is included, put next at next location data[index] = arr[i] combination_util(arr, n, r, index + 1, data, i + 1) - # current is excluded, replace it with - # next (Note that i+1 is passed, but - # index is not changed) combination_util(arr, n, r, index, data, i + 1) - # The main function that prints all combinations - # of size r in arr[] of size n. This function - # mainly uses combinationUtil() def print_combination(arr, n, r): - # A temporary array to store all combination one by one data = [0] * r - # Print all combination using temporary array 'data[]' combination_util(arr, n, r, 0, data, 0) if __name__ == "__main__": - # Driver code to check the function above arr = [10, 20, 30, 40, 50] print_combination(arr, len(arr), 3) - # This code is contributed by Ambuj sahu + + import doctest + + doctest.testmod() From 806949b500d679a5cd5ce7e5c81abe1da593b06b Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 20:08:23 +0530 Subject: [PATCH 2/8] build fail fixing --- dynamic_programming/subset_generation.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9d6f2785e81d..d8cf6fabae66 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,7 @@ # Print all subset combinations of n element in given set of r element. -def combination_util(arr, n, r, index, data, i): +def combinations(arr, n, r, index, data, i): """ Generate and print all combinations of 'r' elements from the input list 'arr'. Args: @@ -18,7 +18,7 @@ def combination_util(arr, n, r, index, data, i): >>> n = len(arr) >>> r = 2 >>> data = [0] * r - >>> combination_util(arr, n, r, 0, data, 0) + >>> combinations(arr, n, r, 0, data, 0) 1 2 1 3 1 4 @@ -34,13 +34,13 @@ def combination_util(arr, n, r, index, data, i): if i >= n: return data[index] = arr[i] - combination_util(arr, n, r, index + 1, data, i + 1) - combination_util(arr, n, r, index, data, i + 1) + combinations(arr, n, r, index + 1, data, i + 1) + combinations(arr, n, r, index, data, i + 1) def print_combination(arr, n, r): data = [0] * r - combination_util(arr, n, r, 0, data, 0) + combinations(arr, n, r, 0, data, 0) if __name__ == "__main__": From 9ca12cec12ed39b54dc00fa3ddd6e3c98d85887d Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 20:39:25 +0530 Subject: [PATCH 3/8] build fail fixing --- dynamic_programming/subset_generation.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index d8cf6fabae66..5cf59efca848 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,24 +1,23 @@ # Print all subset combinations of n element in given set of r element. -def combinations(arr, n, r, index, data, i): +def combinations(arr, n, r, index=0, data=None, i=0): """ Generate and print all combinations of 'r' elements from the input list 'arr'. Args: arr (list): The input list from which combinations are generated. n (int): The total number of elements in the input list 'arr'. r (int): The size of the combinations to be generated. - index (int): The current index in the 'data' array. - data (list): Temporary array to store the current combination being generated. - i (int): The current index in the input list 'arr'. + index (int, optional): The current index in the 'data' array. Defaults to 0. + data (list, optional): Temporary array to store the current combination being generated. Defaults to None. + i (int, optional): The current index in the input list 'arr'. Defaults to 0. Returns: None: This function prints the combinations but does not return a value. Examples: >>> arr = [1, 2, 3, 4] >>> n = len(arr) >>> r = 2 - >>> data = [0] * r - >>> combinations(arr, n, r, 0, data, 0) + >>> combinations(arr, n, r) 1 2 1 3 1 4 @@ -26,6 +25,9 @@ def combinations(arr, n, r, index, data, i): 2 4 3 4 """ + if data is None: + data = [0] * r + if index == r: for j in range(r): print(data[j], end=" ") From 1c094170bbd42e407e0a89654e7ba242c9ed9e1b Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 20:40:40 +0530 Subject: [PATCH 4/8] build fail fixing --- dynamic_programming/subset_generation.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 5cf59efca848..9c7d7e544706 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -40,15 +40,10 @@ def combinations(arr, n, r, index=0, data=None, i=0): combinations(arr, n, r, index, data, i + 1) -def print_combination(arr, n, r): - data = [0] * r - combinations(arr, n, r, 0, data, 0) - - if __name__ == "__main__": arr = [10, 20, 30, 40, 50] - print_combination(arr, len(arr), 3) - + r = 3 + combinations(arr,5,3) import doctest doctest.testmod() From f8520402e0bbe3b38c4f706f7e3ca01ec566c212 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 15:12:41 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9c7d7e544706..62e2b9097a08 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -43,7 +43,7 @@ def combinations(arr, n, r, index=0, data=None, i=0): if __name__ == "__main__": arr = [10, 20, 30, 40, 50] r = 3 - combinations(arr,5,3) + combinations(arr, 5, 3) import doctest doctest.testmod() From c3e02dc36ba814c8b57b1ec4e903bbf1c0642969 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:43:21 +0530 Subject: [PATCH 6/8] Update subset_generation.py --- dynamic_programming/subset_generation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 62e2b9097a08..0e123bd09c89 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -9,7 +9,7 @@ def combinations(arr, n, r, index=0, data=None, i=0): n (int): The total number of elements in the input list 'arr'. r (int): The size of the combinations to be generated. index (int, optional): The current index in the 'data' array. Defaults to 0. - data (list, optional): Temporary array to store the current combination being generated. Defaults to None. + data (list, optional): Temporary array to store the current combination. i (int, optional): The current index in the input list 'arr'. Defaults to 0. Returns: None: This function prints the combinations but does not return a value. From 8faa0cd0a0a04b48103c6c4fda77f639a057fe2c Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:48:38 +0530 Subject: [PATCH 7/8] Update subset_generation.py --- dynamic_programming/subset_generation.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 0e123bd09c89..7fe1506e855d 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -43,7 +43,4 @@ def combinations(arr, n, r, index=0, data=None, i=0): if __name__ == "__main__": arr = [10, 20, 30, 40, 50] r = 3 - combinations(arr, 5, 3) - import doctest - - doctest.testmod() + combinations(arr, len(arr), r) From a28e0d438a73f045b9eb937bcd229731821e7c23 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:53:30 +0530 Subject: [PATCH 8/8] Update subset_generation.py --- dynamic_programming/subset_generation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 7fe1506e855d..156a2b8da276 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -41,6 +41,6 @@ def combinations(arr, n, r, index=0, data=None, i=0): if __name__ == "__main__": - arr = [10, 20, 30, 40, 50] - r = 3 - combinations(arr, len(arr), r) + import doctest + + doctest.testmod()