Created
June 23, 2015 22:16
recursive all subsets
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_rec(left, so_far, accum): | |
if len(left) == 0: | |
accum.append(so_far) | |
else: | |
# find subsets that include first item of left | |
all_subsets_rec(left[1:], [left[0]] + so_far, accum) | |
# find subsets that do NOT include first item of left | |
all_subsets_rec(left[1:], so_far, accum) | |
def all_subsets(items): | |
accum = [] | |
all_subsets_rec(items, [], accum) | |
return accum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out the
powerset
recipe from the standard library: https://docs.python.org/3/library/itertools.html#itertools-recipesTo match the order your function produces, you can call
reversed
onrange
.