Python 多级凯撒密码

Python 多级凯撒密码,python,Python,嘿,我想破解一个多级凯撒密码。我的意思是一个字母串可能已经移位了好几次,所以如果我说apply_shift[2,3,4,5],这意味着我将第二个字母的所有内容移位3,然后将第四个字母的所有内容移位5。这是到目前为止我的代码 def find_best_shifts_rec(wordlist, text, start): """ Given a scrambled string and a starting position from which to decode, re

嘿,我想破解一个多级凯撒密码。我的意思是一个字母串可能已经移位了好几次,所以如果我说apply_shift[2,3,4,5],这意味着我将第二个字母的所有内容移位3,然后将第四个字母的所有内容移位5。这是到目前为止我的代码

def find_best_shifts_rec(wordlist, text, start):
    """
    Given a scrambled string and a starting position from which
    to decode, returns a shift key that will decode the text to
    words in wordlist, or None if there is no such key.

    Hint: You will find this function much easier to implement
    if you use recursion.

    wordlist: list of words
    text: scambled text to try to find the words for
    start: where to start looking at shifts
    returns: list of tuples.  each tuple is (position in text, amount of shift)
    """

    for shift in range(27):
        text=apply_shifts(text, [(start,-shift)])
        #first word is text.split()[0]
        #test if first word is valid.  if not, go to next shift

        if is_word(wordlist,text.split()[0])==False:
            continue

        #enter the while loop if word is valid, otherwise never enter and go to the next shift
        i=0
        next_index=0
        shifts={}
        while is_word(wordlist,text.split()[i])==True:
            next_index+= len(text.split()[i])
            i=i+1
            #once a word isn't valid, then try again, starting from the new index.
            if is_word(wordlist,text.split()[i])==False:
                shifts[next_index]=i
                find_best_shifts_rec(wordlist, text, next_index)

    return shifts
我的问题是

1我的代码没有正常运行,我不明白为什么它会搞砸它没有进入我的while循环 和 2我不知道如何测试我的最后一个移位(例如字符串的最后一部分)是否都是有效的单词,我也不知道如何再次从那里开始循环


非常感谢您的帮助。

我认为问题在于您总是处理整个文本,但在文本内部的某个开头应用新的移位。因此,您的检查是\u wordlist,text.split[0]将始终检查第一个单词,当然,这是您第一次轮班后的一个单词

你需要做的是在你的新起点之后找到第一个单词,所以检查文本中实际未处理的部分

编辑 我注意到的另一个问题是您尝试找到正确换档的方式:

for shift in range(27):
    text=apply_shifts(text, [(start,-shift)])
因此,您基本上希望尝试从0到26的所有移位,直到第一个单词被接受。这样做是可以的,但是请注意,在第一次尝试移位之后,文本已经改变了。因此,你不能将它移动1,2,3。。。但是到了1,3,6,10。。。这当然不是你想要的,你当然会错过一些班次,而做一些相同的多次

因此,在继续处理文本之前,您需要临时移动文本并检查该临时文本的状态。或者,您总是按1移位

编辑² 我注意到的另一个问题是,您试图使用递归来获得最终结果的方式。通常,带有结果的递归的工作方式与您一直调用函数本身、传递返回值或收集结果的方式相同。在您的情况下,由于您希望有多个值,而不仅仅是内部某个位置的单个值,因此需要收集每个移位结果


但是现在,您正在丢弃递归调用的返回值,只返回最后一个值。因此,请存储所有值并确保不会丢失它们。

递归函数的伪代码:

coded_text = text from start-index to end of string
if length of coded_text is 0, return "valid solution (no shifts)"

for shift in possible_shifts:
    decoded_text = apply shift of (-shift) to coded_text
    first_word = split decoded_text and take first piece
    if first_word is a valid word:
        rest_of_solution = recurse on (text preceding start-index)+decoded_text, starting at start+(length of first_word)+1
        if rest_of_solution is a valid solution
            if shift is 0
                return rest_of_solution
            else
                return (start, -shift mod alphabet_size) + rest_of_solution

# no valid solution found
return "not a valid solution"

请注意,这保证给出由有效单词组成的答案,而不一定是原始字符串。一个具体的例子:“一顶附加帽”可以被解码来代替“一看”。

家庭作业?您还应该确保代码的格式正确。看起来不全是缩进的。是的,这是家庭作业。我不是要求别人帮我做这件事,只是需要帮助看看我的代码出了什么问题。这个函数是提供给你的吗?它看起来像什么?是的,是单词列表,单词是提供给我的。它只是检查单词是否在给定的单词列表中。我还想加上一句话,如果测试的第一个单词在任何移位下都无效,那么就回到之前的循环。我只是不知道最好的方法。