Snack Break Problem #12
Posted on March 31, 2016 by Shawnyg
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