jeudi 27 mai 2010

Generate all possible words from a number and a phone keyboard

Each key on the telephone represents a list of letters. Given a telephone number, please write a program to output all the possible strings the telephone number represents.

E.g.
phone number has 8 digits
every key has 3 letters
nb of possible strings: 3^8


recursive approach:

// n = list of digits
// get_letters return a list of characters associated to a given digit (actually returns a list of singleton lists)
rec (n)
if n = []
return []
else
letters = get_letters(n[0])
res = []
for l in letters:
res.append(
l.extend(rec ( right-hand_side (n) ) ) )
return res



Aucun commentaire:

Enregistrer un commentaire