Created
November 29, 2021 21:29
-
-
Save les-peters/efaa868de2edd8c5c18c12aa2bbd657d to your computer and use it in GitHub Desktop.
Telephone Texting
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
| question = """ | |
| Given a string containing digits from 2-9, return all possible letter combinations | |
| that the number could represent based on phone numbers/letters. For example, | |
| 2 could be a, b, or c, 3 could be d, e, or f, and so on. | |
| Example: | |
| $ phoneLetter('9') | |
| $ ['w', 'x', 'y', 'z'] | |
| $ phoneLetter('23') | |
| $ ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'] | |
| """ | |
| def phoneLetter(string): | |
| key_to_letter = {} | |
| key_to_letter['2'] = [ 'a', 'b', 'c' ] | |
| key_to_letter['3'] = [ 'd', 'e', 'f' ] | |
| key_to_letter['4'] = [ 'g', 'h', 'i' ] | |
| key_to_letter['5'] = [ 'j', 'k', 'l' ] | |
| key_to_letter['6'] = [ 'm', 'n', 'p' ] | |
| key_to_letter['7'] = [ 'p', 'q', 'r', 's' ] | |
| key_to_letter['8'] = [ 't', 'u', 'v' ] | |
| key_to_letter['9'] = [ 'w', 'x', 'y', 'z' ] | |
| output = [] | |
| for ch in string: | |
| if len(output) == 0: | |
| for letter in key_to_letter[ch]: | |
| output.append(letter) | |
| else: | |
| new = [] | |
| for letter in key_to_letter[ch]: | |
| for added in list(map(lambda x: x + letter, output)): | |
| new.append(added) | |
| output = new | |
| return output | |
| print(phoneLetter('9')) | |
| print(phoneLetter('23')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment