From 137fa1092e6cd3433a8434bf82f8fa57c9a4ec72 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Wed, 14 May 2025 14:28:18 +0800 Subject: [PATCH 1/6] Add tests and cleanup sum_of_subsets algorithm. --- backtracking/sum_of_subsets.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index f34d3ca34339..5a3e96be0af7 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -1,5 +1,5 @@ """ -The sum-of-subsetsproblem states that a set of non-negative integers, and a +The sum-of-subsets problem states that a set of non-negative integers, and a value M, determine all possible subsets of the given set whose summation sum equal to given M. @@ -7,10 +7,19 @@ can be used only once. """ -from __future__ import annotations +def generate_sum_of_subsets_solutions(nums: list[int], max_sum: int) -> list[list[int]]: + """ + The main function. For list of numbers 'nums' find the the subsets with sum + equal to 'max_sum' + >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 9) + [[3, 4, 2], [4, 5]] + >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 3) + [[3]] + >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 1) + [] + """ -def generate_sum_of_subsets_soln(nums: list[int], max_sum: int) -> list[list[int]]: result: list[list[int]] = [] path: list[int] = [] num_index = 0 @@ -34,7 +43,9 @@ def create_state_space_tree( This algorithm follows depth-fist-search and backtracks when the node is not branchable. + >>> create_state_space_tree([1], 1, 0, [], [], 1) """ + if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: return if sum(path) == max_sum: @@ -51,16 +62,7 @@ def create_state_space_tree( ) -""" -remove the comment to take an input from the user - -print("Enter the elements") -nums = list(map(int, input().split())) -print("Enter max_sum sum") -max_sum = int(input()) +if __name__ == "__main__": + import doctest -""" -nums = [3, 34, 4, 12, 5, 2] -max_sum = 9 -result = generate_sum_of_subsets_soln(nums, max_sum) -print(*result) + doctest.testmod() From 91ad9c6c4d6d8140cf178d9dc6830707351267be Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 22 May 2025 23:54:52 +0300 Subject: [PATCH 2/6] Update sum_of_subsets.py --- backtracking/sum_of_subsets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index 5a3e96be0af7..bec8e21cfaa6 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -10,7 +10,7 @@ def generate_sum_of_subsets_solutions(nums: list[int], max_sum: int) -> list[list[int]]: """ - The main function. For list of numbers 'nums' find the the subsets with sum + The main function. For list of numbers 'nums' find the subsets with sum equal to 'max_sum' >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 9) [[3, 4, 2], [4, 5]] From 57bed48249b38f87e1afae8bfcdf14e79236036c Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 22 May 2025 23:59:34 +0300 Subject: [PATCH 3/6] Update sum_of_subsets.py --- backtracking/sum_of_subsets.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index bec8e21cfaa6..306a2262f748 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -12,11 +12,12 @@ def generate_sum_of_subsets_solutions(nums: list[int], max_sum: int) -> list[lis """ The main function. For list of numbers 'nums' find the subsets with sum equal to 'max_sum' - >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 9) + + >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=9) [[3, 4, 2], [4, 5]] - >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 3) + >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=3) [[3]] - >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 1) + >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=1) [] """ @@ -43,7 +44,13 @@ def create_state_space_tree( This algorithm follows depth-fist-search and backtracks when the node is not branchable. - >>> create_state_space_tree([1], 1, 0, [], [], 1) + >>> path = [] + >>> result = [] + >>> create_state_space_tree(nums=[1], max_sum=1, num_index=0, path=path, result=result, remaining_nums_sum=1) + >>> path + [] + >>> result + [] """ if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: From ecfaf991c30faa13d134c448492a05c176e0e4d9 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 23 May 2025 00:01:01 +0300 Subject: [PATCH 4/6] Update sum_of_subsets.py --- backtracking/sum_of_subsets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index 306a2262f748..427512775003 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -46,7 +46,8 @@ def create_state_space_tree( >>> path = [] >>> result = [] - >>> create_state_space_tree(nums=[1], max_sum=1, num_index=0, path=path, result=result, remaining_nums_sum=1) + >>> create_state_space_tree( + ... nums=[1], max_sum=1, num_index=0, path=path, result=result, remaining_nums_sum=1) >>> path [] >>> result From 8b54a264c8cce0ef8c4a6d51e3ada9f5ec3f1d71 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Fri, 23 May 2025 00:03:16 +0300 Subject: [PATCH 5/6] Update sum_of_subsets.py --- backtracking/sum_of_subsets.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index 427512775003..b176f96e0f4b 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -47,11 +47,16 @@ def create_state_space_tree( >>> path = [] >>> result = [] >>> create_state_space_tree( - ... nums=[1], max_sum=1, num_index=0, path=path, result=result, remaining_nums_sum=1) + ... nums=[1], + ... max_sum=1, + ... num_index=0, + ... path=path, + ... result=result, + ... remaining_nums_sum=1) >>> path [] >>> result - [] + [[1]] """ if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: From 53aa8914ab7b7046864bf9cb8747a4f541a92040 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 22 May 2025 21:03:38 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/sum_of_subsets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index b176f96e0f4b..f26f179f8725 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -47,7 +47,7 @@ def create_state_space_tree( >>> path = [] >>> result = [] >>> create_state_space_tree( - ... nums=[1], + ... nums=[1], ... max_sum=1, ... num_index=0, ... path=path,