[英]RuntimeError: maximum recursion depth exceeded in Python
#Recursive BinaryChop
def recursiveBinaryChop( value, elementList, min, max ):
if len( elementList ) == 0:
return -1
if max <= min:
if ( max == min and elementList[min] == value ):
return min
else:
return -1
else:
midPointOfList = ( min + max ) / 2
if elementList[midPointOfList] > value:
max = --midPointOfList
return recursiveBinaryChop( value, elementList, min, max )
elif elementList[midPointOfList] < value:
min = ++midPointOfList
return recursiveBinaryChop( value, elementList, min, max )
else:
return midPointOfList
#Recursive BinaryChop Test Cases
assert recursiveBinaryChop(3, [], 0, 0) == -1
assert recursiveBinaryChop(3, [1], 0, 0) == -1
assert recursiveBinaryChop(1, [1], 0, 0) == 0
assert recursiveBinaryChop(1, [1, 3, 5], 0, 2) == 0
assert recursiveBinaryChop(3, [1, 3, 5], 0, 2) == 1
assert recursiveBinaryChop(5, [1, 3, 5], 0, 2) == 2
assert recursiveBinaryChop(0, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(2, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(4, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(6, [1, 3, 5], 0, 2) == -1
assert recursiveBinaryChop(1, [1, 3, 5, 7], 0, 3) == 0
assert recursiveBinaryChop(3, [1, 3, 5, 7], 0, 3) == 1
assert recursiveBinaryChop(5, [1, 3, 5, 7], 0, 3) == 2
assert recursiveBinaryChop(7, [1, 3, 5, 7], 0, 3) == 3
assert recursiveBinaryChop(0, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(2, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(4, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(6, [1, 3, 5, 7], 0, 3) == -1
assert recursiveBinaryChop(8, [1, 3, 5, 7], 0, 3) == -1
I am getting the run time error for this simple code, I have tried searching but all answer seems to suggest setting the recursion limit but I don't see that happening with my test input. 我得到这个简单代码的运行时错误,我已经尝试搜索,但所有答案似乎建议设置递归限制但我没有看到我的测试输入发生。 I am not sure whether my algorithm is wrong or has some logical error.
我不确定我的算法是错误的还是有一些逻辑错误。 I have the same algorithm working for me in
C++
. 我有相同的算法在
C++
为我工作。
Please help. 请帮忙。
if elementList[midPointOfList] > value:
max = --midPointOfList
return recursiveBinaryChop( value, elementList, min, max )
elif elementList[midPointOfList] < value:
min = ++midPointOfList
return recursiveBinaryChop( value, elementList, min, max )
Python doesn't have -- or ++ operators. Python没有 - 或++运算符。 If you're trying to decrement and increment, try "-1" and "+1".
如果您尝试递减和递增,请尝试“-1”和“+1”。
if elementList[midPointOfList] > value:
max = midPointOfList - 1
return recursiveBinaryChop( value, elementList, min, max )
elif elementList[midPointOfList] < value:
min = midPointOfList + 1
return recursiveBinaryChop( value, elementList, min, max )
(This isn't exactly the same behavior as C++'s --
and ++
, since midPointOfList
's value remains unchanged, but that doesn't seem to matter in this particular circumstance; midPointOfList
doesn't get referred to after those lines anyway) (这是不完全一样的行为C ++的
--
和++
,因为midPointOfList
的值保持不变,但似乎并没有在这个特定环境下重要; midPointOfList
没有得到反正提到的那些行后)
These two lines don't do what you think: 这两行不符合你的想法:
max = --midPointOfList
min = ++midPointOfList
Python doesn't have this type of increment operators, but they do parse and execute successfully. Python没有这种类型的增量运算符,但它们解析并成功执行。
++i
parses as +(+i)
and --i
as -(-i)
. ++i
解析为+(+i)
和--i
as -(-i)
。 Both leave i
unchanged and are effectively 双方离开
i
不变,是有效
max = midPointOfList
min = midPointOfList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.