【发布时间】:2019-11-14 13:15:28
【问题描述】:
def binarySearch(list, selection):
start = 0
end = len(list) - 1
while start <= end:
middle = start + (end - start) // 2
middleValue = list[middle]
if middleValue == selection:
return middle
elif selection < middleValue:
end = middle - 1
else:
start = middle + 1
return None
lista = [1, 5, 7, 10, 11, 19,]
print(lista)
selectiona = int(input('Enter a number to search for: '))
index = lista.index(selectiona)
binarySearch(lista, selectiona)
print(str(selectiona)) + "found at index " + str(index))
exit = input()
它可以在不打印索引的情况下工作,但这是一个要求。如果有人可以就我做错的事情提出建议,我将不胜感激。谢谢
【问题讨论】:
-
问题仍然存在,伙计。锁定要找你吗?