Recursive multi shift caesar cipher - 类型错误(无法连接 None)

Recursive multi shift caesar cipher - Type error (can't conc None)

第一次在这里发帖,python 的新手,但真的很喜欢我用它做的事情。目前正在解决 MIT Open 课件问题。有没有其他类似资源的建议?

我的问题是 return 递归函数,该函数旨在将多层移位列表构建为元组,其中每个元组所在的位置(移位的起始位置、移位的幅度)。移动 5 会将 a 变为 f,a-b-c-d-e-f。

下面的代码供参考,但您不需要全部阅读。

text为多层打乱输入,例如:'grrkxmdffi jwyxechants idchdgyqapufeulij'

def find_best_shifts_rec(wordlist, text, start):

    ### TODO.

    """Shifts stay in place at least until a space, so shift can only start
    with a new word
    Base case? all remaining words are real

    Need to find the base case which goes at the start of the function to
    ensure it's all cleanly returned

    Base case could be empty string if true words are removed?
    Base case when start = end

    take recursive out of loop

    use other functions to simplify
    """


    shifts = []
    shift = 0


    for a in range(27):
        shift += 1
        #creates a string and only shifts from the start point
        """text[:start] + optional add in not sure how it'd help""" 
        testtext = apply_shift(text[start:],-shift)

        testlist = testtext.split()

        #counts how many real words were made by the current shift
        realwords = 0
        for word in testlist:
            if word in wordlist:
                realwords += 1

            else:
                #as soon as a non valid word is found i know the shift is not valid
                break
        if a == 27 and realwords == 0:
            print 'here\'s the prob'
        #if one or more words are real
        if realwords > 0:

            #add the location and magnitude of shift
            shifts = [(start,shift)]

            #recursive call - start needs to be the end of the last valid word
            start += testtext.find(testlist[realwords - 1]) + len(testlist[realwords - 1]) + 1

            if start >= len(text):

                #Base case
                return shifts
            else:
                return shifts + find_best_shifts_rec(wordlist,text,start)

这经常 return 是正确的元组列表,但有时,例如使用此文本输入,我会收到错误消息:

    return shifts + find_best_shifts_rec(wordlist,text,start)
TypeError: can only concatenate list (not "NoneType") to list

此错误是针对我代码最底部的以下内容

else:
                return shifts + find_best_shifts_rec(wordlist,text,start)

据我所知,其中一个递归调用 return 是 None 值,然后尝试将其与列表结合会引发错误。我该如何解决这个问题?

编辑:

最后加上:

elif == 26: return[()]

当找不到正确的班次时,我可以防止类型错误。我怎样才能得到整个函数 return none?

下面是我尝试重新编写您的代码以执行您想要的操作。具体更改:将范围从 27 降低到 26 让循环自然退出和 return 空 shifts 数组;合并 ashift 并从零开始,因此未编码的文本将 return [(0, 0)];如果同一个词在列表中出现两次,.find() 逻辑将混乱,将其更改为 rindex() 作为创可贴(即最后一个正确解码的是你想要开始的地方,而不是第一个)。

def find_best_shifts_rec(wordlist, text, start=0):

    shifts = []

    for shift in range(26):

        # creates a string and only shifts from the start point
        testtext = apply_shift(text[start:], -shift)

        testlist = testtext.split()

        # words made by the current shift
        realwords = []

        for word in testlist:

            if word in wordlist:
                realwords.append(word)
            else:  # as soon as an invalid word is found I know the shift is invalid
                break

        if realwords:  # if one or more words are real

            # add the location and magnitude of shift
            shifts = [(start, shift)]

            # recursive call - start needs to be the end of the last valid word

            realword = realwords[-1]

            start += testtext.rindex(realword) + len(realword) + 1

            if start >= len(text):
                return shifts  # base case

            return shifts + find_best_shifts_rec(wordlist, text, start)

    return shifts

'lbh fwj hlzkv tbizljb'