How to Solve the Puzzle With Code
From Ed Pegg Jr., of Mathpuzzle.com: "Nonclassical analysis" contains eight different letters.
Two of them occur once, two of them occur twice, two of them occur three times, and two of
them occur four times each. Which sports team's name has this same property?
That puzzle was quite pleasing, and not too hard either! Ed's letter pattern
corresponds to a recent event, and also forms a cool pattern.
The image below represents an array with a winning solution, since there are two 1's, two 2's, two
3's and two 4's. Our challenge is to build an array like this for every
sports team. (The team illustrated is the Philadelphia Phillies.)
To solve Ed's conundrum: build your own array of letter counts, with one slot for each letter
of the alphabet. Examine each team name and fill the array with its letter
counts. If any team has the correct totals, we've found our solution!
Tip: use the letter in question as an index to the array! Remember that letter 'a' is
represented by 97; b is 98, c is 99, etc.
(
ASCII representation of letters). So all we need do is subtract 97 from each letter and we've
created an index to our array
(assuming we are using a zero-based array where letter 'a' has index 0). The code
below is even shorter than my explanation!
foreach (char c in teamName) {
//ignore spaces, numbers, etc.
if (char.IsLetter(c)) {
//Increment the count, in our array, at letter index:
letterCounts[c - 97]++;
}
}
Note that I have an array named 'letterCounts'. Also, the code works because char
operates like a
number in C# (as well as a letter); you can do arithmetic on chars, or use them for indeces.
Did We Find a Winner?
After you run the code above on a team name, it is elementary to check if we
have the counts we need:
//Create variables to hold our counts:
int oneCount, twoCount, threeCount, fourCount;
oneCount = twoCount = threeCount = fourCount = 0;
//Loop through the letters in the alphabet (our array)
foreach (int aCount in letterCounts) {
if (aCount == 1) {
oneCount++;
} else if (aCount == 2) {
twoCount++;
} else if (aCount == 3) {
threeCount++;
} else if (aCount == 4) {
fourCount++;
} else if (aCount != 0) {
//No good; there is some letter with a differing count, such as 5
return false;
}
}
//We have a winner if there are exactly two of each!
bool haveWinner = oneCount == 2 && twoCount == 2
&& threeCount == 2 && fourCount == 2;
Get Yer Team Lists Right Here!
If you download my sample code, it includes the list I built. If you're curious,
again, I used Wikipedia for the pro sports team lists:
- http://en.wikipedia.org/wiki/National_Hockey_League
- http://en.wikipedia.org/wiki/List_of_current_NBA_team_rosters
- http://en.wikipedia.org/wiki/List_of_current_NFL_team_rosters
- http://en.wikipedia.org/wiki/List_of_Major_League_Baseball_teams_by_payroll
The pro teams were short, so I created their
text files manually by cutting-and-pasting.
I also got a great list of college football teams from http://prwolfe.bol.ucla.edu/cfootball/LinksList.html. However,
the college list has about 700 entries, so I whipped up some code to build my text file.
I used my standard techniques of:
-
Writing a
spider
to scrape the UCLA screen
-
Matching team names via a
regular expression
to extract them from the page markup, and
-
Employing some simple
File IO
to write the regular expression matches to a file we can use.
The only novel item here is my regular expression, which you can examine if you download my
solution code.
Take-Home Challenge!
I'm curious whether there are any words with a pattern of 1-1, 2-2, 3-3, i.e.
one letter repeated once, two letters repeated twice, and three letters repeated
three times. That pattern is even cooler than Ed's. So, puzzle-pros and
coder-comrades, take that as a challenge, and leave a comment with your
discovery. Should you accept my challenge, you can use a word list from one of
my sidebar references, such as Kevin's Word Lists.
Click
here
to download my sample C# code that solves the puzzle. If you have solved
it yourself using a language different than C#, leave a comment; I am considering adding a section
for users to share their own solutions.