Problem of the Day
A new programming or logic puzzle every Mon-Fri

Telephone Words

Each number on the telephone dial (except 0 and 1) corresponds to three alphabetic characters. Those correspondences are:

2 ABC  
3 DEF  
4 GHI  
5 JKL 
6 MNO 
7 PRS 
8 TUV 
9 WXY

Given a 7 digit phone number, print all possible words that can be formed. For bonus points write the program to solve for N digit phone numbers. Here is a dictionary to help you out (English).

Permalink: http://problemotd.com/problem/telephone-words/

Comments:

  • Heroka - 9 years, 8 months ago

    Generally, 7 and 9 correspond to 4 letters (7: PQRS and 9 WXYZ). Was the omission of Q and Z intentional for the question?

    reply permalink

  • Anonymous - 9 years, 8 months ago

    #dialpad.py
    
    def f(x):
        return {
            0: [" "],
            1: [" "],
            2: ['a', 'b', 'c'],
            3: ['d', 'e', 'f'],
            4: ['g', 'h', 'i'],
            5: ['j', 'k', 'l'],
            6: ['m', 'n', 'o'],
            7: ['p', 'q', 'r', 's'],
            8: ['t', 'u', 'v'],
            9: ['w', 'x', 'y', 'z']
        }[x]
    
    #get phone number and determine length
    phonenumber = raw_input("Enter a phone number> ")
    numberlength = len(phonenumber)
    
    phonelist = list(phonenumber)
    
    wordlist = []
    
    for item in phonelist:
        wordlist.append(f(int(item)))
    
    potentials = []
    
    
    file = open('WordsEn.txt', 'r')
    
    for line in file:
    
        if len(line) == (numberlength+1):
    
            j = 0
    
            while line[j] in f(int(phonelist[j])):
    
                if j == numberlength - 1:
                    potentials.append(line[0:numberlength])
                    break;
    
                j = j + 1   
    
    print potentials
    

    output: Enter a phone number> 6275377 ['markers', 'maskers', 'napless', 'oarless']

    reply permalink

Content curated by @MaxBurstein