Skip to content

Instantly share code, notes, and snippets.

@Grissess
Created December 17, 2015 16:14
Show Gist options
  • Save Grissess/30494d7ef1073a20f6bb to your computer and use it in GitHub Desktop.
Save Grissess/30494d7ef1073a20f6bb to your computer and use it in GitHub Desktop.
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