Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created March 2, 2012 03:29
Show Gist options
  • Save begriffs/1955354 to your computer and use it in GitHub Desktop.
Save begriffs/1955354 to your computer and use it in GitHub Desktop.
generate all set partitions
def partitions(set_):
if not set_:
yield []
return
for i in xrange(2**len(set_)/2):
parts = [set(), set()]
for item in set_:
parts[i&1].add(item)
i >>= 1
for b in partitions(parts[1]):
yield [parts[0]]+b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment