Chopping Words
July 3, 2012
A simple word game starts with a word and repeatedly removes a single letter from the word, forming a new word, until there are no letters left. The act of removing a letter is called chopping and the resulting list of words is a chopping chain. For instance:
planet → plane → plan → pan → an → a
Your task is to write a program that takes a word and returns a list of all possible chopping chains that can be formed from the word, given a suitable dictionary. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] today’s Programming Praxis exercise, our goal is to reduce a word one letter at a time, where each step […]
My Haskell solution (see http://bonsaicode.wordpress.com/2012/07/03/programming-praxis-chopping-words/ for a version with comments):
Not terribly pretty, but it does the job. I end up with more chopping chains than the above, only because of my using
/usr/share/dict/words
.Builds a tree of possible chops, then “flattens” it to obtain a list of all possible paths from root to nodes.
This would of course benefit a lot from memoization, since we’re doing again and again the same chops recursively.
Python:
‘chain()’ returns a generator that returns each chain, one at a time.
Use a tree to map all the words in dictionary
http://codepad.org/XBwJufSS
[…] One more challenge from Programming Praxis’ Word Games today (there are only a few left!). This time we have the challenge of cutting off bits of words, one letter at a time, such that each step is still a word. […]
Here’s my solution in Racket: Chopping Words
I went a step beyond what the problem strictly asked and returned a nested tree-like structure with all possible chains of chopped words. Using the recursive solution that I was, it really wasn’t much harder than just returning a single solution and it still runs rather quickly.
Here’s a sample run for
PLANET
: