From dde13de591cb5af22bc263ea76f826d0f8ee1f8d Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 21:50:43 +0530 Subject: [PATCH 01/21] Changing the code to return tuple --- dynamic_programming/subset_generation.py | 52 +++++++----------------- 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 819fd8106def..cd9da7f9cab0 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,44 +1,22 @@ -# Print all subset combinations of n element in given set of r element. +def subset_combinations_dp(elements, n): + r = len(elements) + dp = [[] for _ in range(r + 1)] -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 - """ - 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() + dp[0].append(()) + for i in range(1, r + 1): + for j in range(i, 0, -1): + for prev_combination in dp[j - 1]: + dp[j].append(tuple(prev_combination) + (elements[i - 1],)) -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) + combinations = dp[n] + + return combinations 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 + elements = [10, 20, 30, 40] + n = 2 + result = subset_combinations_dp(elements, n) + print(result) \ No newline at end of file From 0a2428616011c07bd9ae645cf7e6421bf164e0c6 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 9 Oct 2023 21:52:20 +0530 Subject: [PATCH 02/21] Changing the code to return tuple --- dynamic_programming/subset_generation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index cd9da7f9cab0..f29c509d2f7a 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,3 +1,7 @@ +# return all subset combinations of n element in given set of r element. + + + def subset_combinations_dp(elements, n): r = len(elements) From 78699e66da9149370b4017f5518bea02d1830328 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 16:24:21 +0000 Subject: [PATCH 03/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index f29c509d2f7a..5d4d2792bd08 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,6 @@ # return all subset combinations of n element in given set of r element. - def subset_combinations_dp(elements, n): r = len(elements) @@ -23,4 +22,4 @@ def subset_combinations_dp(elements, n): elements = [10, 20, 30, 40] n = 2 result = subset_combinations_dp(elements, n) - print(result) \ No newline at end of file + print(result) From 2428546abe5d6a81884771c66313ed5857f87514 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Sun, 15 Oct 2023 21:13:34 +0530 Subject: [PATCH 04/21] Update dynamic_programming/subset_generation.py Co-authored-by: Christian Clauss --- 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 5d4d2792bd08..d5707d737d0f 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -19,7 +19,4 @@ def subset_combinations_dp(elements, n): if __name__ == "__main__": - elements = [10, 20, 30, 40] - n = 2 - result = subset_combinations_dp(elements, n) - print(result) + print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") From 80bd8359dee50a85649a8d535691ee64b07279e4 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 21:27:34 +0530 Subject: [PATCH 05/21] Adding doctests in subset_generation.py --- dynamic_programming/subset_generation.py | 37 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 5d4d2792bd08..607330cf7509 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,33 @@ # return all subset combinations of n element in given set of r element. -def subset_combinations_dp(elements, n): +def subset_combinations_dp(elements: list, n: int) -> list: + """ + Generate all possible combinations of n elements from the given list of elements using dynamic programming. + Args: + elements (list): The list of elements from which combinations will be generated. + n (int): The number of elements in each combination. + Returns: + list: A list of tuples, each representing a combination of n elements. + >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) + [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=1) + [(1,), (2,), (3,)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=3) + [(1, 2, 3)] + >>> subset_combinations_dp(elements=[42], n=1) + [(42,)] + >>> subset_combinations_dp(elements=[1, 2, 3, 4, 5], n=3) + [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 5), (1, 3, 5), (2, 3, 5), (1, 4, 5), (2, 4, 5), (3, 4, 5)] + >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) + [(6, 7, 8, 9)] + >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) + [()] + >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) + [(1, 'apple'), (1, 3.14), ('apple', 3.14)] + >>> subset_combinations_dp(elements=['single'], n=0) + [()] + """ r = len(elements) dp = [[] for _ in range(r + 1)] @@ -19,7 +45,8 @@ def subset_combinations_dp(elements, n): if __name__ == "__main__": - elements = [10, 20, 30, 40] - n = 2 - result = subset_combinations_dp(elements, n) - print(result) + print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") + + import doctest + + doctest.testmod() From f79c742cbcd85b04190800afd0c10ff380e2105a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 15:58:54 +0000 Subject: [PATCH 06/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 50 ++++++++++++------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 607330cf7509..97c528773f5b 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,31 +3,31 @@ def subset_combinations_dp(elements: list, n: int) -> list: """ - Generate all possible combinations of n elements from the given list of elements using dynamic programming. - Args: - elements (list): The list of elements from which combinations will be generated. - n (int): The number of elements in each combination. - Returns: - list: A list of tuples, each representing a combination of n elements. - >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) - [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=1) - [(1,), (2,), (3,)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=3) - [(1, 2, 3)] - >>> subset_combinations_dp(elements=[42], n=1) - [(42,)] - >>> subset_combinations_dp(elements=[1, 2, 3, 4, 5], n=3) - [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 5), (1, 3, 5), (2, 3, 5), (1, 4, 5), (2, 4, 5), (3, 4, 5)] - >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) - [(6, 7, 8, 9)] - >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) - [()] - >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) - [(1, 'apple'), (1, 3.14), ('apple', 3.14)] - >>> subset_combinations_dp(elements=['single'], n=0) - [()] - """ + Generate all possible combinations of n elements from the given list of elements using dynamic programming. + Args: + elements (list): The list of elements from which combinations will be generated. + n (int): The number of elements in each combination. + Returns: + list: A list of tuples, each representing a combination of n elements. + >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) + [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=1) + [(1,), (2,), (3,)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=3) + [(1, 2, 3)] + >>> subset_combinations_dp(elements=[42], n=1) + [(42,)] + >>> subset_combinations_dp(elements=[1, 2, 3, 4, 5], n=3) + [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 5), (1, 3, 5), (2, 3, 5), (1, 4, 5), (2, 4, 5), (3, 4, 5)] + >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) + [(6, 7, 8, 9)] + >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) + [()] + >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) + [(1, 'apple'), (1, 3.14), ('apple', 3.14)] + >>> subset_combinations_dp(elements=['single'], n=0) + [()] + """ r = len(elements) dp = [[] for _ in range(r + 1)] From 29f67cfe4fc52669ddf6e7753ecd4affdb9ad0b7 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 21:47:34 +0530 Subject: [PATCH 07/21] 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 607330cf7509..84ce8d726492 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,7 +3,7 @@ def subset_combinations_dp(elements: list, n: int) -> list: """ - Generate all possible combinations of n elements from the given list of elements using dynamic programming. + Compute n-element combinations from a given list using dynamic programming. Args: elements (list): The list of elements from which combinations will be generated. n (int): The number of elements in each combination. @@ -17,12 +17,12 @@ def subset_combinations_dp(elements: list, n: int) -> list: [(1, 2, 3)] >>> subset_combinations_dp(elements=[42], n=1) [(42,)] - >>> subset_combinations_dp(elements=[1, 2, 3, 4, 5], n=3) - [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 5), (1, 3, 5), (2, 3, 5), (1, 4, 5), (2, 4, 5), (3, 4, 5)] >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) [(6, 7, 8, 9)] >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) [()] + >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) + [(1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4)] >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) [(1, 'apple'), (1, 3.14), ('apple', 3.14)] >>> subset_combinations_dp(elements=['single'], n=0) From 2a9f506347b86500c03f647480e2cf465e12b2ad Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 21:48:52 +0530 Subject: [PATCH 08/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 84ce8d726492..c7c0a4243a48 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -48,5 +48,4 @@ def subset_combinations_dp(elements: list, n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest - doctest.testmod() From 3d8fe15ee27224354a8cd1e60531a392c59e5e9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 16:19:34 +0000 Subject: [PATCH 09/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 51 ++++++++++++------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index c7c0a4243a48..8810fe4ab007 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -3,31 +3,31 @@ def subset_combinations_dp(elements: list, n: int) -> list: """ - Compute n-element combinations from a given list using dynamic programming. - Args: - elements (list): The list of elements from which combinations will be generated. - n (int): The number of elements in each combination. - Returns: - list: A list of tuples, each representing a combination of n elements. - >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) - [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=1) - [(1,), (2,), (3,)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=3) - [(1, 2, 3)] - >>> subset_combinations_dp(elements=[42], n=1) - [(42,)] - >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) - [(6, 7, 8, 9)] - >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) - [()] - >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) - [(1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4)] - >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) - [(1, 'apple'), (1, 3.14), ('apple', 3.14)] - >>> subset_combinations_dp(elements=['single'], n=0) - [()] - """ + Compute n-element combinations from a given list using dynamic programming. + Args: + elements (list): The list of elements from which combinations will be generated. + n (int): The number of elements in each combination. + Returns: + list: A list of tuples, each representing a combination of n elements. + >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) + [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=1) + [(1,), (2,), (3,)] + >>> subset_combinations_dp(elements=[1, 2, 3], n=3) + [(1, 2, 3)] + >>> subset_combinations_dp(elements=[42], n=1) + [(42,)] + >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) + [(6, 7, 8, 9)] + >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) + [()] + >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) + [(1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4)] + >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) + [(1, 'apple'), (1, 3.14), ('apple', 3.14)] + >>> subset_combinations_dp(elements=['single'], n=0) + [()] + """ r = len(elements) dp = [[] for _ in range(r + 1)] @@ -48,4 +48,5 @@ def subset_combinations_dp(elements: list, n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest + doctest.testmod() From 9ea41a3dff1d959759f3d56791d58d91f96bbfba Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 21:54:56 +0530 Subject: [PATCH 10/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index c7c0a4243a48..2ff8bdd2e75b 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,5 +1,7 @@ # return all subset combinations of n element in given set of r element. +from typing import List + def subset_combinations_dp(elements: list, n: int) -> list: """ @@ -30,7 +32,7 @@ def subset_combinations_dp(elements: list, n: int) -> list: """ r = len(elements) - dp = [[] for _ in range(r + 1)] + dp: List[List[tuple]] = [[] for _ in range(r + 1)] dp[0].append(()) @@ -48,4 +50,5 @@ def subset_combinations_dp(elements: list, n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest + doctest.testmod() From 314541dd304a585693482231c62fec54a0225645 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 21:57:14 +0530 Subject: [PATCH 11/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 5436c5871cb7..c2f8f1eb1950 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,8 +1,5 @@ # return all subset combinations of n element in given set of r element. -from typing import List - - def subset_combinations_dp(elements: list, n: int) -> list: """ Compute n-element combinations from a given list using dynamic programming. @@ -32,7 +29,7 @@ def subset_combinations_dp(elements: list, n: int) -> list: """ r = len(elements) - dp: List[List[tuple]] = [[] for _ in range(r + 1)] + dp: list[list[tuple]] = [[] for _ in range(r + 1)] dp[0].append(()) @@ -50,5 +47,4 @@ def subset_combinations_dp(elements: list, n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest - doctest.testmod() From bc028344abf659a46579248f814c1f6d8e1916c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 16:28:01 +0000 Subject: [PATCH 12/21] [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, 2 insertions(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index c2f8f1eb1950..2f2f12dc39b7 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,5 +1,6 @@ # return all subset combinations of n element in given set of r element. + def subset_combinations_dp(elements: list, n: int) -> list: """ Compute n-element combinations from a given list using dynamic programming. @@ -47,4 +48,5 @@ def subset_combinations_dp(elements: list, n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest + doctest.testmod() From c455a31fb2caa50f241ab9b4cd68b54e3996cd86 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Sun, 15 Oct 2023 22:01:09 +0530 Subject: [PATCH 13/21] Update dynamic_programming/subset_generation.py Co-authored-by: Christian Clauss --- 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 2f2f12dc39b7..2d8ec2ed183b 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,7 @@ # return all subset combinations of n element in given set of r element. -def subset_combinations_dp(elements: list, n: int) -> list: +def subset_combinations_dp(elements: list[int], n: int) -> list: """ Compute n-element combinations from a given list using dynamic programming. Args: From 9088b31e60de6f927be8e182d59fe0ce62cb1865 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Sun, 15 Oct 2023 23:49:51 +0530 Subject: [PATCH 14/21] Update stock_span_problem.py --- dynamic_programming/subset_generation.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index c2f8f1eb1950..a3dec465bd14 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -9,7 +9,7 @@ def subset_combinations_dp(elements: list, n: int) -> list: Returns: list: A list of tuples, each representing a combination of n elements. >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) - [(10, 20), (10, 30), (20, 30), (10, 40), (20, 40), (30, 40)] + [(10, 20), (10, 30), (10, 40), (20, 30), (20, 40), (30, 40)] >>> subset_combinations_dp(elements=[1, 2, 3], n=1) [(1,), (2,), (3,)] >>> subset_combinations_dp(elements=[1, 2, 3], n=3) @@ -21,13 +21,15 @@ def subset_combinations_dp(elements: list, n: int) -> list: >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) [()] >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) - [(1, 2), (1, 3), (2, 3), (1, 4), (2, 4), (3, 4)] + [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) [(1, 'apple'), (1, 3.14), ('apple', 3.14)] >>> subset_combinations_dp(elements=['single'], n=0) [()] """ r = len(elements) + if n > r: + return [] dp: list[list[tuple]] = [[] for _ in range(r + 1)] @@ -38,12 +40,27 @@ def subset_combinations_dp(elements: list, n: int) -> list: for prev_combination in dp[j - 1]: dp[j].append(tuple(prev_combination) + (elements[i - 1],)) - combinations = dp[n] + element_type = type(elements[0]) + if all(isinstance(e, element_type) for e in elements): + combinations = sorted(dp[n]) + else: + combinations = dp[n] + return combinations if __name__ == "__main__": + from itertools import combinations + + for items, n in ( + ([10, 20, 30, 40], 2), ([1, 2, 3], 1), ([1, 2, 3], 3), ([42], 1), + ([6, 7, 8, 9], 4), ([10, 20, 30, 40, 50], 1), ([1, 2, 3, 4], 2), + ([1, 'apple', 3.14], 2), (['single'], 0), ([], 9) + ): + actual = subset_combinations_dp(items, n) + expected = list(combinations(items, n)) + assert actual == expected, f"items, n: {actual} != {expected}" print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest From 5aa6f60e93e179b60a234402b669911cbda0a402 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:21:03 +0000 Subject: [PATCH 15/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/subset_generation.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9371d5ce2435..7e8ee1b47e80 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -47,7 +47,6 @@ def subset_combinations_dp(elements: list[int], n: int) -> list: else: combinations = dp[n] - return combinations @@ -55,9 +54,16 @@ def subset_combinations_dp(elements: list[int], n: int) -> list: from itertools import combinations for items, n in ( - ([10, 20, 30, 40], 2), ([1, 2, 3], 1), ([1, 2, 3], 3), ([42], 1), - ([6, 7, 8, 9], 4), ([10, 20, 30, 40, 50], 1), ([1, 2, 3, 4], 2), - ([1, 'apple', 3.14], 2), (['single'], 0), ([], 9) + ([10, 20, 30, 40], 2), + ([1, 2, 3], 1), + ([1, 2, 3], 3), + ([42], 1), + ([6, 7, 8, 9], 4), + ([10, 20, 30, 40, 50], 1), + ([1, 2, 3, 4], 2), + ([1, "apple", 3.14], 2), + (["single"], 0), + ([], 9), ): actual = subset_combinations_dp(items, n) expected = list(combinations(items, n)) From 1669f130090f42004c08c2bab71ee94ceaf08bff Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 16 Oct 2023 00:03:19 +0530 Subject: [PATCH 16/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9371d5ce2435..da8c86d6d295 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,6 @@ # return all subset combinations of n element in given set of r element. - -def subset_combinations_dp(elements: list[int], n: int) -> list: +def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]]: """ Compute n-element combinations from a given list using dynamic programming. Args: @@ -24,9 +23,7 @@ def subset_combinations_dp(elements: list[int], n: int) -> list: >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) - [(1, 'apple'), (1, 3.14), ('apple', 3.14)] - >>> subset_combinations_dp(elements=['single'], n=0) - [()] + [(1, 'apple'), (1, 3.14), ('apple', 3.14)]\ """ r = len(elements) if n > r: @@ -65,5 +62,4 @@ def subset_combinations_dp(elements: list[int], n: int) -> list: print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest - doctest.testmod() From bb18a8ee8346ff1c30dddc2bdb95852bd77dbc5e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:34:11 +0000 Subject: [PATCH 17/21] [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, 2 insertions(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9f124ed84b74..8a09942831f6 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,5 +1,6 @@ # return all subset combinations of n element in given set of r element. + def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]]: """ Compute n-element combinations from a given list using dynamic programming. @@ -68,4 +69,5 @@ def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]] print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest + doctest.testmod() From 3385a1e81e5a15d2e903c679c1e358ebadb0ce17 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 16 Oct 2023 00:04:48 +0530 Subject: [PATCH 18/21] 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 9f124ed84b74..278a1fe7c2c4 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -23,7 +23,7 @@ def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]] >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) - [(1, 'apple'), (1, 3.14), ('apple', 3.14)]\ + [(1, 'apple'), (1, 3.14), ('apple', 3.14)] """ r = len(elements) if n > r: From 0116388c1591c2e5fe2be7f3afb3ea55b27eaf5a Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 16 Oct 2023 00:10:56 +0530 Subject: [PATCH 19/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 95a73b87ae52..d86ed3ce7ec0 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -49,23 +49,6 @@ def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]] if __name__ == "__main__": - from itertools import combinations - - for items, n in ( - ([10, 20, 30, 40], 2), - ([1, 2, 3], 1), - ([1, 2, 3], 3), - ([42], 1), - ([6, 7, 8, 9], 4), - ([10, 20, 30, 40, 50], 1), - ([1, 2, 3, 4], 2), - ([1, "apple", 3.14], 2), - (["single"], 0), - ([], 9), - ): - actual = subset_combinations_dp(items, n) - expected = list(combinations(items, n)) - assert actual == expected, f"items, n: {actual} != {expected}" print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") import doctest From afd8d566495bf33bf221cb7583d965ffd1ed8a96 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 15 Oct 2023 21:50:24 +0200 Subject: [PATCH 20/21] Update subset_generation.py --- dynamic_programming/subset_generation.py | 50 +++++++++++++----------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index d86ed3ce7ec0..83039ca553c2 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,4 @@ -# return all subset combinations of n element in given set of r element. - - -def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]]: +def subset_combinations(elements: list[int], n: int) -> list: """ Compute n-element combinations from a given list using dynamic programming. Args: @@ -9,22 +6,33 @@ def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]] n (int): The number of elements in each combination. Returns: list: A list of tuples, each representing a combination of n elements. - >>> subset_combinations_dp(elements=[10, 20, 30, 40], n=2) + >>> subset_combinations(elements=[10, 20, 30, 40], n=2) [(10, 20), (10, 30), (10, 40), (20, 30), (20, 40), (30, 40)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=1) + >>> subset_combinations(elements=[1, 2, 3], n=1) [(1,), (2,), (3,)] - >>> subset_combinations_dp(elements=[1, 2, 3], n=3) + >>> subset_combinations(elements=[1, 2, 3], n=3) [(1, 2, 3)] - >>> subset_combinations_dp(elements=[42], n=1) + >>> subset_combinations(elements=[42], n=1) [(42,)] - >>> subset_combinations_dp(elements=[6, 7, 8, 9], n=4) + >>> subset_combinations(elements=[6, 7, 8, 9], n=4) [(6, 7, 8, 9)] - >>> subset_combinations_dp(elements=[10, 20, 30, 40, 50], n=0) + >>> subset_combinations(elements=[10, 20, 30, 40, 50], n=0) [()] - >>> subset_combinations_dp(elements=[1, 2, 3, 4], n=2) + >>> subset_combinations(elements=[1, 2, 3, 4], n=2) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] - >>> subset_combinations_dp(elements=[1, 'apple', 3.14], n=2) + >>> subset_combinations(elements=[1, 'apple', 3.14], n=2) [(1, 'apple'), (1, 3.14), ('apple', 3.14)] + >>> subset_combinations(elements=['single'], n=0) + [()] + >>> subset_combinations(elements=[], n=9) + [] + >>> from itertools import combinations + >>> all(subset_combinations(items, n) == list(combinations(items, n)) + ... for items, n in ( + ... ([10, 20, 30, 40], 2), ([1, 2, 3], 1), ([1, 2, 3], 3), ([42], 1), + ... ([6, 7, 8, 9], 4), ([10, 20, 30, 40, 50], 1), ([1, 2, 3, 4], 2), + ... ([1, 'apple', 3.14], 2), (['single'], 0), ([], 9))) + True """ r = len(elements) if n > r: @@ -39,18 +47,14 @@ def subset_combinations_dp(elements: list[int], n: int) -> list[tuple[int, ...]] for prev_combination in dp[j - 1]: dp[j].append(tuple(prev_combination) + (elements[i - 1],)) - element_type = type(elements[0]) - if all(isinstance(e, element_type) for e in elements): - combinations = sorted(dp[n]) - else: - combinations = dp[n] - - return combinations + try: + return sorted(dp[n]) + except TypeError: + return dp[n] if __name__ == "__main__": - print(f"{subset_combinations_dp(elements=[10, 20, 30, 40], n=2) = }") - - import doctest + from doctest import testmod - doctest.testmod() + testmod() + print(f"{subset_combinations(elements=[10, 20, 30, 40], n=2) = }") From a746cd6d65edfcdbfe91db65aa7945bb65d0be29 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 15 Oct 2023 21:52:39 +0200 Subject: [PATCH 21/21] 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 83039ca553c2..1be412b9374d 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -2,10 +2,10 @@ def subset_combinations(elements: list[int], n: int) -> list: """ Compute n-element combinations from a given list using dynamic programming. Args: - elements (list): The list of elements from which combinations will be generated. - n (int): The number of elements in each combination. + elements: The list of elements from which combinations will be generated. + n: The number of elements in each combination. Returns: - list: A list of tuples, each representing a combination of n elements. + A list of tuples, each representing a combination of n elements. >>> subset_combinations(elements=[10, 20, 30, 40], n=2) [(10, 20), (10, 30), (10, 40), (20, 30), (20, 40), (30, 40)] >>> subset_combinations(elements=[1, 2, 3], n=1)