Created
March 2, 2012 03:29
-
-
Save begriffs/1955354 to your computer and use it in GitHub Desktop.
generate all set partitions
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 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