Sunday Puzzle April 24, 2022

Posted on

Animal Sound to Color

This week’s challenge comes from listener Jeff Balch, of Evanston, Ill. Name a sound made by a certain animal. Change one letter in it to the next letter of the alphabet, and you’ll get a color associated with that animal. What’s the sound, and what’s the color?

Link to the challenge
Changing Each Letter in an Animal Sound to the Next Letter in the Alphabet

Discussion

This was a super easy one. I was able to get a list of color names from Wikipedia and a list of animal sounds from a site called ‘VisualDictionary.org‘. I actually solved it just by glancing at the list of sounds, but elected to write the code for fun.

  • File handling
  • String manipulation
  • Loops
  • Indexing strings

Python Methods Used

Screen shot showing the solution printed in the Terminal Pane

Here’s the Code

# Build a list of color names
colorNames = []
fName = "C:\\Users\\Randy\\Documents\\Puzzles\\Data\\ColorNames.txt"
fHandle = open(fName, 'rt', encoding="utf-8")
for aLine in fHandle:
colorNames.append(aLine.strip().lower())

# Open the list of animal sounds and manipulate each entry
fName = "C:\\Users\\Randy\\Documents\\Puzzles\\Data\\AnimalSounds.txt"
fHandle = open(fName, 'rt')
for aLine in fHandle:
# Make it the same lower case as the color names, remove the newline character:
sound = aLine.lower().strip()
for i in range(len(sound)):
ltr = sound[i]
# convert the letter to integer, add 1, convert back to character:
nextLtr = chr(ord(ltr) + 1)
candidate = sound[0:i] + nextLtr + sound[i+1:]
if candidate in colorNames:
print(sound + ' ' + candidate)

A Few Comments

The basic algorithm is:

  • Load the color names, from file, into a list called ‘colorNames
  • Open the animal sounds file and process each line: for aLine in fHandle:
    1. We will build candidate words that might be colors, but in order to check if a candidate matches a color in the list, we need to make sure the candidate is the same case as the list, i.e. lower case. Remember, two strings are different if they use different case, for example, ‘Pink’ ≠ ‘pink’
    2. Also, when we read a line, the end-of-line marker is embedded in the string, so we use ‘strip’ to clean it out. The following code does both things in one line: sound = aLine.lower().strip()
    3. Every time we read a line from the animal sounds file, we loop through the letters in that sound
    4. This code does that: for i in range(len(sound)):
    5. We use indexing to grab each letter ltr = sound[i]
    6. The instructions state: “Change one letter in it to the next letter of the alphabet”,
      1. We do that with nextLtr = chr(ord(ltr) + 1)
      2. ‘Ord’ converts a character to its numeric representation. For example,
        • ord(‘o’) = 111
        • ord(‘p’) = 112
        • ord(‘i’) = 105
      3. ‘chr’ converts the number back into a letter. The line of code above does the following conversions:
        1. ord(‘o’) = 111
        2. 111 + 1 = 112
        3. chr(112) = ‘p’
    7. Now that we have manipulated the letter to get the next letter in the alphabet, we build a candidate color name by concatenating the first part of the animal sound, the manipulated letter, and the last part of the word: candidate = sound[0:i] + nextLtr + sound[i+1:]
    8. Finally, we check if our candidate is actually a color name by seeing whether it exists in our list: if candidate in colorNames:

Summary

The code works because there is only one sound that maps to a color name, so I don’t need to check whether the color is “associated with that animal”. The basic algorithm is to load the color names into a list, loop through the sounds file, manipulating each sound to make a candidate which we evaluate by checking if it is in our list.

Since the code is so short, I don’t have a download this time. Just copy the code above if you want to play with it.

Leave a comment