Skip to content

Instantly share code, notes, and snippets.

@joshbode
Created November 15, 2015 15:08
Show Gist options
  • Save joshbode/0369a7f016d0686c046c to your computer and use it in GitHub Desktop.
Save joshbode/0369a7f016d0686c046c to your computer and use it in GitHub Desktop.
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