python对分搜索练习
我已经看了一段时间了,我看不出我的对分搜索有什么问题。如果我运行它,它会显示“RecursionError:在比较中超过了最大递归深度”。谁能看看下面有什么问题吗?谢谢大家!python对分搜索练习,python,bisection,Python,Bisection,我已经看了一段时间了,我看不出我的对分搜索有什么问题。如果我运行它,它会显示“RecursionError:在比较中超过了最大递归深度”。谁能看看下面有什么问题吗?谢谢大家! #Write a function called in_bisect #that takes a sorted list and a target value #and returns the index #of the value in the list if it’s there def in_bisect(t,
#Write a function called in_bisect
#that takes a sorted list and a target value
#and returns the index
#of the value in the list if it’s there
def in_bisect(t, word):
if len(t) == 0:
return False
middle = len(t) // 2
if t[middle] == word:
return middle
elif t[middle] > word:
return in_bisect(t[:middle], word)
else:
return in_bisect(t[middle:], word)
if __name__ == "__main__":
fruits = ['apple', 'banana', 'kiwi', 'peach', 'watermelon']
in_bisect(fruits, 'banana')
in_bisect(fruits, 'ewf')
想象一下当列表长度为1时会发生什么。那么中间将是0。如果现在最后的
else
案例是实际案例,那么递归将再次得到相同的列表(大小),结果相同。。。无限递归
解决方案:在middle
中添加一个,因为此时您已经知道middle
本身不再是候选项:
else:
return in_bisect(t[middle+1:], word)
想象一下当列表长度为1时会发生什么。那么中间将是0。如果现在最后的
else
案例是实际案例,那么递归将再次得到相同的列表(大小),结果相同。。。无限递归
解决方案:在middle
中添加一个,因为此时您已经知道middle
本身不再是候选项:
else:
return in_bisect(t[middle+1:], word)
我在这里使用循环而不是递归,因为Python不能将尾部递归转换为循环,并且对递归深度的限制非常低 我还要检查
word
是否在t
中,否则返回False
def in_bisect(t, word):
def iterator(start, end):
# loop will terminate when exactly start = end - 1
while start < end - 1:
middle = (start + end) // 2
if t[middle] == word:
return middle
elif t[middle] > word:
end = middle
else:
start = middle + 1
# here we need to check wheither the last element in the list is the one we search for
return start if t[start] == word else False
# if len(t) is zero, our inner function would raise IndexError so we check it explicitly
if len(t) == 0:
return False
return iterator(0, len(t))
def对分(t,word):
def迭代器(开始、结束):
#循环将在恰好开始=结束-1时终止
当开始<结束-1时:
中间=(开始+结束)//2
如果t[中间]==字:
返回中间
elif t[中间]>单词:
末端=中间
其他:
开始=中间+1
#这里我们需要检查列表中的最后一个元素是否是我们搜索的元素
如果t[start]==word else为False,则返回start
#如果len(t)为零,我们的内部函数将引发indexer,所以我们显式地检查它
如果len(t)==0:
返回错误
返回迭代器(0,len(t))
我会在这里使用循环而不是递归,因为Python无法将尾部递归转换为循环,并且递归深度的限制非常低
我还要检查word
是否在t
中,否则返回False
def in_bisect(t, word):
def iterator(start, end):
# loop will terminate when exactly start = end - 1
while start < end - 1:
middle = (start + end) // 2
if t[middle] == word:
return middle
elif t[middle] > word:
end = middle
else:
start = middle + 1
# here we need to check wheither the last element in the list is the one we search for
return start if t[start] == word else False
# if len(t) is zero, our inner function would raise IndexError so we check it explicitly
if len(t) == 0:
return False
return iterator(0, len(t))
def对分(t,word):
def迭代器(开始、结束):
#循环将在恰好开始=结束-1时终止
当开始<结束-1时:
中间=(开始+结束)//2
如果t[中间]==字:
返回中间
elif t[中间]>单词:
末端=中间
其他:
开始=中间+1
#这里我们需要检查列表中的最后一个元素是否是我们搜索的元素
如果t[start]==word else为False,则返回start
#如果len(t)为零,我们的内部函数将引发indexer,所以我们显式地检查它
如果len(t)==0:
返回错误
返回迭代器(0,len(t))
递归时必须排除中间值,以避免无限递归。您忘记从最后一个案例中排除它。顺便说一句,您返回中间值
而不是返回t[middle]
在递归时必须排除中间值,以避免无限递归。您忘记将其从上一个案例中排除。顺便说一句,您将返回中间值
而不是返回t[middle]
谢谢您的代码!我不知道Python是递归的下限,我也没有在另一个定义中看到一个定义——看看它是如何以这种方式编写的是很有帮助的。默认限制是1000 AFAIK,但如果您愿意,可以更改它。无论如何,Python开发人员更喜欢命令式范式。他们将reduce
之类的函数从builtins
移动到functools
模块,并且不支持尾部递归优化,因此,在我看来,最好不要在Python中使用函数样式:-)感谢您的代码!我不知道Python是递归的下限,我也没有在另一个定义中看到一个定义——看看它是如何以这种方式编写的是很有帮助的。默认限制是1000 AFAIK,但如果您愿意,可以更改它。无论如何,Python开发人员更喜欢命令式范例。他们将reduce
之类的函数从builtins
移动到functools
模块,并且不支持尾部递归优化,因此,在我看来,最好不要在Python中使用函数式:-)为代码添加一些解释可能会帮助我们更好地理解编写的代码。为代码添加一些解释可能会帮助我们更好地理解编写的代码。