The task: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
and my solution:
import itertools
letters_stack = list('abcdefghijklmnopqrstuvwxyz')
keypad_dict = {}
for num in range(2, 10):
size = 3
if num in [7, 9]:
size = 4
keypad_dict[str(num)] = letters_stack[:size]
letters_stack = letters_stack[size:]
def aux(numbers):
if len(numbers) == 1:
return keypad_dict[numbers[0]]
return itertools.product(keypad_dict[numbers[0]], iletters(numbers[1:]))
def iletters(numbers):
assert len(numbers) > 0
return [''.join(x) for x in aux(numbers)]
print list(iletters('234'))
# ['adg', 'adh', 'adi', 'aeg', 'aeh', 'aei', 'afg', 'afh', 'afi', 'bdg', 'bdh', 'bdi', 'beg', 'beh', 'bei', 'bfg', 'bfh', 'bfi', 'cdg', 'cdh', 'cdi', 'ceg', 'ceh', 'cei', 'cfg', 'cfh', 'cfi']