2012-03-28 88 views
3

我幾乎完成了我的程序,但我犯了一個微妙的錯誤。我的程序應該記錄一個字,並且一次更改一個字母,最終應該按照指定的步數達到目標字。我起初一直試圖尋找相同點,例如:如果字是發現,目標字輸了,這裏是如何我計劃將在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每個字跑,我不知道該怎麼做。儘管這只是猜測。

任何幫助,將不勝感激,

乾杯。

+1

「find」如何達到「丟失」。你能解釋它背後的邏輯嗎?如何更改每個單詞.. – Surya 2012-03-28 18:04:36

+0

我會告訴你我的原始代碼。 – Unknown 2012-03-28 18:06:20

+0

@Nichols我寧願你寫一個關於這個單詞如何改變的小說明。這將幫助我們輕鬆。至少如果你能描述你的算法,那它很簡單! – Surya 2012-03-28 18:09:31

回答

6

我可能會對你需要的有些困惑,但是借用this post我相信我有一些應該有幫助的代碼。

>>> alphabet = 'abcdefghijklmnopqrstuvwxyz' 
>>> word = 'java' 
>>> splits = [(word[:i], word[i:]) for i in range(len(word) + 1)] 
>>> splits 
[('', 'java'), ('j', 'ava'), ('ja', 'va'), ('jav', 'a'), ('java', '')] 
>>> replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b] 
>>> replaces 
['aava', 'bava', 'cava', 'dava', 'eava', 'fava', 'gava', 'hava', 'iava', 'java', 'kava', 'lava', 'mava', 'nava', 'oava', 'pava', 'qava', 'rava', 'sava', 'tava', 'uava', 'vava', 'wav 
a', 'xava', 'yava', 'zava', 'java', 'jbva', 'jcva', 'jdva', 'jeva', 'jfva', 'jgva', 'jhva', 'jiva', 'jjva', 'jkva', 'jlva', 'jmva', 'jnva', 'jova', 'jpva', 'jqva', 'jrva', 'jsva', ' 
jtva', 'juva', 'jvva', 'jwva', 'jxva', 'jyva', 'jzva', 'jaaa', 'jaba', 'jaca', 'jada', 'jaea', 'jafa', 'jaga', 'jaha', 'jaia', 'jaja', 'jaka', 'jala', 'jama', 'jana', 'jaoa', 'japa' 
, 'jaqa', 'jara', 'jasa', 'jata', 'jaua', 'java', 'jawa', 'jaxa', 'jaya', 'jaza', 'java', 'javb', 'javc', 'javd', 'jave', 'javf', 'javg', 'javh', 'javi', 'javj', 'javk', 'javl', 'ja 
vm', 'javn', 'javo', 'javp', 'javq', 'javr', 'javs', 'javt', 'javu', 'javv', 'javw', 'javx', 'javy', 'javz'] 

一旦你擁有所有可能的內容替換的列表,你可以簡單地做

valid_words = [valid for valid in replaces if lookup(valid)] 

這應該給你,可以在單詞替換1個字符構成的所有單詞。通過將這些代碼放置在一個單獨的方法中,您可以接受一個單詞,從當前單詞中獲取可能的下一個單詞,並對每個單詞進行遞歸。例如:

alphabet = 'abcdefghijklmnopqrstuvwxyz' 
def next_word(word): 
    splits = [(word[:i], word[i:]) for i in range(len(word) + 1)] 
    replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b] 
    return [valid for valid in replaces if lookup(valid)] 

這是否有足夠的幫助?我認爲你的代碼可以通過將任務分成更小的塊來獲益。

+0

從技術上講,你的代碼與我的代碼做的是一樣的,我不太清楚如何讓我的程序調用列表中的每個單詞。 – Unknown 2012-03-28 23:10:20

+0

因爲我不能稱之爲改變(替換,「失去」,步驟)。我也嘗試過:換句話說:替換(單詞,目標,步驟-1) – Unknown 2012-03-28 23:11:00

+0

他們可能在技術上做同樣的事情,但你的代碼試圖在同一時間做太多。你需要將事情分解,以便你明確重現的目標。話雖如此,當您嘗試在替換中爲每個單詞調用換位(單詞,目標,步驟-1)時會發生什麼?這是正確的軌道。很快,您會遇到在檢查下一級別的任何單詞(深度優先與寬度優先搜索)之前是否檢查某個級別的所有單詞的問題。 – 2012-03-28 23:15:45

0

固定代碼:

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. 
     raise BaseException("Starting word and target word not the same length: %d and %d" % (len(word), 
    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(pos_word, target, steps-1) for pos_word in holderlist] 
    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 

len(word)len(target),它會是更好的提高比打印的東西晦澀異常,W/O堆棧跟蹤和非致命的。

哦和反斜槓(\),而不是正斜槓(/),用於繼續行。並且他們對評論不起作用