我幾乎完成了我的程序,但我犯了一個微妙的錯誤。我的程序應該記錄一個字,並且一次更改一個字母,最終應該按照指定的步數達到目標字。我起初一直試圖尋找相同點,例如:如果字是發現,目標字輸了,這裏是如何我計劃將在4個步驟輸出:字母和遞歸
['find','fine','line','lone','lose]
這實際上是我想要的輸出。但是如果你考慮一組更強硬的詞,比如Java和work,那麼輸出結果應該分爲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
兩者之間的區別是,首先檢查與丟失的信件找到的每一個變化。含義:林德,喜歡,fisd和罰款。然後,如果它發現查找功能的工作單詞,它會調用該新單詞的換行。
與我的新程序相反,它使用字母表中的每個字母檢查查找的每個變體。
我似乎無法得到此代碼的工作。我通過簡單的打印結果如何發現的測試吧:
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
這是完美的!請注意,字母表中的每個字母至少要經過一次字。現在,我的程序所做的就是使用幫助函數來確定這個單詞是否在我給出的字典中。
考慮這一點,而不是像我的第一個程序,我現在收到是合法的,當我做詞,除了多個字= foundword這意味着我更換,每次上一個字。這就是爲什麼我正在嘗試holderlist.append(單詞)。
我想我的問題是,我需要低能通過holderlist每個字跑,我不知道該怎麼做。儘管這只是猜測。
任何幫助,將不勝感激,
乾杯。
「find」如何達到「丟失」。你能解釋它背後的邏輯嗎?如何更改每個單詞.. – Surya 2012-03-28 18:04:36
我會告訴你我的原始代碼。 – Unknown 2012-03-28 18:06:20
@Nichols我寧願你寫一個關於這個單詞如何改變的小說明。這將幫助我們輕鬆。至少如果你能描述你的算法,那它很簡單! – Surya 2012-03-28 18:09:31