Last active
January 24, 2017 23:37
-
-
Save eugenius1/9281dd50c19fecc6e50e7335a169295a to your computer and use it in GitHub Desktop.
All subsets of a list
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
def all_subsets(arr): | |
"""Returns a list of all subsets of a given list. | |
Mathematically: a set of all subsets of a set | |
Notes: | |
- Duplicates are treated distinct, i.e no duplicate checking or filtering. | |
- Recursive implementation. | |
""" | |
set_length = len(arr) | |
if set_length == 0: | |
return [[]] | |
else: | |
subsets = all_subsets(arr[1:]) | |
return subsets + [sub + [arr[0]] for sub in subsets] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment