// Thomas Dybdahl Ahle (https://github.com/thomasahle)
// Source: http://compprog.wordpress.com/2007/10/15/generating-the-partitions-of-a-set
if not set_:
yield ()
return
for i in xrange(2**len(set_) / 2):
parts = [set(), set()]
for item in set_:
After Change
// Handle iterators.
seq = list(seq)
if tuples:
for partition in partitions1( seq ):
// Convert the partition into a list of sorted tuples.
partition = map(tuple, map(sorted, partition))
// Convert the partition into a sorted tuple of sorted tuples.
// Sort by smallest parts first, then lexicographically.
partition = tuple(sorted(partition, cmp=len_cmp))
yield partition
else:
for partition in partitions1( seq ):
partition = frozenset( map(frozenset, partition) )
yield partition
def ordered_partitions(seq, tuples=False):
Generates ordered partitions of elements in `seq`.