Return the index where to insert item x in list a, assuming a is sorted. The return value i is such that all e in a[:i] have e <= x, and all e in a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will insert just after the rightmost x already there. Optional ar
(a, x, lo=0, hi=None, *, key=None)
| 19 | |
| 20 | |
| 21 | def bisect_right(a, x, lo=0, hi=None, *, key=None): |
| 22 | """Return the index where to insert item x in list a, assuming a is sorted. |
| 23 | |
| 24 | The return value i is such that all e in a[:i] have e <= x, and all e in |
| 25 | a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will |
| 26 | insert just after the rightmost x already there. |
| 27 | |
| 28 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 29 | slice of a to be searched. |
| 30 | |
| 31 | A custom key function can be supplied to customize the sort order. |
| 32 | """ |
| 33 | |
| 34 | if lo < 0: |
| 35 | raise ValueError('lo must be non-negative') |
| 36 | if hi is None: |
| 37 | hi = len(a) |
| 38 | # Note, the comparison uses "<" to match the |
| 39 | # __lt__() logic in list.sort() and in heapq. |
| 40 | if key is None: |
| 41 | while lo < hi: |
| 42 | mid = (lo + hi) // 2 |
| 43 | if x < a[mid]: |
| 44 | hi = mid |
| 45 | else: |
| 46 | lo = mid + 1 |
| 47 | else: |
| 48 | while lo < hi: |
| 49 | mid = (lo + hi) // 2 |
| 50 | if x < key(a[mid]): |
| 51 | hi = mid |
| 52 | else: |
| 53 | lo = mid + 1 |
| 54 | return lo |
| 55 | |
| 56 | |
| 57 | def insort_left(a, x, lo=0, hi=None, *, key=None): |
no outgoing calls
no test coverage detected
searching dependent graphs…