python递归计数_python中递归计数器的解决方案

该程序的基本思想是在一个大列表中搜索给定字符串,记录其索引,并根据该索引分配其他单词。主要问题是,由于系统中内置了一些拒绝某些单词的需求,因此很可能会出现非常大的递归深度。可能有一些解决方案可以通过完全避免递归来规避这个问题(我很有兴趣看到),但是递归似乎是最优雅的解决方案,特别是这个程序的几个版本将处理越来越多的单词和索引。因此,主要的问题是,如何可能让它计数递归,以便它可以踢出函数,如果达到深度限制?理想情况下,这将是一个循环,这样它就可以从它停止的地方开始,但这似乎是一个不同的问题。实际上,计数器正在意外重置。在lines = open ('newkj')

corpus= []

for line in lines :

corpus.extend(line.split())

limit = 800000

globallimit=800000

count=0

count2=0

"""This one is unique as it carries the null case in it. It is also a forward facing no check function"""

def curgb (indexa=indexof('the'),count=0) :

worda=wordfor (indexa)

wordb = wordfor (indexa+1)

indexb=indexof(wordb)

wordc = wordfor (advance(indexa)+1)

indexc=indexof (wordc)

if indexa> globallimit:

print ('no results')

return (indexa)

elif indexb

curgd (indexc,indexb,indexa,count2+1)

else:

curgb(advance(indexa),count+1)

"""This function is also forward facing no check"""

def curgd (indexc,indexb,indexa,count2=0):

wordd = wordfor (indexc+1)

indexd = indexof(wordd)

indexd=check(indexd,indexb,-1)

print(count2)

if indexd

"""print (wordfor(indexa),wordfor(indexb))

print (wordfor(indexd),wordfor (indexc))

print (' ')"""

curgd(advance(indexc),indexb,indexa,count2+1)

else :

curgb(advance(indexa),count+1)

实际上,这是简化的,不太可能达到递归限制,但计数器重置的主要问题仍然存在。运行这个程序就是用一个整数参数调用curgb(),对应于给定的索引。在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值