标签:else imp binary *args 技术分享 结束 run 有序 arch
以下是示例代码:
def line_search(li, val):
for key, value in enumerate(li):
if value == val:
return key
else:
return None
def binary_search(li, val):
left = 0 # 最小值的下标
right = len(li)-1 # z最大值的下标
while left <= right: # 候选区有值
# mid是中间值的下标
mid = (left+right) // 2
if li[mid] == val: # 找到了就返回索引
return mid
elif li[mid] < val: # 如果中间值小于找的值,说明要找的值在右边候选区
left = mid + 1
else:
right = mid - 1
else:
return None
import time
def cal_time(func):
def inner(*args):
start = time.time()
func(*args)
end = time.time()
print(‘run coding time %s‘ % (end-start))
return inner
from cal_time import cal_time
@cal_time
def binary_search(li, val):
left = 0
right = len(li)-1
while left <= right: # 候选区有值
# mid是中间值的下标
mid = (left+right) // 2
if li[mid] == val: # 找到了就返回索引
return mid
elif li[mid] < val: # 如果中间值小于找的值,说明要找的值在右边候选区
left = mid + 1
else:
right = mid - 1
else:
return None
li = list(range(10000000))
binary_search(li, 6678)
@cal_time
def line_search(li, val):
for key, value in enumerate(li):
if value == val:
return value
else:
return None
line_search(li, 56567856)
标签:else imp binary *args 技术分享 结束 run 有序 arch
原文地址:https://www.cnblogs.com/lishi-jie/p/9902544.html