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

发布于 2024-11-16 16:02:13 字数 1586 浏览 1 评论 0原文

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

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<limit and indexc<limit:
        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<limit and indexc<limit and indexb< limit and wordfor(indexd-1)==wordfor(indexb):
        """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() 即可。

The basic idea of the program is that it searches a large list for a given string, notes its index, and assigns other words based upon that index. The major issue is that, as there are some requirements built into the system that reject certain words, a very large recursion depth is likely to occur. There may be solutions that circumvent this problem (which I would be very interested in seeing) by avoiding recursion altogether, but recursion seems to be the most elegant solution, especially as there will be several versions of this program, that handle more and more words and indexes. Therefore, the major question is, how is it possible to get it to count recursion, so that it can kick out of the function if a depth limit is reached? Ideally, this would be in a loop, so that it could start where it left off from after that, but that seems to be a different problem. As it is, the counters are resetting unexpectedly.

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<limit and indexc<limit:
        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<limit and indexc<limit and indexb< limit and wordfor(indexd-1)==wordfor(indexb):
        """print (wordfor(indexa),wordfor(indexb))
        print (wordfor(indexd),wordfor (indexc))
        print ('     ')"""
        curgd(advance(indexc),indexb,indexa,count2+1)
    else :
        curgb(advance(indexa),count+1)

As it is, this is simplified and not likely to hit a recursion limit, but the major problem of the counter resetting is still there. Running the program is a matter of calling curgb() with an integer argument, corresponding to a given index.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-11-23 16:02:13

您似乎混合了局部变量和全局变量。

curgb 中,未定义 count2 变量,在 curgd 中,未定义 count 变量。使用 global keywork 来访问它们,或者将两个变量作为参数传递给两个函数(后者是你应该做的)

You seem to be mixing local and global variables.

In curgb, count2 variable isn't defined, and in curgd, count variable isn't defined. Either use global keywork to access them, or pass both variable as arguments to both functions (the later is what you should do)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文