我正在按照指令编写一个bisection_search:
import random
arr = list(range(11))
random.shuffle(arr)
m = 9
#list is a stack in python
def bisection_search(m, arr):
top_index = len(arr) - 1 # rather than top_index = -1
bottom_index = 0
while bottom_index <= top_index:
mid_index = (top_index + bottom_index) // 2
pivot = arr[mid_index]
if m == pivot:
return mid_index
if m > pivot:
bottom_index = mid_index + 1
else:
top_index = mid_index - 1
return None
target_index = bisection_search(m, arr)
print(target_index)
## -- End pasted text --
None我使用了ipython的%paste,它没有返回,
另一种尝试:
In [3]: arr
Out[3]: [4, 10, 7, 3, 0, 1, 9, 6, 2, 5, 8]
In [4]: m
Out[4]: 9
In [5]: bisection_search(m, arr)
In [6]: x = bisection_search(m, arr)
In [7]: x
In [8]: def bisection_search(m, arr):我仔细检查了密码,确认里面没有窃听器。
怎么可能没有结果呢?
发布于 2018-08-21 01:23:33
https://stackoverflow.com/questions/51940279
复制相似问题