Unknown
Unknown

Reputation: 676

Two functions in a recursive functions?

I almost done my coding for a specific program i have. I just need help with one final piece. The program takes a word and changes one letter to more closely resemble a target word. The changed word must exist within a dictionary that I've been given.

my function is as such:

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

        return holderlist
    if len(word)!=len(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"


                                holderlist.append(items)
                                flatten_result(holderlist,target)



                                holderlist.append(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)
                                flatten_result(holderlist,target)

                                holderlist.append(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)
                                flatten_result(holderlist,target)

                                holderlist.append(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)

                                holderlist.append(changeling(items,target,steps-1))

                        else:
                            return None

                        i+=1

    return holderlist

my helper function is this:

def flatten_result(nested_list, target):
    if not nested_list:
        return None
    for word, children in zip(nested_list[::2], nested_list[1::2]):
        if word == target:
            return [word]
        children_result = flatten_result(children, target)
        if children_result:
            return [word] + children_result
    return None

the flatten_result function allows me to represent lists within lists as a single lists, and also backtracks through my program. How do I implement flatten result within changeling? I can only do it in the python shell as of yet.

Upvotes: 0

Views: 237

Answers (1)

georg
georg

Reputation: 214969

Basically,

def word_chain(chain_so_far, target, dictionary):
    last_word = chain_so_far[-1]
    if last_word == target:
        print chain_so_far
        return True
    for word in dictionary:
        if have_one_different_letter(word, last_word) and word not in chain_so_far:
            word_chain(chain_so_far + [word], target)

Call it like this word_chain(['love'], 'hate', your dict). Let us know if you need help with have_one_different_letter().

Upvotes: 1

Related Questions