初めてここに投稿すると、初心者には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型のエラーを防ぐことができシフト。関数全体を返さないようにするにはどうすればよいですか?
おかげで私はあなたがこれを見て時間を割い感謝しています。私は特に.rindex()の使用が好きです。私はそれがバギーのようなやり方で最終的に働くようにしました。再度、感謝します :) –