Skip to content

Instantly share code, notes, and snippets.

@ZSendokame
Created February 10, 2024 01:21
Show Gist options
  • Save ZSendokame/e1fd046c90eb6f4649a7c6ebbef9c6cd to your computer and use it in GitHub Desktop.
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
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