Beat the Puzzle Master Home
Beat the Puzzle Master!
A web site dedicated to solving the NPR Sunday Puzzle

Puzzle September 14, 2008

Podcast of the original challenge

At the REC Center

Didn't get enough puzzle challenge Sunday? Try these
Challenge: Each answer is a familiar two-word phrase in which the first word ends in R-E and the second word starts with C. For example, if the clue is "text that comes from the Associated Press or Reuters," the answer would be "wire copy."

Clue:  
Word 1: Word 2:

 

How you can Solve the Puzzle With Code

From Eric Berlin of Milford, Conn.:

Take the phrases "move over" and "local call." In each case, the last three letters of the first word are the first three letters in the next. Name a familiar animal, in two words, in which the last three letters of the first word are the first three letters of the next. Hints: It's a furry, four-footed animal that can grow up to 6 feet in length. The first word in its name has five letters, the second word has eight.

How you can Solve the Puzzle With Code

This one isn't hard at all if you can just get the right list of animals! From Will's clue, we know the animal in question is a mammal, since Will mentioned fur and the distinguishing characteristics of mammals are hair, milk and four chamber hearts. So that simplifies the problem of getting our animal list.

Once we have the list of animals, all we have to do is
  1. Traverse our list to identify with animals having two word names.....
  2. that also have lengths five and eight, respectively
  3. If any of our pairs have a second word that starts with the last three letters of the first, we've got a solution!

Here's the Easy Part

So, if you can follow C# code, the items above are easy to translate into code:

//Open the file for reading:
using (FileStream fs = new FileStream(ANIMALS_FILE, 
                FileMode.Open, FileAccess.Read)) {
    using (StreamReader sr = new StreamReader(fs)) {
        while (sr.Peek() != -1) {
            string aLine = sr.ReadLine();
            //use the splig function to get the first and last word
            string[] words = aLine.Split(' ');
            if (words.Length == 2) {
                //retrieve the last 3 letters
                string last3 = words[0].Substring(words[0].Length - 3);
                if (words[1].StartsWith(last3, 
                        StringComparison.CurrentCultureIgnoreCase))
                    //Score!
                    lstResults.Items.Add(aLine);
            }
        }
    }
}

Getting the Animal List

Anteater

Once again, Wikipedia has the best list available. Of course, it is spread out across 22 different pages, so we'll definitely want to automate our download. Fortunately, I've done this many times while solving Will's puzzles, so it isn't that bad any more!

If this is the first time you've seen one of my puzzle solutions, the basic idea is to:
  1. Write a spider/robot that goes to Wikipedia's master page of mammal categories
  2. (You can learn how reading my article on writing spiders )
  3. Programmatically identify the links on the master page and visit each in succession
  4. To wit, the Wikipedia master page has links to each order of mammal, such as order Pilosa (anteaters and sloths) and we need to visit each of those "child" pages.
  5. On each child page, grab the page contents and match it against a regular expression that corresponds to the name of a mammal. Take a look at my article on using regular expressions for this purpose.
So, since the techniques are old hat, at least on this site, the main thing to discuss here is the regular expressions that I used. Here's the regular expression that matches a link to an order of mammals on Wikipedia:
<p><a                                 #Literal match against the markup
\s                                    #whitespace
href="                                #literal match against their markup
(/wiki/List_of_placental_mammals_     #Paren starts the group; literal
[^"]+)                                #Match anything except a close quote

If you're familiar with HTML markup and regular expressions, you should immediately realize that this is an extremely simple regex. Most of the regex is matching literal text that looks like a link to a page whose address starts with '/wiki/List_of_placental_mammals_', has some additional words, and ends with a quote mark. For each mammalian order whose page we want to visit, this regex will return a match group (the group is in the parentheses) and we can use that group to programmatically jump to the order in question and scrape that child screen.

The second regular expression matches animal common names, found on the "child page" for their order. The Wikipedia HTML markup we are trying to match looks like this:

<li><a href="/wiki/Giant_Anteater" title="Giant Anteater">
Giant Anteater</a> (<i><a href="/wiki/Myrmecophaga_tridactyla" 
title="Myrmecophaga tridactyla" class="mw-redirect">
Myrmecophaga tridactyla</a></i>)</li>
</ul>
Obviously, the part we are interested in is the part I highlighted above. Since there are many pieces of text on the page, we look for the one that is preceeded by the markup highlighted above, and ending with a parentheses. There are other links on the page, but only the mammal names are followed by a parentheses. The regular expression to perform this match is:
<li><a             #Literal match against the Wikipedia markup
\s                 #Whitespace
href=              #More literal matching
[^>]+              #Match anything except an angle bracket
>                  #Literal match against the close tag of the link
([^<]+)            #Start a group; match anything except <
</a>               #Close tag on the link
\s                 #Whitespace
\(                 #Match a parentheses

Once you have these two regular expressions in place, it is easy to utilize them in conjunction with the techniques discussed in my articles on writing your own spider and on file IO techniques to build your list of mammals.


For more detail, download my C# Solution

Solve the Problem in Code!

By clicking the button, you will run a small javascript program in your browser to solve the problem. It needs to fetch the list from the server, and then it should solve the problem in a few seconds. If you're curious, inspect the page source code! (Search for "function mammalsFetched".)






Reader Comments

Be the first to comment!

Add a Comment Show comment controls
Comment Details
Your Comment:

Name:
Password:
Your Name*:
Password*:
Confirm*:
Email*: (will not be published!)
  Your Gravatar image
Website?
About Me: