18.6 Combinations

Module: sage.combinat.combination

Combinations

Module-level Functions

Combinations( mset, [k=None])

Returns the combinatorial class of combinations of mset. If k is specified, then it returns the combintorial class of combinations of mset of size k.

The combinatorial classes correctly handle the cases where mset has duplicate elements.

sage: C = Combinations(range(4)); C
Combinations of [0, 1, 2, 3]
sage: C.list()
[[],
 [0],
 [1],
 [2],
 [3],
 [0, 1],
 [0, 2],
 [0, 3],
 [1, 2],
 [1, 3],
 [2, 3],
 [0, 1, 2],
 [0, 1, 3],
 [0, 2, 3],
 [1, 2, 3],
 [0, 1, 2, 3]]
 sage: C.count()
 16

sage: C2 = Combinations(range(4),2); C2
Combinations of [0, 1, 2, 3] of length 2
sage: C2.list()
[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]
sage: C2.count()
6

sage: Combinations([1,2,2,3]).list()
[[],
 [1],
 [2],
 [3],
 [1, 2],
 [1, 3],
 [2, 2],
 [2, 3],
 [1, 2, 2],
 [1, 2, 3],
 [2, 2, 3],
 [1, 2, 2, 3]]

Class: Combinations_mset

class Combinations_mset
Combinations_mset( self, mset)

TESTS:

sage: C = Combinations(range(4))
sage: C == loads(dumps(C))
True

Functions: count,$ \,$ iterator

count( self)

TESTS:

sage: Combinations([1,2,3]).count()
8
sage: Combinations(['a','a','b']).count()
6

iterator( self)

TESTS:

sage: Combinations(['a','a','b']).list() #indirect doctest
[[], ['a'], ['b'], ['a', 'a'], ['a', 'b'], ['a', 'a', 'b']]

Special Functions: __contains__,$ \,$ __init__,$ \,$ __repr__

__contains__( self, x)

sage: c = Combinations(range(4))
sage: all( i in c for i in c )
True
sage: [3,4] in c
False
sage: [0,0] in c
False

__repr__( self)

TESTS:

sage: repr(Combinations(range(4)))
'Combinations of [0, 1, 2, 3]'

Class: Combinations_msetk

class Combinations_msetk
Combinations_msetk( self, mset, k)

TESTS:

sage: C = Combinations([1,2,3],2)
sage: C == loads(dumps(C))
True

Functions: count,$ \,$ iterator

count( self)

Returns the size of combinations(mset,k). IMPLEMENTATION: Wraps GAP's NrCombinations.

sage: mset = [1,1,2,3,4,4,5]
sage: Combinations(mset,2).count()
12

iterator( self)

sage: Combinations(['a','a','b'],2).list() # indirect doctest
[['a', 'a'], ['a', 'b']]

Special Functions: __contains__,$ \,$ __init__,$ \,$ __repr__

__contains__( self, x)

sage: c = Combinations(range(4),2)
sage: all( i in c for i in c )
True
sage: [0,1] in c
True
sage: [0,1,2] in c
False
sage: [3,4] in c
False
sage: [0,0] in c
False

__repr__( self)

TESTS:

sage: repr(Combinations([1,2,2,3],2))
'Combinations of [1, 2, 2, 3] of length 2'

Class: Combinations_set

class Combinations_set

Functions: iterator,$ \,$ rank,$ \,$ unrank

iterator( self)

sage: Combinations([1,2,3]).list() #indirect doctest
[[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]

rank( self, x)

sage: c = Combinations([1,2,3])
sage: range(c.count()) == map(c.rank, c)
True

unrank( self, r)

sage: c = Combinations([1,2,3])
sage: c.list() == map(c.unrank, range(c.count()))
True

Class: Combinations_setk

class Combinations_setk

Functions: iterator,$ \,$ list,$ \,$ rank,$ \,$ unrank

iterator( self)

Posted by Raymond Hettinger, 2006/03/23, to the Python Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474124

sage: Combinations([1,2,3,4,5],3).list() # indirect doctest
[[1, 2, 3],
 [1, 2, 4],
 [1, 2, 5],
 [1, 3, 4],
 [1, 3, 5],
 [1, 4, 5],
 [2, 3, 4],
 [2, 3, 5],
 [2, 4, 5],
 [3, 4, 5]]

list( self)

sage: Combinations([1,2,3,4,5],3).list()
[[1, 2, 3],
 [1, 2, 4],
 [1, 2, 5],
 [1, 3, 4],
 [1, 3, 5],
 [1, 4, 5],
 [2, 3, 4],
 [2, 3, 5],
 [2, 4, 5],
 [3, 4, 5]]

rank( self, x)

sage: c = Combinations([1,2,3], 2)
sage: range(c.count()) == map(c.rank, c.list())
True

unrank( self, r)

sage: c = Combinations([1,2,3], 2)
sage: c.list() == map(c.unrank, range(c.count()))
True

Special Functions: _iterator,$ \,$ _iterator_zero

_iterator( self, items, len_items, n)

An iterator for all the n-combinations of items.

sage: it = Combinations([1,2,3,4],3)._iterator([1,2,3,4],4,3)
sage: list(it)
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

_iterator_zero( self)

An iterator which just returns the empty list.

sage: it = Combinations([1,2,3,4,5],3)._iterator_zero()
sage: list(it)
[[]]

See About this document... for information on suggesting changes.