From 1b68ef6e285a95129916a8afed6e0ebcebbea7c1 Mon Sep 17 00:00:00 2001 From: Meenaksh Singhania Date: Sat, 25 Apr 2026 18:44:07 +0530 Subject: [PATCH 1/3] feat: add binary search on answer template with example use case --- searches/binary_search_on_answers.py | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 searches/binary_search_on_answers.py diff --git a/searches/binary_search_on_answers.py b/searches/binary_search_on_answers.py new file mode 100644 index 000000000000..d83841664c1b --- /dev/null +++ b/searches/binary_search_on_answers.py @@ -0,0 +1,66 @@ +from typing import Callable + + +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 + + +# 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 + + 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) \ No newline at end of file From 708818db3331ab7df004e3a637efcd5e1112c50e Mon Sep 17 00:00:00 2001 From: Meenaksh Singhania Date: Sat, 25 Apr 2026 18:57:31 +0530 Subject: [PATCH 2/3] test: add doctests and edge case coverage for binary search on answer implementation --- searches/binary_search_on_answers.py | 56 +++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/searches/binary_search_on_answers.py b/searches/binary_search_on_answers.py index d83841664c1b..17f8ba47cfce 100644 --- a/searches/binary_search_on_answers.py +++ b/searches/binary_search_on_answers.py @@ -40,6 +40,34 @@ def binary_search_on_answer( 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 @@ -63,4 +91,30 @@ def can_ship(capacity: int) -> bool: return days_used <= days - return binary_search_on_answer(max(weights), sum(weights), can_ship) \ No newline at end of file + 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 +""" \ No newline at end of file From 4898fae76f1731965532aadd0b5ac86e0a3e470a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 25 Apr 2026 13:29:13 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/binary_search_on_answers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/searches/binary_search_on_answers.py b/searches/binary_search_on_answers.py index 17f8ba47cfce..350f730fa9eb 100644 --- a/searches/binary_search_on_answers.py +++ b/searches/binary_search_on_answers.py @@ -40,6 +40,7 @@ def binary_search_on_answer( return answer + # Doctests-1. Doctests for binary_search_on_answer """ @@ -71,6 +72,7 @@ def binary_search_on_answer( # 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. @@ -93,6 +95,7 @@ def can_ship(capacity: int) -> bool: return binary_search_on_answer(max(weights), sum(weights), can_ship) + # Doctest-2. Doctests for min_capacity_to_ship """ @@ -117,4 +120,4 @@ def can_ship(capacity: int) -> bool: >>> def condition(x): return x >= 100 >>> binary_search_on_answer(0, 50, condition) 50 -""" \ No newline at end of file +"""