[英]Python RuntimeError: maximum recursion depth exceeded
I am new about Python. 我是关于Python的新手。 I wrote a function about returns the number of occurences of x in the sorted repeated elements array A: 我写了一个关于返回排序重复元素数组A中x的出现次数的函数:
def FindFirstIndex(A, low, high, x, n):
low = 0
high = len(A) - 1
if low <= high:
mid = low + (high - low) / 2
if (mid == 0 or x > A[mid - 1]) and A[mid] == x:
return mid
elif x > A[mid]:
return FindFirstIndex(A, (mid + 1), high, x, n)
else:
return FindFirstIndex(A, low, (mid - 1), x, n)
return -1
def FindLastIndex(A, low, high, x, n):
low = 0
high = len(A) - 1
if low <= high:
mid = low + (high - low) / 2
if (mid == n - 1 or x < A[mid + 1]) and A[mid] == x:
return mid
elif x < A[mid]:
return FindFirstIndex(A, low, (mid - 1), x, n)
else:
return FindFirstIndex(A, (mid + 1), high, x, n)
return -1
def COUNT(A, x):
i = FindFirstIndex(A, 0, len(A) - 1, x, len(A))
if i == -1:
return i
j = FindLastIndex(A, i, len(A) - 1, x, len(A))
length = j - i + 1
return length
The error is: RuntimeError: maximum recursion depth exceeded. 错误是:RuntimeError:超出最大递归深度。 Anybody knows how to solve it? 有人知道怎么解决吗?
A recursive fuction should check arguments in every recursion.Also must change the values of arguments.Otherwise it never ends.For example 递归函数应检查每个递归中的参数。还必须更改参数的值。否则它永远不会结束。例如
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
Which provides factorial of n 其中提供了n的阶乘
sure you can use: 确定你可以使用:
import sys
sys.setrecursionlimit(3000)
I believe Python's default is 1000 so this should do. 我相信Python的默认值是1000,所以这应该做。 I caution you though if you try and test your implementation on a very large list python might crash so I would encourage you to re-implement your code iteratively 我告诫你,如果你试着在一个非常大的列表上测试你的实现,python可能会崩溃,所以我鼓励你迭代地重新实现你的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.