【发布时间】:2012-04-12 08:57:30
【问题描述】:
我的程序快完成了,但我犯了一个微妙的错误。我的程序应该接受一个单词,并且通过一次更改一个字母,最终应该在指定的步数内达到目标单词。起初我一直在尝试寻找相似之处,例如:如果找到了这个词,而目标词丢失了,我的程序将分 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
两者之间的区别在于第一个检查 find 的每个变体与来自 lost 的字母。含义:lind、fand、fisd 和 Fine。然后,如果它使用查找功能找到一个工作单词,它会调用该新找到的单词的 changeling。
与我的新程序相反,它检查字母表中每个字母的 find 的每个变体。
我似乎无法让这段代码工作。我通过简单地打印 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
这是完美的!请注意,字母表中的每个字母都至少经过我的话一次。现在,我的程序所做的是使用一个辅助函数来确定该单词是否在我给定的字典中。
考虑到这一点,我现在收到多个合法的单词,而不是像我的第一个程序那样,除了当我执行 word=foundword 时,这意味着我每次都替换前一个单词。这就是我尝试 holderlist.append(word) 的原因。
我认为我的问题是我需要更改来遍历 holderlist 中的每个单词,但我不知道该怎么做。虽然这只是猜测。
任何帮助将不胜感激,
干杯。
【问题讨论】:
-
find如何到达lose。你能解释一下它背后的逻辑吗?如何更改每个单词.. -
我会给你看我的原始代码。
-
@Nichols 我更喜欢你先写一个关于单词如何变化的简短描述。这将很容易帮助我们。至少如果你能描述你的算法,那很简单!
-
我刚刚添加了我的原始编码,并且还描述了它是如何工作的!
标签: python list recursion append alphabet