11"""
2- The sum-of-subsetsproblem states that a set of non-negative integers, and a
2+ The sum-of-subsets problem states that a set of non-negative integers, and a
33value M, determine all possible subsets of the given set whose summation sum
44equal to given M.
55
66Summation of the chosen numbers must be equal to given number M and one number
77can be used only once.
88"""
99
10- from __future__ import annotations
1110
11+ def generate_sum_of_subsets_solutions (nums : list [int ], max_sum : int ) -> list [list [int ]]:
12+ """
13+ The main function. For list of numbers 'nums' find the the subsets with sum
14+ equal to 'max_sum'
15+ >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 9)
16+ [[3, 4, 2], [4, 5]]
17+ >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 3)
18+ [[3]]
19+ >>> generate_sum_of_subsets_solutions([3, 34, 4, 12, 5, 2], 1)
20+ []
21+ """
1222
13- def generate_sum_of_subsets_soln (nums : list [int ], max_sum : int ) -> list [list [int ]]:
1423 result : list [list [int ]] = []
1524 path : list [int ] = []
1625 num_index = 0
@@ -34,7 +43,9 @@ def create_state_space_tree(
3443 This algorithm follows depth-fist-search and backtracks when the node is not
3544 branchable.
3645
46+ >>> create_state_space_tree([1], 1, 0, [], [], 1)
3747 """
48+
3849 if sum (path ) > max_sum or (remaining_nums_sum + sum (path )) < max_sum :
3950 return
4051 if sum (path ) == max_sum :
@@ -51,16 +62,7 @@ def create_state_space_tree(
5162 )
5263
5364
54- """
55- remove the comment to take an input from the user
56-
57- print("Enter the elements")
58- nums = list(map(int, input().split()))
59- print("Enter max_sum sum")
60- max_sum = int(input())
65+ if __name__ == "__main__" :
66+ import doctest
6167
62- """
63- nums = [3 , 34 , 4 , 12 , 5 , 2 ]
64- max_sum = 9
65- result = generate_sum_of_subsets_soln (nums , max_sum )
66- print (* result )
68+ doctest .testmod ()
0 commit comments