Last active
November 14, 2015 17:32
-
-
Save joshbode/0da2ccbbb1b2eeff2724 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(head, tail=None): | |
"""Generate subsets.""" | |
if not head: | |
return | |
if tail is None: | |
tail = [] | |
yield [] | |
head = head[:] | |
while head: | |
t = tail + [head.pop(0)] | |
yield from subset(head, t) | |
yield t | |
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 = head.copy() | |
while head: | |
t = tail.union({head.pop()}) | |
yield from (set(y) for y in sub_set(head, t)) | |
yield t | |
print(list(sub_set(x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment