From Ken Marx, of Mount Vernon, NY:
Think of a 9-letter word with no repeated letters. The letters in the odd positions
(the 1st, 3rd, 5th, 7th and 9th positions) are vowels. All five vowels, A, E, I, O and U,
appear once each in some order. What word is this?
How to Solve the Puzzle With Code
This puzzle is quite easy if you have a good word list.
Basically, it's a matter of knowing your string functions.
To solve it, you must loop through your list of words and apply three tests to each:
-
Word length must be nine
-
The letters at the odd positions must be vowels
-
There can't be any repeated letters. Note that this simplifies the
requirement above, since if there are no repeated letters, then we also
know that each of the 5 vowels are used.
Vowels at the Odd Postions
Since checking the word length is trivial, we'll jump ahead and work on having vowels in the right
places. To do so, we need a loop that steps through the odd numbers, and we need to retrieve letters
from the odd position dictated by our loop.
In C# and other languages based on C syntax, such as Perl or Java, it is easy to construct a loop
that whips through the odd numbers, like so:
for (int i = 0; i < 9; i+=2)
This loop starts at 0 and adds two every time.
To fetch the letter at an odd position, we use the substring function, like this:
string letter = aLine.Substring(i, 1);
Now all we have to do is ensure the letter in question is a vowel and break out of our loop
if not. The complete code follows:
bool hasAll = true;
//look at letters in the odd positions; if any aren't a vowel, bail
for (int i = 0; i < 9; i+=2) {
string letter = aLine.Substring(i, 1);
if (letter != "a" && letter != "e" && letter
!= "i" && letter != "o" && letter != "u") {
hasAll = false;
break;
}
}
Determining Whether There are Repeated Letters
There are several ways to determine whether any repeated letters occur, but an easy method is to
sort the letters in the word and then loop through, looking at adjoining letters. Sorting
places duplicate letters next to each other, so if any two adjoining letters match, we know there are some repeated
letters
//In order to sort, convert to an array:
char[] allLetters = aLine.ToCharArray();
Array.Sort(allLetters);
char c = allLetters[0];
bool noRepeats = true;
for (int i = 1; i < 9; i++) {
if (allLetters[i] == c) {
noRepeats = false;
break;
}
c = allLetters[i];
}
At the end of the loop, our bool 'noRepeats' will reflect whether any letter is repeated.
For more detail,
download
the C# Solution