Skip to content
Open
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
123 changes: 123 additions & 0 deletions searches/binary_search_on_answers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from typing import Callable

Check failure on line 1 in searches/binary_search_on_answers.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP035)

searches/binary_search_on_answers.py:1:1: UP035 Import from `collections.abc` instead: `Callable` help: Import from `collections.abc`


def binary_search_on_answer(
low: int, high: int, condition: Callable[[int], bool]
) -> int:
"""
Generic Binary Search on Answer template.

Finds the minimum value in the range [low, high] that satisfies the condition.

The condition function must be monotonic:
- False False False True True True

:param low: lower bound of search space
:param high: upper bound of search space
:param condition: function that returns True if mid is a valid answer

:return: smallest value that satisfies the condition

Examples:
>>> def condition(x): return x * x >= 16
>>> binary_search_on_answer(0, 10, condition)
4

>>> def condition(x): return x >= 7
>>> binary_search_on_answer(0, 10, condition)
7
"""
answer = high

while low <= high:
mid = low + (high - low) // 2

if condition(mid):
answer = mid
high = mid - 1
else:
low = mid + 1

return answer


# Doctests-1. Doctests for binary_search_on_answer

"""
Generic Binary Search on Answer template.

Finds the minimum value in the range [low, high] that satisfies the condition.

The condition must be monotonic:
False False False True True True

Examples:
>>> def condition(x): return x * x >= 16
>>> binary_search_on_answer(0, 10, condition)
4

>>> def condition(x): return x >= 7
>>> binary_search_on_answer(0, 10, condition)
7

>>> def condition(x): return x >= 0
>>> binary_search_on_answer(-5, 5, condition)
0

>>> def condition(x): return True
>>> binary_search_on_answer(1, 5, condition)
1
"""


# Example1: minimum capacity to ship


def min_capacity_to_ship(weights: list[int], days: int) -> int:
"""
Find minimum capacity to ship packages within given days.

>>> min_capacity_to_ship([1,2,3,4,5,6,7,8,9,10], 5)
15
"""

def can_ship(capacity: int) -> bool:
days_used = 1
current = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file searches/binary_search_on_answers.py, please provide doctest for the function can_ship


for weight in weights:
if current + weight > capacity:
days_used += 1
current = 0
current += weight

return days_used <= days

return binary_search_on_answer(max(weights), sum(weights), can_ship)


# Doctest-2. Doctests for min_capacity_to_ship

"""
Find minimum capacity to ship packages within given days.

Examples:
>>> min_capacity_to_ship([1,2,3,4,5,6,7,8,9,10], 5)
15

>>> min_capacity_to_ship([3,2,2,4,1,4], 3)
6

>>> min_capacity_to_ship([1,2,3,1,1], 4)
3

>>> min_capacity_to_ship([10], 1)
10
"""

# Edge case to be handled
"""
>>> def condition(x): return x >= 100
>>> binary_search_on_answer(0, 50, condition)
50
"""
Loading