200,525 views
8 votes
8 votes
Phone numbers and PIN codes can be easier to remember when you find words that spell out the number on a standard phone keypad. For example, instead of remembering the combination 2633, you can just think of CODE. Write a recursive function that, given a number, yields all possible spellings (which may or may not be real words).

User Publicmat
by
2.6k points

1 Answer

24 votes
24 votes

Answer:

Step-by-step explanation:

The following is written in Python and the code first creates a simulated keypad with all the characters. Then the function detects the number passed as an input argument and outputs all the possible spelling combinations.

keypad = {

'2': 'abc',

'3': 'def',

'4': 'ghi',

'5': 'jkl',

'6': 'mno',

'7': 'pqrs',

'8': 'tuv',

'9': 'wxyz',

}

def word_numbers(number):

number = str(number)

wordList = ['']

for char in number:

letters = keypad.get(char, '')

wordList = [prefix+letter for prefix in wordList for letter in letters]

return wordList

Phone numbers and PIN codes can be easier to remember when you find words that spell-example-1
User Ishan Bhatt
by
3.4k points