MCPcopy Index your code
hub / github.com/python/cpython / bisect_right

Function bisect_right

Lib/bisect.py:21–54  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

19
20
21def 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
57def insort_left(a, x, lo=0, hi=None, *, key=None):

Callers 4

median_groupedFunction · 0.90
pdfFunction · 0.90
cdfFunction · 0.90
insort_rightFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…