Scripting Helpers is winding down operations and is now read-only. More info→
← Blog Home

Snack Break Problem #12

Hello scripting enthusiasts! We're back with another Snack Break. This week's task is: Word chains. You have to write a function that, given a start and end word, can create a chain of real words, only changing one letter each time.


Break-Down

Say if a person gave a word, "cat" as the start and "dog" as the end, the script should then print, "cat, cot, dot, dog", which are all real words. As stated above, it'll can only change ONE letter per cycle. We have provided a list of real words for you to use:


local WordList = require(390477136) -- `WordList` is a function that takes one argument, the length of the words that should be returned in a table. local list = WordList(4) --`list` now references a table containing many string values, all of which are words with the given length.

list is a table which contains words of the given length, in no particular order. Your function should return all words it computed. Ex

function test(start, finish)
    local chain = {start}

    -- code here

    -- puts all real worlds in the table
    return table.concat(chain, ', ')
end

print(test("cat", "dog")) -- could print "cat, cot, dot, dog"

Remember that efficiency is a factor, in both your program and the actual chain of words itself. This means that the chain should try to be the shortest possible chain of words from the start word to the end word.

Elite Challenge

Modify the function so that it can also create chains of words with varying lengths, so that it supports a start and end word that aren't the same length (ex: the words "dog" and "color").

In this case, in each cycle, you can either modify a letter (replace), add a single letter (anywhere in the word), or remove a letter (anywhere in the word).

Remember that every word in the word chain must still be an actual word from the word list.

Conclusion

I will be publishing the answer next week Monday, or possibly a bit earlier to bring a new Snack Break! Those who email me with their answer will receive a shoutout on the next Snack Break related post I make!

Last Week's Snack Break

Last week's challenge was a real brain teaser! YellowTide was the intelligent mind who solved it, and the solution can be found here.

Be sure to follow our twitter to stay up to date!

Commentary

Leave a Comment

YellowoTide says: March 31, 2016
sweet
SatireIncarnate says: March 31, 2016
Is this only a substitution, and not including adding or removing letters?
XAXA says: April 4, 2016
In the example where "dog" and "color" are given, what would happen if it is impossible to get from dog to color? If it is possible, what would be a sample output?
SatireIncarnate says: April 5, 2016
I generated a dictionary for Lua consumption ( https://preview.c9users.io/lunate/skyewashere/dict2.txt ) but it couldn't find a solution for dog -> color
einsteinK says: April 11, 2016
I've written some nice code that, when given "dog" and "color", returns "dog, cog, col, colo, color". It actually generates instructions as "replace d at position 1 with c, replace g..., add o at pos 4, ..." (also has remove). I can give it any 2 words and it'll make a path. Of course it doesn't use real words, although I could check for the generated output if they are actually words.