プログラムはほぼ完成しましたが、微妙な間違いを犯しました。私のプログラムは単語を取り、一度に 1 文字ずつ変更することで、指定されたステップ数で最終的に目的の単語に到達するはずです。最初は類似点を探していました。たとえば、単語が見つかり、ターゲット単語が失われた場合、プログラムは次のように 4 つのステップで出力します。
['find','fine','line','lone','lose]
これは実際に私が望んでいた出力です。しかし、Java や作業などのより難しい単語のセットを考慮すると、出力は 6 つのステップであると想定されます。
['java', 'lava', 'lave', 'wave', 'wove', 'wore', 'work']
つまり、訳語や原語に存在しない文字を使って、訳語にたどり着くことができるとは知らなかったのが私の間違いです。
これが私の元のコードです:
import string
def changeling(word,target,steps):
alpha=string.ascii_lowercase
x=word##word and target has been changed to keep the coding readable.
z=target
if steps==0 and word!= target:##if the target can't be reached, return nothing.
return []
if x==z:##if target has been reached.
return [z]
if len(word)!=len(target):##if the word and target word aren't the same length print error.
print "error"
return None
i=1
if lookup
if lookup(z[0]+x[1:]) is True and z[0]+x[1:]!=x :##check every letter that could be from z, in variations of, and check if they're in the dictionary.
word=z[0]+x[1:]
while i!=len(x):
if lookup(x[:i-1]+z[i-1]+x[i:]) and x[:i-1]+z[i-1]+x[i:]!=x:
word=x[:i-1]+z[i-1]+x[i:]
i+=1
if lookup(x[:len(x)-1]+z[len(word)-1]) and x[:len(x)-1]+z[len(x)-1]!=x :##same applies here.
word=x[:len(x)-1]+z[len(word)-1]
y = changeling(word,target,steps-1)
if y :
return [x] + y##used to concatenate the first word to the final list, and if the list goes past the amount of steps.
else:
return None
これが私の現在のコードです:
import string
def changeling(word,target,steps):
alpha=string.ascii_lowercase
x=word##word and target has been changed to keep the coding readable.
z=target
if steps==0 and word!= target:##if the target can't be reached, return nothing.
return []
if x==z:##if target has been reached.
return [z]
holderlist=[]
if len(word)!=len(target):##if the word and target word aren't the same length print error.
print "error"
return None
i=1
for items in alpha:
i=1
while i!=len(x):
if lookup(x[:i-1]+items+x[i:]) is True and x[:i-1]+items+x[i:]!=x:
word =x[:i-1]+items+x[i:]
holderlist.append(word)
i+=1
if lookup(x[:len(x)-1]+items) is True and x[:len(x)-1]+items!=x:
word=x[:len(x)-1]+items
holderlist.append(word)
y = changeling(word,target,steps-1)
if y :
return [x] + y##used to concatenate the first word to the final list, and if the/
list goes past the amount of steps.
else:
return None
2 つの違いは、1 つ目は、lose の文字を含む find のすべてのバリエーションをチェックすることです。意味:リンド、フォンド、フィズド、ファイン。次に、lookup 関数で有効な単語が見つかった場合、その新しく見つかった単語に対して changeling を呼び出します。
アルファベットのすべての文字で検索のすべてのバリエーションをチェックする私の新しいプログラムとは対照的です。
このコードを機能させることができないようです。find の結果を単純に出力してテストしました。
for items in alpha:
i=1
while i!=len(x):
print (x[:i-1]+items+x[i:])
i+=1
print (x[:len(x)-1]+items)
これは与える:
aind
fand
fiad
fina
bind
fbnd
fibd
finb
cind
fcnd
ficd
finc
dind
fdnd
fidd
find
eind
fend
fied
fine
find
ffnd
fifd
finf
gind
fgnd
figd
fing
hind
fhnd
fihd
finh
iind
find
fiid
fini
jind
fjnd
fijd
finj
kind
fknd
fikd
fink
lind
flnd
fild
finl
mind
fmnd
fimd
finm
nind
fnnd
find
finn
oind
fond
fiod
fino
pind
fpnd
fipd
finp
qind
fqnd
fiqd
finq
rind
frnd
fird
finr
sind
fsnd
fisd
fins
tind
ftnd
fitd
fint
uind
fund
fiud
finu
vind
fvnd
fivd
finv
wind
fwnd
fiwd
finw
xind
fxnd
fixd
finx
yind
fynd
fiyd
finy
zind
fznd
fizd
finz
これは完璧です!アルファベットの各文字が少なくとも 1 回は単語を通過することに注意してください。さて、私のプログラムは、ヘルパー関数を使用して、その単語が与えられた辞書にあるかどうかを判断します。
これを考えてみてください。私の最初のプログラムのようにではなく、正しい単語を複数受け取るようになりました。これが、holderlist.append(word) を試している理由です。
私の問題は、ホルダーリストの各単語を実行するためにチェンジリングが必要なことだと思いますが、その方法がわかりません。それは憶測に過ぎませんが。
どんな助けでも大歓迎です、
乾杯。