Created
January 11, 2022 21:25
-
-
Save les-peters/7c4766fc23f5a22cf99ba500ba52033b to your computer and use it in GitHub Desktop.
Permutations
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 s, you can transform every letter individually to be lowercase or | |
| uppercase. Return a list of all possible permutations you could create from s. | |
| Example: | |
| $ capPermutations("ab2") | |
| $ ["ab2","aB2","Ab2","AB2"] | |
| $ capPermutations("35p") | |
| $ ["35p","35P"] | |
| """ | |
| import re | |
| char_p = re.compile(r'[a-zA-Z]') | |
| def capPermutations(string): | |
| perm_array = [] | |
| for ch in string: | |
| new_perm_array = [] | |
| char_m = char_p.search(ch) | |
| char_a = [] | |
| if char_m: | |
| char_a.append(ch.upper()) | |
| char_a.append(ch.lower()) | |
| else: | |
| char_a.append(ch) | |
| if len(perm_array): | |
| for perm in perm_array: | |
| for char in char_a: | |
| new_perm_array.append(perm + char) | |
| else: | |
| for char in char_a: | |
| new_perm_array.append(char) | |
| perm_array = new_perm_array | |
| return perm_array | |
| print(capPermutations("ab2")) | |
| print(capPermutations("35p")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment