Skip to content

Commit a2bfbda

Browse files
SalmanSipre-commit-ci[bot]cclauss
authored andcommitted
added doctests for dynamicprogramming/minimum_partition (TheAlgorithms#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 <[email protected]>
1 parent fc8f688 commit a2bfbda

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

Diff for: dynamic_programming/integer_partition.py

+24
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@
33
partitions into exactly k parts plus the number of partitions into at least k-1 parts.
44
Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k
55
into k parts. These two facts together are used for this algorithm.
6+
* https://en.wikipedia.org/wiki/Partition_(number_theory)
7+
* https://en.wikipedia.org/wiki/Partition_function_(number_theory)
68
"""
79

810

911
def partition(m: int) -> int:
12+
"""
13+
>>> partition(5)
14+
7
15+
>>> partition(7)
16+
15
17+
>>> partition(100)
18+
190569292
19+
>>> partition(1_000)
20+
24061467864032622473692149727991
21+
>>> partition(-7)
22+
Traceback (most recent call last):
23+
...
24+
IndexError: list index out of range
25+
>>> partition(0)
26+
Traceback (most recent call last):
27+
...
28+
IndexError: list assignment index out of range
29+
>>> partition(7.8)
30+
Traceback (most recent call last):
31+
...
32+
TypeError: 'float' object cannot be interpreted as an integer
33+
"""
1034
memo: list[list[int]] = [[0 for _ in range(m)] for _ in range(m + 1)]
1135
for i in range(m + 1):
1236
memo[i][0] = 1

Diff for: dynamic_programming/minimum_partition.py

+33-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55

6-
def find_min(arr: list[int]) -> int:
6+
def find_min(numbers: list[int]) -> int:
77
"""
88
>>> find_min([1, 2, 3, 4, 5])
99
1
@@ -15,9 +15,37 @@ def find_min(arr: list[int]) -> int:
1515
3
1616
>>> find_min([])
1717
0
18+
>>> find_min([1, 2, 3, 4])
19+
0
20+
>>> find_min([0, 0, 0, 0])
21+
0
22+
>>> find_min([-1, -5, 5, 1])
23+
0
24+
>>> find_min([-1, -5, 5, 1])
25+
0
26+
>>> find_min([9, 9, 9, 9, 9])
27+
9
28+
>>> find_min([1, 5, 10, 3])
29+
1
30+
>>> find_min([-1, 0, 1])
31+
0
32+
>>> find_min(range(10, 0, -1))
33+
1
34+
>>> find_min([-1])
35+
Traceback (most recent call last):
36+
--
37+
IndexError: list assignment index out of range
38+
>>> find_min([0, 0, 0, 1, 2, -4])
39+
Traceback (most recent call last):
40+
...
41+
IndexError: list assignment index out of range
42+
>>> find_min([-1, -5, -10, -3])
43+
Traceback (most recent call last):
44+
...
45+
IndexError: list assignment index out of range
1846
"""
19-
n = len(arr)
20-
s = sum(arr)
47+
n = len(numbers)
48+
s = sum(numbers)
2149

2250
dp = [[False for x in range(s + 1)] for y in range(n + 1)]
2351

@@ -31,8 +59,8 @@ def find_min(arr: list[int]) -> int:
3159
for j in range(1, s + 1):
3260
dp[i][j] = dp[i - 1][j]
3361

34-
if arr[i - 1] <= j:
35-
dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]]
62+
if numbers[i - 1] <= j:
63+
dp[i][j] = dp[i][j] or dp[i - 1][j - numbers[i - 1]]
3664

3765
for j in range(int(s / 2), -1, -1):
3866
if dp[n][j] is True:

0 commit comments

Comments
 (0)