mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-04 08:50:41 +08:00
Add tests and cleanup sum_of_subsets algorithm (#12746)
* Add tests and cleanup sum_of_subsets algorithm. * Update sum_of_subsets.py * Update sum_of_subsets.py * Update sum_of_subsets.py * Update sum_of_subsets.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -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
|
value M, determine all possible subsets of the given set whose summation sum
|
||||||
equal to given M.
|
equal to given M.
|
||||||
|
|
||||||
@ -7,10 +7,20 @@ Summation of the chosen numbers must be equal to given number M and one number
|
|||||||
can be used only once.
|
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 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]]
|
||||||
|
>>> generate_sum_of_subsets_solutions(nums=[3, 34, 4, 12, 5, 2], max_sum=1)
|
||||||
|
[]
|
||||||
|
"""
|
||||||
|
|
||||||
def generate_sum_of_subsets_soln(nums: list[int], max_sum: int) -> list[list[int]]:
|
|
||||||
result: list[list[int]] = []
|
result: list[list[int]] = []
|
||||||
path: list[int] = []
|
path: list[int] = []
|
||||||
num_index = 0
|
num_index = 0
|
||||||
@ -34,7 +44,21 @@ def create_state_space_tree(
|
|||||||
This algorithm follows depth-fist-search and backtracks when the node is not
|
This algorithm follows depth-fist-search and backtracks when the node is not
|
||||||
branchable.
|
branchable.
|
||||||
|
|
||||||
|
>>> path = []
|
||||||
|
>>> result = []
|
||||||
|
>>> create_state_space_tree(
|
||||||
|
... 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:
|
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:
|
||||||
return
|
return
|
||||||
if sum(path) == max_sum:
|
if sum(path) == max_sum:
|
||||||
@ -51,16 +75,7 @@ def create_state_space_tree(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
"""
|
if __name__ == "__main__":
|
||||||
remove the comment to take an input from the user
|
import doctest
|
||||||
|
|
||||||
print("Enter the elements")
|
doctest.testmod()
|
||||||
nums = list(map(int, input().split()))
|
|
||||||
print("Enter max_sum sum")
|
|
||||||
max_sum = int(input())
|
|
||||||
|
|
||||||
"""
|
|
||||||
nums = [3, 34, 4, 12, 5, 2]
|
|
||||||
max_sum = 9
|
|
||||||
result = generate_sum_of_subsets_soln(nums, max_sum)
|
|
||||||
print(*result)
|
|
||||||
|
Reference in New Issue
Block a user