mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
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:
parent
1117a50665
commit
d96029e13d
|
@ -3,10 +3,34 @@ The number of partitions of a number n into at least k parts equals the number o
|
|||
partitions into exactly k parts plus the number of partitions into at least k-1 parts.
|
||||
Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k
|
||||
into k parts. These two facts together are used for this algorithm.
|
||||
* https://en.wikipedia.org/wiki/Partition_(number_theory)
|
||||
* https://en.wikipedia.org/wiki/Partition_function_(number_theory)
|
||||
"""
|
||||
|
||||
|
||||
def partition(m: int) -> int:
|
||||
"""
|
||||
>>> partition(5)
|
||||
7
|
||||
>>> partition(7)
|
||||
15
|
||||
>>> partition(100)
|
||||
190569292
|
||||
>>> partition(1_000)
|
||||
24061467864032622473692149727991
|
||||
>>> partition(-7)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: list index out of range
|
||||
>>> partition(0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: list assignment index out of range
|
||||
>>> partition(7.8)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'float' object cannot be interpreted as an integer
|
||||
"""
|
||||
memo: list[list[int]] = [[0 for _ in range(m)] for _ in range(m + 1)]
|
||||
for i in range(m + 1):
|
||||
memo[i][0] = 1
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user