As the crossword editor at the New York Times, Will Shortz is the world's most famous puzzle expert. On February 19th, 2012 he presented the following puzzle during the puzzle segment of NPR's Weekend Edition:
The word marten, as in the animal, consists of the beginning letters of Mississippi, Arkansas, Texas, and New Mexico. And you can actually drive from Mississippi to Arkansas to Texas to New Mexico, in that order. What is the longest common English word you can spell by taking the beginning letters of consecutive states in order, as you travel through them? My answer has eight letters. Maybe you can better mine. The longest answer will win.
The winner of this challenge was Roger Buch of Louisville Kentucky. Here is an excerpt from the transcript of the next week's show in which Mr. Buch describes his approach to solving the problem:
SHORTZ: Hey, Roger. Congratulations. BUCH: Hello, Will. Thanks. Glad to finally talk with you. MARTIN: So, Roger, you sent in the word omissions as your answer. How did you figure it out? BUCH: A lot of time staring at a globe with North America facing me. MARTIN: Really? BUCH: My globe has been sitting by my sofa for a week now. Any commercial when I'm watching TV, looking over that globe, tracing my finger around the country to come up with another word.
Mr. Buch is clearly not a computer scientist. No self-respecting computer scientist would solve this problem by hand when it would clearly be faster (and more fun) to write a program to solve it for us.
Your goal for this assignment is to write a Java program that solves exactly the problem described above. Notice that the problem statement is ambiguous: it doesn't specify whether or not we are allowed to reenter states. For the purposes of this assignment we will assume that reentering states is not allowed.
StateGraph.java
(Documentation) - This class
represents a collection of State objects. State.java
(Documentation) - State objects
maintain a list of neighboring states. WordTree.java
(Documentation) - This is a
"dictionary" class that determines whether arbitrary strings
correspond to English words.
WordNode.java
- Helper class for WordTree
. (You shouldn't need to
interact with this class directly.) StateSpeller.java
(Documentation) (INCOMPLETE) This class will contain the search code. You must
complete the unfinished methods so that they are consistent with the
provided JavaDoc comments. words.txt
- the YAWL public domain word list containing 263,533 English words. It does not contain single-letter words or proper names.states.txt
- A text file containing a list of US states and their neighbors.
You must complete the unfinished methods in StateSpeller.java
without
changing the existing method declarations. You will need to add additional private methods to handle the recursive searches.
Rather than searching for the single longest admissible word, the
methods in StateSpeller.java
must return all solutions
longer than some minimum value. The return values are ArrayLists
of
Strings where each string represents a single solution.
Each solution string must contain three pieces of information: the word, the length of the word, and the corresponding list of states. Here is an example of a correctly formatted entry:
team 4 [Tennessee, Arkansas, Mississippi]Solution strings should not have any trailing spaces and should not be terminated with a newline. The solution strings may be returned in any order.
If the same word can be constructed using two separate state sequences, both should be returned. For example,
team 4 [Tennessee, Arkansas, Mississippi]and
team 4 [Tennessee, Arkansas, Missouri]are considered different solutions.
According to the instructions above, valid solutions may pick up any (non-zero) number of letters from each state. The code would be simpler if we only considered the first letter from each state name. Under that requirement "tam" would be a solution:
tam 3 [Tennessee, Arkansas, Missouri]but "team" would not qualify.
StateSpeller.java
includes methods for
both the original version of the problem and the single-letter
variation. You should solve the single-letter version first. Once you
have completed that version, the multi-letter version should be
relatively simple.
DirectorySizeCalculator
class in
the Experimenting
with Recursion lab. It may be helpful to take another look at
that code. ArrayList
is a reference type.
Returning references to instance variables can lead to confusing
bugs. You should return copies instead.
StateSpeller
class. In particular, you will probably
want to use an ArrayList
to keep track of which states have been
visited while constructing the current word: you can add a state to
the list when your search visits the state, and remove it from the
list when the search is finished with the state.ArrayList
of states to a string.
The ArrayList
toString
method will return a
correctly formatted string. StateSpeller.java
.