python递归深度报错_已达到Python最大递归深度

因此,我得到的是一系列在字符串中搜索目标值的函数。

(例如:在$&(*,)中查找R,。02468:<@BDFHJLNPRTVXZ\^`bdfhj,H在索引18处找到。)

但是,当对某些字符执行函数测试时,它工作得很好,但对于其他一些字符(如D或L),它给了我一个“最大递归深度到达误差”。在

也适用于前面的字符,如$&(,),。02468:<@BDFHJLNPRTVXZ\^`bdfhj,结果是“找不到”*

作为一个python新手,我很难看到这些函数有什么问题,那么这里似乎有什么问题呢?在

编辑----------

很抱歉,我的意思是在导致最大递归深度错误的函数中出现了什么错误,以及如何开始测试这些函数。def str_search( data, target, start, end ):

"""

str_search : String String NatNum NatNum -> NatNum or NoneType

Description:

Search for a target value in a sorted data string.

The search happens between the start and end indices inclusively.

This starts searching in the middle. If it finds the target, it is done.

Otherwise it decides whether to search the first half or the second half.

preconditions: the data string is in ascending alphanumeric order.

Parameters:

data - a string

target - the target value to find is a single character string e.g. 'Q'

start - the starting index into the data

end - the ending index into the data

Returns:

index of target in data, if present; otherwise None.

"""

if start == end:

return None

mid_index = ( start + end ) // 2

mid_value = data[mid_index]

# debug statement prints the data.

#print( "Searching for", target, ":", data[start:mid_index],

# "*" + str( mid_value ) + "*", data[mid_index+1:end+1] )

if target == mid_value:

return mid_index

elif target < mid_value:

return str_search( data, target, start, mid_index-1 )

else:

return str_search( data, target, mid_index, end )

def find_target( data, target ):

"""

find_target : String String -> NatNum or NoneType

find_target returns the index of target in data or None if not found.

Parameters:

data - a string

target - the target value to find

Returns:

The index of the target element in data, if present, or None.

"""

return str_search( data, target, 0, len( data ) - 1 )

def makeString():

"""

makeString : () -> String

makeString returns a String

"""

data = ""

# append characters to make the string

for num in range( 36, 108, 2 ):

data += chr( num )

return data

def main_search():

"""

main_search : Void -> NoneType

"""

data = makeString()

print( "Number of elements: ", len( data ) )

while True:

print( "\nData: ", data )

target = input( "Enter a character to find: " )

if target == "":

break

else:

index = find_target( data, target )

print()

if index != None:

print( target, "found at index", index )

else:

print( target, "not found" )

# end while

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值