Skip to content

Instantly share code, notes, and snippets.

@Julisam
Created May 30, 2021 14:47
Show Gist options
  • Save Julisam/68cc75602be4c90ee4ff1cee52f105c1 to your computer and use it in GitHub Desktop.
Save Julisam/68cc75602be4c90ee4ff1cee52f105c1 to your computer and use it in GitHub Desktop.
AlgorithmFriday_Week8
# First define the list of chars for all digits
num_dict = {'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'],
# just incase 0 and 1 is part of input, we can safely skip
'0': [''], '1': [''],}
# using a recursive approach
def all_combinations(num):
if num==None: return []
try:
num = str(num)
if len(num)==0: return []
if len(num)==1: return num_map[num]
# function that returns comb of only 2 digits
def comb_2(prev, next): return [x+y for x in prev for y in next]
prev = num_dict[num[0]]
for i in num[1:]:
# print(prev)
prev = comb_2(prev, num_dict[i])
return prev
except:
return []
all_combinations(23)
@Julisam
Copy link
Author

Julisam commented Jun 4, 2021

Thank you @meekg33k.
I'm so excited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment