Locates the first element in a sorted array that is larger or equal to a given value. It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.bisect_left . :param sorted_collection: some ascending sorted collection with comparable items :param ite
(
sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
)
| 15 | |
| 16 | |
| 17 | def bisect_left( |
| 18 | sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1 |
| 19 | ) -> int: |
| 20 | """ |
| 21 | Locates the first element in a sorted array that is larger or equal to a given |
| 22 | value. |
| 23 | |
| 24 | It has the same interface as |
| 25 | https://docs.python.org/3/library/bisect.html#bisect.bisect_left . |
| 26 | |
| 27 | :param sorted_collection: some ascending sorted collection with comparable items |
| 28 | :param item: item to bisect |
| 29 | :param lo: lowest index to consider (as in sorted_collection[lo:hi]) |
| 30 | :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) |
| 31 | :return: index i such that all values in sorted_collection[lo:i] are < item and all |
| 32 | values in sorted_collection[i:hi] are >= item. |
| 33 | |
| 34 | Examples: |
| 35 | >>> bisect_left([0, 5, 7, 10, 15], 0) |
| 36 | 0 |
| 37 | >>> bisect_left([0, 5, 7, 10, 15], 6) |
| 38 | 2 |
| 39 | >>> bisect_left([0, 5, 7, 10, 15], 20) |
| 40 | 5 |
| 41 | >>> bisect_left([0, 5, 7, 10, 15], 15, 1, 3) |
| 42 | 3 |
| 43 | >>> bisect_left([0, 5, 7, 10, 15], 6, 2) |
| 44 | 2 |
| 45 | """ |
| 46 | if hi < 0: |
| 47 | hi = len(sorted_collection) |
| 48 | |
| 49 | while lo < hi: |
| 50 | mid = lo + (hi - lo) // 2 |
| 51 | if sorted_collection[mid] < item: |
| 52 | lo = mid + 1 |
| 53 | else: |
| 54 | hi = mid |
| 55 | |
| 56 | return lo |
| 57 | |
| 58 | |
| 59 | def bisect_right( |
no outgoing calls
no test coverage detected