def bsearch(s, e, first, last):
print(first, last)
if (last - first) < 2:
return s[first] == e or s[last] == e
mid = first + (last - first)/2
if s[mid] == e: return True
if s[mid] > e: return bsearch(s, e, first, mid - 1)
return bsearch(s, e, mid + 1, last)
def search1(s,e):
print(bsearch(s, e, 0, len(s)-1))
print('Search complete')
def testSearch():
s = range(0, 1000000)
input('binary,-1')
print(search1(s, -1))
它是二進制搜索算法。我有兩個問題。二進制搜索程序
問題1: 爲什麼在以下行中需要first
?
mid = first + (last - first)/2
問題2: 我不能運行的結果,當我跑的程序。 的錯誤信息是:
range indices must be integers or slices, not float.
我怎樣才能解決呢?
當你使用'python 3 float','python 3 range function'和你的錯誤信息時,你發現了什麼? – philipxy