Task
Old mobile phones had the ability to type characters by pressing a number. The letter a
could be typed by pressing 2
once. The letter b
could be typed by pressing 2
twice.
Given a sequence of numbers, give all possible letter combinations.
For example: The number 23
could give an output ad, ae, af, bd, be, bf, cd, ce, cf
My recursive solution to this problem is given below.
def num_to_char(value):
if value == 2: return ["a","b","c"]
if value == 3: return ["d","e","f"]
if value == 4: return ["g","h","i"]
if value == 5: return ["j","k","l"]
if value == 6: return ["m","n","o"]
if value == 7: return ["p","q","r","s"]
if value == 8: return ["t","u","v"]
if value == 9: return ["w","x","y","z"]
def convert_num(number, current_string = ""):
if number == []:
print(current_string)
return
get_list = num_to_char(int(number[0]))
for character in get_list:
current_string += character
convert_num(number[1:], current_string)
current_string = current_string[:-1]
num_to_covert = list("234")
convert_num(num_to_covert)