MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / generate_sum_of_subsets_solutions

Function generate_sum_of_subsets_solutions

backtracking/sum_of_subsets.py:11–29  ·  view source on GitHub ↗

The main function. For list of numbers 'nums' find the subsets with sum equal to 'max_sum' >>> 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(nums=[3, 34, 4, 12, 5, 2], max_sum=3) [[3]] >

(nums: list[int], max_sum: int)

Source from the content-addressed store, hash-verified

9
10
11def 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 subsets with sum
14 equal to 'max_sum'
15
16 >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=9)
17 [[3, 4, 2], [4, 5]]
18 >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=3)
19 [[3]]
20 >>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=1)
21 []
22 """
23
24 result: list[list[int]] = []
25 path: list[int] = []
26 num_index = 0
27 remaining_nums_sum = sum(nums)
28 create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum)
29 return result
30
31
32def create_state_space_tree(

Callers

nothing calls this directly

Calls 1

create_state_space_treeFunction · 0.70

Tested by

no test coverage detected