Returns the index of the first element greater than or equal to the item. :param sorted_collection: The sorted list to search. :param item: The item to find the lower bound for. :return: The index where the item can be inserted while maintaining order.
(sorted_collection: list[int], item: int)
| 272 | raise ValueError("sorted_collection must be sorted in ascending order") |
| 273 | |
| 274 | def lower_bound(sorted_collection: list[int], item: int) -> int: |
| 275 | """ |
| 276 | Returns the index of the first element greater than or equal to the item. |
| 277 | |
| 278 | :param sorted_collection: The sorted list to search. |
| 279 | :param item: The item to find the lower bound for. |
| 280 | :return: The index where the item can be inserted while maintaining order. |
| 281 | """ |
| 282 | left = 0 |
| 283 | right = len(sorted_collection) |
| 284 | while left < right: |
| 285 | midpoint = left + (right - left) // 2 |
| 286 | current_item = sorted_collection[midpoint] |
| 287 | if current_item < item: |
| 288 | left = midpoint + 1 |
| 289 | else: |
| 290 | right = midpoint |
| 291 | return left |
| 292 | |
| 293 | def upper_bound(sorted_collection: list[int], item: int) -> int: |
| 294 | """ |
no outgoing calls
no test coverage detected