Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.3k
Expand file tree
/
Copy pathbisect.py
More file actions
Latest commit
118 lines (93 loc) · 3.34 KB
/
Copy pathbisect.py
File metadata and controls
118 lines (93 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Bisection algorithms."""
definsort_right(a, x, lo=0, hi=None, *, key=None):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the right of the rightmost x.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
ifkeyisNone:
lo=bisect_right(a, x, lo, hi)
else:
lo=bisect_right(a, key(x), lo, hi, key=key)
a.insert(lo, x)
defbisect_right(a, x, lo=0, hi=None, *, key=None):
"""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 args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
iflo<0:
raiseValueError('lo must be non-negative')
ifhiisNone:
hi=len(a)
# Note, the comparison uses "<" to match the
# __lt__() logic in list.sort() and in heapq.
ifkeyisNone:
whilelo<hi:
mid= (lo+hi) //2
ifx<a[mid]:
hi=mid
else:
lo=mid+1
else:
whilelo<hi:
mid= (lo+hi) //2
ifx<key(a[mid]):
hi=mid
else:
lo=mid+1
returnlo
definsort_left(a, x, lo=0, hi=None, *, key=None):
"""Insert item x in list a, and keep it sorted assuming a is sorted.
If x is already in a, insert it to the left of the leftmost x.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
ifkeyisNone:
lo=bisect_left(a, x, lo, hi)
else:
lo=bisect_left(a, key(x), lo, hi, key=key)
a.insert(lo, x)
defbisect_left(a, x, lo=0, hi=None, *, key=None):
"""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 before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
iflo<0:
raiseValueError('lo must be non-negative')
ifhiisNone:
hi=len(a)
# Note, the comparison uses "<" to match the
# __lt__() logic in list.sort() and in heapq.
ifkeyisNone:
whilelo<hi:
mid= (lo+hi) //2
ifa[mid] <x:
lo=mid+1
else:
hi=mid
else:
whilelo<hi:
mid= (lo+hi) //2
ifkey(a[mid]) <x:
lo=mid+1
else:
hi=mid
returnlo
# Overwrite above definitions with a fast C implementation
try:
from_bisectimport*
exceptImportError:
pass
# Create aliases
bisect=bisect_right
insort=insort_right