python二分查找带index
def binarysearch(l, value):
def _binarysearch(_l, _value, beg, end):
mid = (end + beg) // 2
if beg == end:
return -1
if _value == _l[mid]:
return mid
elif _value < _l[mid]:
return _binarysearch(_l, _value, beg, mid)
else:
return _binarysearch(_l, _value, mid + 1, end)
return _binarysearch(l, value, 0, len(l))
l = [x * x for x in range(100) if x % 3 == 0]
print(l)
print(binarysearch(l, 900))
print(binarysearch(l, 0))
print(binarysearch(l, 9801))
输出:
10
0
33