Project: Telephone-Number Word Generator Modification

(Project: Telephone-Number Word Generator Modification) If you have a computerized dictionary available, modify the program you wrote in Exercise 11.13 to look up the words in the dictionary. Some seven-letter combinations created by this program consist of two or more words (the phone number 843-2677 produces “THEBOSS”).

Exercise 11.13

(Telephone-Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following

table:

The Correct Answer and Explanation is:

Certainly! Let’s walk through a solution and explanation for Exercise 11.13 and its modification, involving a telephone-number word generator that checks against a dictionary.


🔢 Phone Keypad Mapping

Here’s how letters are mapped to digits 2–9 on a standard keypad:

2 → ABC  
3 → DEF  
4 → GHI  
5 → JKL  
6 → MNO  
7 → PQRS  
8 → TUV  
9 → WXYZ

Modified Solution (Python Program)

This version includes a dictionary check:

from itertools import product

# Mapping from digits to letters
digit_to_letters = {
    '2': 'ABC', '3': 'DEF',
    '4': 'GHI', '5': 'JKL',
    '6': 'MNO', '7': 'PQRS',
    '8': 'TUV', '9': 'WXYZ'
}

# Load a dictionary file into a set
def load_dictionary(file_path):
    with open(file_path, 'r') as file:
        words = {line.strip().upper() for line in file}
    return words

# Generate all letter combinations for a phone number
def generate_combinations(phone_number):
    groups = [digit_to_letters[d] for d in phone_number if d in digit_to_letters]
    return (''.join(p) for p in product(*groups))

# Check combinations against dictionary
def find_valid_words(phone_number, dictionary):
    matches = []
    for word in generate_combinations(phone_number):
        # Check full word
        if word in dictionary:
            matches.append(word)
        # Check for two-word splits
        for i in range(1, len(word)):
            if word[:i] in dictionary and word[i:] in dictionary:
                matches.append(f"{word[:i]} {word[i:]}")
    return matches

# Example usage
if __name__ == "__main__":
    phone_number = "8432677"
    dictionary = load_dictionary("dictionary.txt")  # Provide path to dictionary file
    results = find_valid_words(phone_number, dictionary)

    print("Valid words or combinations:")
    for result in results:
        print(result)

📖 Explanation (300+ Words)

The problem involves converting a 7-digit phone number (like 8432677) into all possible letter combinations based on the telephone keypad and then filtering them against an English dictionary to find meaningful words or combinations of words.

Each digit from 2 to 9 corresponds to multiple letters (e.g., 2 → A, B, C). Given that each digit can map to 3 or 4 letters, a 7-digit number could theoretically yield over 2,000 combinations. To generate all those combinations, we use the itertools.product function, which computes the Cartesian product of input letter groups. For example, 8432677 translates to letter groups:

  • 8 → TUV
  • 4 → GHI
  • 3 → DEF
  • 2 → ABC
  • 6 → MNO
  • 7 → PQRS
  • 7 → PQRS

Once the combinations are generated, we check each one against a preloaded dictionary. We use a set to store dictionary words for fast look-up (O(1) time complexity per lookup). We check each 7-letter string in two ways:

  1. Is the full 7-letter word in the dictionary?
  2. Can it be split into two smaller valid words? (e.g., “THEBOSS” → “THE” + “BOSS”)

This split is handled with a simple loop, checking each possible point of division (from 1 to 6), and seeing whether both segments are in the dictionary.

This approach allows the program to identify not just single words but also two-word combinations hidden in the phone number, enhancing the utility of the word generator. For example, 8432677 could yield “THEBOSS”, which is meaningful and memorable—ideal for a vanity phone number.

By combining algorithmic generation with dictionary validation, this modified program provides both computational efficiency and practical relevance.

Scroll to Top