Created
November 15, 2015 15:08
-
-
Save joshbode/0369a7f016d0686c046c 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 subset(elements): | |
"""Generate subsets.""" | |
if not elements: | |
yield [] | |
return | |
head = elements[0] | |
for tail in subset(elements[1:]): | |
yield tail | |
yield [head] + tail | |
x = [1, 2, 3] | |
print(list(subset(x))) | |
def sub_set(head, tail=None): | |
"""Generate subsets.""" | |
if not head: | |
return | |
if tail is None: | |
tail = set() | |
yield tail | |
head = set(head) | |
while head: | |
t = tail.union({head.pop()}) | |
yield t | |
yield from (set(y) for y in sub_set(head, t)) | |
print(list(sub_set(x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment