Created
February 10, 2024 01:21
-
-
Save ZSendokame/e1fd046c90eb6f4649a7c6ebbef9c6cd to your computer and use it in GitHub Desktop.
Return all the combinations of a set of characters. ['A', 'B', 'C'], 2 -> AA, AB, AC, BA, BB, BC, CA, CB, CC
This file contains 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
def combinations(letters: list, length: int = 4, string = '', result = []) -> list[str]: | |
if length == len(string): | |
result.append(string) | |
return | |
for letter in letters: | |
combinations(letters, length, string + letter, result) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment