Skip to content

added doctests for dynamicprogramming/minimum_partition #10033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions dynamic_programming/integer_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
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
Expand Down
38 changes: 33 additions & 5 deletions dynamic_programming/minimum_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


def find_min(arr: list[int]) -> int:
def find_min(numbers: list[int]) -> int:
"""
>>> find_min([1, 2, 3, 4, 5])
1
Expand All @@ -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)]

Expand All @@ -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:
Expand Down