Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Alphabet and Recursion
    primarykey
    data
    text
    <p>I'm almost done with my program, but I've made a subtle mistake. My program is supposed to take a word, and by changing one letter at a time, is eventually supposed to reach a target word, in the specified number of steps. I had been trying at first to look for similarities, for example: if the word was find, and the target word lose, here's how my program would output in 4 steps:</p> <pre><code>['find','fine','line','lone','lose] </code></pre> <p>Which is actually the output I wanted. But if you consider a tougher set of words, like Java and work, the output is supposed to be in 6 steps. </p> <pre><code>['java', 'lava', 'lave', 'wave', 'wove', 'wore', 'work'] </code></pre> <p>So my mistake is that I didn't realize you could get to the target word, by using letters that don't exist in the target word or original word. </p> <p>Here's my Original Code:</p> <pre><code>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 </code></pre> <p>Here's my current code:</p> <pre><code>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 </code></pre> <p>The differences between the two is that the first checks every variation of find with the letters from lose. Meaning: lind, fond, fisd, and fine. Then, if it finds a working word with the lookup function, it calls changeling on that newfound word.</p> <p>As opposed to my new program, which checks every variation of find with every single letter in the alphabet. </p> <p>I can't seem to get this code to work. I've tested it by simply printing what the results are of find: </p> <pre><code>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) </code></pre> <p>This gives:</p> <pre><code>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 </code></pre> <p>Which is perfect! Notice that each letter in the alphabet goes through my word at least once. Now, what my program does is use a helper function to determine if that word is in a dictionary that I've been given.</p> <p>Consider this, instead of like my first program, I now receive multiple words that are legal, except when I do word=foundword it means I'm replacing the previous word each time. Which is why I'm trying holderlist.append(word).</p> <p>I think my problem is that I need changeling to run through each word in holderlist, and I'm not sure how to do that. Although that's only speculation.</p> <p>Any help would be appreciated,</p> <p>Cheers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload