对系统的bisect做了稍微的改造
"""Bisection algorithms."""
def insort(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. key (default None) defines an operation on the item before comparison.
"""
if not key:
def key(x): return x
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if key(x) < key(a[mid]):
hi = mid
else:
lo = mid+1
a.insert(lo, x)
def bisect_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(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. key (default None) defines an operation on the item before comparison.
"""
if not key:
def key(x): return x
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < key(a[mid]):
hi = mid
else:
lo = mid+1
return lo
def search(a, x, lo=0, hi=None, key=None):
if not key:
def key(x): return x
index = bisect_right(a, x, lo, hi, key)
if index == 0:
return None
else:
searched = a[index-1]
if key(searched) == x:
return searched
else:
return None