Puzzle November 30, 2008
Every Sunday, PuzzleMaster Will Shortz broadcasts an intriguing puzzle, and whoever solves it
gets to play a fresh puzzle on-air. I try to solve the original puzzle with the
elegance of programming,
and post my code here. For your amusement, I also create my own slightly different
version of Will's challenge for you to test your
skill.
Learn new coding skills, or prove yourself against my puzzle; either way, enjoy
the site!
Sure Bet You'll Know
For each word given, add a letter at the start, the end or somewhere inside to name
a well-known U.S. city. For example, given the word "flit," the answer would be "Flint."
Your answer?
How to Solve Will's Puzzle With Code
From listener Henry Hook of Brooklyn, N.Y.: Think of the name of a lawbreaker that
starts with S. Remove the S and one other letter, and the remaining letters, in order,
will name another lawbreaker.
Code Techniques:
-
string manipulation
-
dictionary lookup
-
simple loops
Rating: easy!
Hardest part: getting the list
Duck soup! My electronic gum-shoe work pumped lead into that whodunit. Which is kinda good,
on account of how last week Will made me grab some sky! I got myself a list of good fellas,
wise guys, flimflam artists, outlaws, torpedoes and various villains, gave them all the third degree,
and spat out an answer.
Algorithm Summary
-
Build a dictionary of criminal names such as
'cutpurse' and 'stickup artist'
-
I chose to use a dictionary (a type of hash table in .NET) which is quite easy
to check whether or not something, such as a criminal name, is in the dictionary
-
Look at each class of criminal (iterate the dictionary) and construct candidate variatons according to Will's instructions.
s.
-
Check whether any candidate is in our criminals dictionary, if so, we have a solution!
The most interesting part was getting a list of lawbreakers. I had to build my own list,
and it was fun dealing with various slang terms such as goons, gunsels, grifters etc.
My favorite site was a list of criminal slang from Dashiel Hammet, Phillip Marlowe et. al.,
Twists, Slugs and Roscoes: A Glossary of Hardboiled Slang.
Together with
a few other sites, I managed to compile a list of 113 terms for miscellaneous made men
and molls. If you
download my code, you can take a look at the text file that resulted.
Take a look at this code sample, which is probably as easy to read as any
explanation:
//Examine each entry in the dictionaryn the dictionary
foreach (KeyValuePair<string, char> criminalPair in criminalsDic) {
string criminal = criminalPair.Key;
//if it has an 's', do something
int p = criminal.IndexOf("s");
if (p >= 0) {
//build all the possible combinations of the word by 1)removing the 's' and
//2) removing other potential letters
for (int i = 0; i < criminal.Length - 1; i++) {
string candidate = criminal.Remove(p, 1).Remove(i, 1);
if (criminalsDic.ContainsKey(candidate)) {
lstResults.Items.Add(candidate + " - " + criminal);
}
}
}
}
I ommitted the code to load the dictionary, since it was really simple. Here's a
few comments on the code above:
-
The following code looks for a criminal name having 's' in its name:
int p = criminal.IndexOf("s");
if (p >= 0) {...
-
It is very easy to construct variations on the word using the
Remove method, which simply removes the letter we don't want
-
Once we have made candidate by removing letters, we check whether it is in our list with
if (criminalsDic.ContainsKey(candidate)), a test which evaluates as
true if the candidate exists in the dictionary.
Click to download my C#
solution code,
including the list of lawbreakers, found in the bin/debug subdirectory.
Got a solution in your own language? Post a comment to that effect and I
may post your code on line.
Solve the Problem Now!
From listener Henry Hook of Brooklyn, N.Y.: Think of the name of a lawbreaker
that starts with S. Remove the S and one other letter, and the remaining
letters, in order, will name another lawbreaker.
By clicking the button, you will run a small javascript program in your browser to
solve the problem. It needs to fetch the lawbreaker 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 dataFetched".)
Take-Home Challenge!
Can you can write your own code to solve a similar problem? Think of a name for criminal,
involving fraud. Drop a 'B', add an 'L', and rearrange the letters to create
another kind of criminal.
Post a comment if you can do it.
If you like, you can
modify my code to accomplish this purpose; in any event, if you use my
download,
you can use my text files for the criminal list.
Reader Comments