This exercise has two parts. In the first, somewhat minor, part
you will draw memory pictures related to lists. The second part
is the more interesting one, in which you will write a recursive
algorithm to solve a word puzzle.
Part I:
In Part I, you will work with lists of lists to see how they
work and to make sure you understand the details:
- Start by downloading ListOfLists.java.
Read through the code to see how one can make a list that itself
contains lists.
- Draw as complete a memory picture as you can after each of
the first three lists are created. Then, draw the memory picture
after the three lists have been added to the fourth (the list of lists).
Thus, you will submit four separate drawings.
Part II:
In this part, you will write code to solve Word-Morphs (sometimes
also called Word-Ladders). Here's an example of a Word-Morph:
lead -> load -> goad -> gold
In this example, by changing only one letter at a time, we transform
the word lead into the word gold. Here's another
example that morphs black into white:
black -> brack -> brace -> trace -> trice -> trite -> write -> white
The first one used three steps (morphs) to change lead
into gold, while the second used seven steps to morph
black into white.
Try this one: can you change dog into cat? How many
steps did you need?
Now for the program you will write:
- Your code will take as input two words, a start word
and an end word, and a number numSteps.
You are to identify all possible morphs from the first word to the
second word involving exactly numSteps steps.
- Start by downloading WordMorph.java.
This already has some test code and some comments indicating where
you need to add your code. Naturally, most of the real work will
be done in a recursive method (that you will write).
- You will also need WordTool.java
and the dictionary words.
- Once you have the tests working, you can run your code along
with a GUI. For that purpose, download
WordMorphGUI.java into the same
directory and execute that. You should not need to modify this
program. You will notice that it simply calls the appropriate
methods in WordMorph.java.
Next, let's look a little deeper into the details:
- First, look at the outline of the method offByOne()
in the class WordMorph. This method should take two
words (String's) and determine whether you can
morph from one to the other. This means they have to be
of the same length and differ by exactly one letter.
Clearly, this is going to be a useful method, so you should
write and test this method early on. Then set it
aside and move on to the main problem (where you will end
up using offByOne()).
- Second, let's think about what we want the output to be like.
For example, consider transform east into west
using four steps.
Here are two ways that work:
east -> past -> pest -> jest -> west
east -> fast -> last -> lest -> west
It turns out that there are 113 such morphs, among which are
the above two. Since each morph is a list of words, we will
use a linked-list for each morph. Thus, we will place
"east", "past", "pest", "jest" and "west" in a linked-list.
Then, we will place "east", "fast", "last", "lest" and "west"
in another linked list. Eventually, we will have 113
such linked-lists. How do we return a collection of
113 linked-lists? Using another linked list of course!
This is why, the return value of findLinks() is
a list of lists. Thus, each time you identify
a complete morph, you should put the morph into a list,
and add that list to the list-of-lists.
- Note that the list-of-lists is called results
in the code and has already been created for you.
- Next, examine the method findLinks(). This is the
method called from outside (in main(), for example)
to provide. This method takes the three relevant parameters:
startWord, endWord and numSteps.
- Now let's think about the algorithm itself: how do we
find the morphs? The idea is to search recursively
in a manner similar to permutation-seating. Given
a start-word, and an end-Word, we can scour the
dictionary for words that are "off-by-one" (a single morph away).
These words are possible next words towards the end-word,
which one can check recursively. Then, every time we
reach the end-word, we'll have found a chain of such words.
The trick is to keep the partial chain of words (in a list!)
and know when to add and remove (un-do) a putative word
in the chain. This description is deliberately not
very precise: after all, the purpose is that you
should be creating the algorithm.
- Important: The wrong way to do this program
is to write out code and see if it works. The right way
is to first write the algorithm down in pseudocode.
Indeed, you are required to submit the pseudocode.
Please spend time thinking about the pseudocode and getting
it right. Once you've done that, the coding will be relatively easy.
- Note that there is a copy() method included in the
code. You might find it useful when you reach the end word.
Submission:
- For Part I, you will need to submit memory-picture drawings.
- For Part II, write all your code in the same file:
WordMorph.java and submit that, along with the other
Java files and the dictionary (the words file) in your
encrypted jar-file. Note that you will need to show that you've
tested your implementation of offByOne(). You will also
need to submit your pseudocode (on paper).
- As usual, submit your file using these
submission instructions.
What else is due: