2016-08-20 5 views
0

初めてここに投稿すると、初心者にはpythonが、実際には私がやっていることを楽しんでいます。現時点では、MITオープンコースウェアの問題を扱っています。他の同様のリソースの提案ですか?再帰的マルチシフトシーザー暗号 - タイプエラー(コンクールなし)

私の問題は、各タプルが(シフトの開始位置、シフトの大きさ)であるタプルとしてマルチレイヤーシフトのリストを構築することを意味する再帰関数を返すことです。 5のシフトは、a、b、c、d、e、fに変化する。

参考のためにコードを作成してください。ただし、すべて読む必要はありません。

テキストが多層である例えば、入力をスクランブル:これは頻繁にタプルの正しいリストを返しますが、時には、例えば、このテキスト入力と、私はエラーを取得する

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) 

「grrkxmdffi jwyxechants idchdgyqapufeulij」:

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) 

私が収集したものから、再帰呼び出しの1つがNone値を返し、次にリストでconcを試みると、エラーがスローされます。どうすればこの問題を回避できますか?

EDIT:

のelif == 26:末尾に追加することにより

それが正しいを見つけることができない場合 リターン[()]

I型のエラーを防ぐことができシフト。関数全体を返さないようにするにはどうすればよいですか?

答えて

0

以下は、あなたが望むことを行うためにコードを修正する試みです。具体的な変更:ループを自然に終了させ、空のshifts配列を返すように範囲を27から26に落としました。 ashiftを組み合わせてゼロで開始したので、エンコードされていないテキストは[(0, 0)]を返します。 .find()ロジックは、同じ単語がリストに2回表示されると混乱し、バンダイとして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'

+0

おかげで私はあなたがこれを見て時間を割い感謝しています。私は特に.rindex()の使用が好きです。私はそれがバギーのようなやり方で最終的に働くようにしました。再度、感謝します :) –