我在“ Grokking算法”一书中发现一个错误,

问题描述 投票:1回答:1
我是编程的初学者。我正在阅读Aditya Y Bhargava撰写的“ Grokking算法”一书。”在第一个代码中,我发现了一个错误。该书描述了二进制算法。它说算法必须取一个数组的平均值,然后取该平均值的平均值,但是我对代码进行了调试,它只需要处理大量数组,然后将其减少1。也许这是版本之间的差异,因为我使用的是Python 3.7,但在书中是Python 2.7

def binary_search(list, item): low = 0 high = len(list)-1 while low <= high: mid = int(low + high) guess = list[mid] if guess == item: return mid if guess > item: high = mid - 1 else: low = mid + 1 return None my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(binary_search(my_list, 1))

我是编程的初学者。我正在阅读Aditya Y Bhargava撰写的“ Grokking算法”这本书。”在第一个代码中,我发现了一个错误。该书描述了二进制算法。它说...
python
1个回答
0
投票
The errata for the book将此列为错误。应该是:
© www.soinside.com 2019 - 2024. All rights reserved.