Created
December 17, 2015 16:14
-
-
Save Grissess/30494d7ef1073a20f6bb to your computer and use it in GitHub Desktop.
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 permute(choices): | |
if not choices: | |
yield () | |
else: | |
for item in choices[0]: | |
for suffix in permute(choices[1:]): | |
yield (item,) + suffix | |
# A unique object that is not a member of any possible powerset | |
NON_MEMBER = object() | |
# Another sensible seq_class is set, but duplicate sets might be generated | |
def powerset(members, seq_class = tuple): | |
choices = tuple((NON_MEMBER, i) for i in members) | |
for permutation in permute(choices): | |
yield seq_class(filter(lambda i: i is not NON_MEMBER, permutation)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment