Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active November 14, 2015 17:32
Show Gist options
  • Save joshbode/0da2ccbbb1b2eeff2724 to your computer and use it in GitHub Desktop.
Save joshbode/0da2ccbbb1b2eeff2724 to your computer and use it in GitHub Desktop.
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