added doctests for dynamicprogramming/minimum_partition (#10033)

* added doctests

* added doctests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add doctests to integer_partition.py

* Update minimum_partition.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
SalmanSi
2023-10-13 22:48:31 +05:00
committed by GitHub
parent 1117a50665
commit d96029e13d
2 changed files with 57 additions and 5 deletions

View File

@ -3,7 +3,7 @@ Partition a set into two subsets such that the difference of subset sums is mini
"""
def find_min(arr: list[int]) -> int:
def find_min(numbers: list[int]) -> int:
"""
>>> find_min([1, 2, 3, 4, 5])
1
@ -15,9 +15,37 @@ def find_min(arr: list[int]) -> int:
3
>>> find_min([])
0
>>> find_min([1, 2, 3, 4])
0
>>> find_min([0, 0, 0, 0])
0
>>> find_min([-1, -5, 5, 1])
0
>>> find_min([-1, -5, 5, 1])
0
>>> find_min([9, 9, 9, 9, 9])
9
>>> find_min([1, 5, 10, 3])
1
>>> find_min([-1, 0, 1])
0
>>> find_min(range(10, 0, -1))
1
>>> find_min([-1])
Traceback (most recent call last):
--
IndexError: list assignment index out of range
>>> find_min([0, 0, 0, 1, 2, -4])
Traceback (most recent call last):
...
IndexError: list assignment index out of range
>>> find_min([-1, -5, -10, -3])
Traceback (most recent call last):
...
IndexError: list assignment index out of range
"""
n = len(arr)
s = sum(arr)
n = len(numbers)
s = sum(numbers)
dp = [[False for x in range(s + 1)] for y in range(n + 1)]
@ -31,8 +59,8 @@ def find_min(arr: list[int]) -> int:
for j in range(1, s + 1):
dp[i][j] = dp[i - 1][j]
if arr[i - 1] <= j:
dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]]
if numbers[i - 1] <= j:
dp[i][j] = dp[i][j] or dp[i - 1][j - numbers[i - 1]]
for j in range(int(s / 2), -1, -1):
if dp[n][j] is True: