Skip to content

Commit 1dbae4a

Browse files
Add test case for binary_search with duplicates
This adds a doctest example showing that binary_search correctly handles lists with duplicate elements. Fixes #13886 The binary search algorithm correctly returns one valid index of the target element, even when there are duplicates in the sorted list.
1 parent a051ab5 commit 1dbae4a

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

searches/binary_search.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ def binary_search(sorted_collection: list[int], item: int) -> int:
197197
1
198198
>>> binary_search([0, 5, 7, 10, 15], 6)
199199
-1
200+
>>> binary_search([1, 2, 2, 2, 3], 2) # Returns one valid index of 2
201+
2
200202
"""
201203
if list(sorted_collection) != sorted(sorted_collection):
202204
raise ValueError("sorted_collection must be sorted in ascending order")

0 commit comments

Comments
 (0)