Skip to content

Commit 8af1e5b

Browse files
Add docstrings to binary search functions
Added docstrings for lower_bound and upper_bound functions.
1 parent ad1574f commit 8af1e5b

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

searches/binary_search.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,19 @@ def binary_search_with_duplicates(sorted_collection: list[int], item: int) -> li
272272
if list(sorted_collection) != sorted(sorted_collection):
273273
raise ValueError("sorted_collection must be sorted in ascending order")
274274

275-
"""find lower bounds"""
275+
276276

277277
def lower_bound(sorted_collection: list[int], item: int) -> int:
278+
"""
279+
Returns the index of the first element greater than or equal to the item.
280+
281+
Args:
282+
sorted_collection: The sorted list to search.
283+
item: The item to find the lower bound for.
284+
285+
Returns:
286+
int: The index where the item can be inserted while maintaining order.
287+
"""
278288
left = 0
279289
right = len(sorted_collection)
280290
while left < right:
@@ -286,9 +296,19 @@ def lower_bound(sorted_collection: list[int], item: int) -> int:
286296
right = midpoint
287297
return left
288298

289-
"""find upper bounds"""
299+
290300

291301
def upper_bound(sorted_collection: list[int], item: int) -> int:
302+
"""
303+
Returns the index of the first element strictly greater than the item.
304+
305+
Args:
306+
sorted_collection: The sorted list to search.
307+
item: The item to find the upper bound for.
308+
309+
Returns:
310+
int: The index where the item can be inserted after all existing instances.
311+
"""
292312
left = 0
293313
right = len(sorted_collection)
294314
while left < right:

0 commit comments

Comments
 (0)