Created
May 30, 2021 14:47
-
-
Save Julisam/68cc75602be4c90ee4ff1cee52f105c1 to your computer and use it in GitHub Desktop.
AlgorithmFriday_Week8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @meekg33k.
I'm so excited