@@ -181,37 +181,37 @@ def insort_right(
181181def binary_search (sorted_collection : list [int ], item : int ) -> int :
182182 """Pure implementation of a binary search algorithm in Python
183183
184- Be careful collection must be ascending sorted otherwise, the result will be
185- unpredictable
184+ Be careful: collection must be ascending sorted,
185+ otherwise results are unpredictable.
186186
187187 :param sorted_collection: some ascending sorted collection with comparable items
188188 :param item: item value to search
189189 :return: index of the found item or -1 if the item is not found
190190
191191 Examples:
192+ >>> binary_search([1, 2, 2, 2, 3, 4], 2) in (1, 2, 3)
193+ True
192194 >>> binary_search([0, 5, 7, 10, 15], 0)
193195 0
194- >>> binary_search([0, 5, 7, 10, 15], 15)
195- 4
196- >>> binary_search([0, 5, 7, 10, 15], 5)
197- 1
198196 >>> binary_search([0, 5, 7, 10, 15], 6)
199197 -1
200198 """
201199 if list (sorted_collection ) != sorted (sorted_collection ):
202200 raise ValueError ("sorted_collection must be sorted in ascending order" )
203- left = 0
204- right = len (sorted_collection ) - 1
201+
202+ left , right = 0 , len (sorted_collection ) - 1
205203
206204 while left <= right :
207- midpoint = left + (right - left ) // 2
208- current_item = sorted_collection [midpoint ]
209- if current_item == item :
210- return midpoint
211- elif item < current_item :
212- right = midpoint - 1
205+ mid = left + (right - left ) // 2
206+ if sorted_collection [mid ] == item :
207+ """ ✅ Handle duplicates properly
208+ Move left to ensure we can find another valid duplicate
209+ (Here we simply return the first found, which is valid)"""
210+ return mid
211+ elif sorted_collection [mid ] < item :
212+ left = mid + 1
213213 else :
214- left = midpoint + 1
214+ right = mid - 1
215215 return - 1
216216
217217
0 commit comments