簡體   English   中英

列表遞歸

[英]Recursion with Lists

我在使用程序時遇到麻煩,該程序需要一個單詞,並且一次更改一個字母,然后將該單詞轉換為目標單詞。 不過,請記住,根據我給的單詞詞典,轉換后的單詞必須是合法單詞。

我在弄清楚如何使其遞歸方面遇到麻煩。 該程序對其必須采取的步驟數量有所限制。

編輯:我不允許使holderlist全球。

到目前為止,我的代碼:

def changeling(word,target,steps):
    holderlist=[]
    i=0
    if steps<0 and word!=target:
        return None

    if steps!=-1:
        for items in wordList:
            if len(items)==len(word):
                i=0

                if items!=word:
                    for length in items:

                        if i==1:
                            if items[1]==target[1] and items[0]==word[0] and items[2:]==word[2:]:
                                if items==target:
                                    print "Target Achieved"
                                    holder.list.append(target)
                                holderlist.append(items)
                                changeling(items,target,steps-1)

                        elif i>0 and i<len(word)-1 and i!=1:
                            if items[i]==target[i] and items[0:i]==word[0:i] and items[i+1:]==word[i+1:]:
                                if items==target:
                                    print "Target Achieved"
                                holderlist.append(items)
                                changeling(items,target,steps-1)

                        elif i==0:
                            if items[0]==target[0] and items[1:]==word[1:]:
                                if items==target:
                                    print "Target Achieved"
                                holderlist.append(items)
                                changeling(items,target,steps-1)

                        elif i==len(word)-1:
                            if items[len(word)-1]==target[len(word)-1] and items[0:len(word)-1]==word[0:len(word)-1]:
                                if items==target:
                                    print "Target Achieved"
                                holderlist.append(items)
                                changeling(items,target,steps-1)
                        else:
                            changeling(None,None,steps-1)

                        i+=1

    return holderlist

我最大的問題是,每次嘗試使程序遞歸時,都會刷新我的持有人清單。

如果手動輸入數據,我可以解決。 這是我希望程序執行的操作:

changeling("find","lose",4)
gives me:
['fine','fond']
the program should then do:
changeling('fine','lose',3)
gives me:
['line']
changeling('line','lose',2)
gives me:
['lone']
changeling('lone','lose',1)
gives me:
['lose']
Target Achieved

也許像

def distx(w1,w2):
    if len(w1) != len(w2):return 100000
    score=0
    for i in range(len(w1)):
       score += int(w1[i] != w2[i])
    return score


word_list = ["fine","fond","line","lose","lone"]

def changeling(guess,target,steps):
    my_steps = []
    print "Guess:",guess
    if target == guess:return [guess]
    try:word_list.remove(guess)
    except:pass
    my_steps.append(guess)
    if target != guess and steps >= 0:
        this_step = []
        one_step_away = [w for w in word_list if distx(guess,w) == 1]
        for k  in one_step_away:
            print "     %s->"%guess,k
            this_step.append(changeling(k,target,steps-1))
    my_steps.append( this_step )
    return my_steps
tmp = changeling("find","lose",4)
print tmp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM