summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTravis Scrimshaw <tscrim at ucdavis.edu>2014-06-12 09:29:41 -0700
committerTravis Scrimshaw <tscrim at ucdavis.edu>2014-06-12 09:29:41 -0700
commitc3979afa7d7a304abc5b8b9ba5d0e2f3c25f6984 (patch)
tree24627e28dcc6f041457fd31d97a8b3ee37d0ac22
parenttrac #10534 removed unused imports (diff)
Some review tweaks and python 3 stuff.
-rw-r--r--src/sage/combinat/choose_nk.py13
-rw-r--r--src/sage/combinat/combination.py6
-rw-r--r--src/sage/combinat/split_nk.py3
-rw-r--r--src/sage/combinat/subset.py26
-rw-r--r--src/sage/combinat/subword.py37
5 files changed, 49 insertions, 36 deletions
diff --git a/src/sage/combinat/choose_nk.py b/src/sage/combinat/choose_nk.py
index 9366979..dd30ded 100644
--- a/src/sage/combinat/choose_nk.py
+++ b/src/sage/combinat/choose_nk.py
@@ -29,6 +29,9 @@ def ChooseNK(n, k):
The elements of the output are tuples of Python int (and not Sage Integer).
+ This was deprecated in :trac:`10534` for :func:`Combinations`
+ (or ``itertools.combinations`` for doing iteration).
+
EXAMPLES::
sage: from sage.combinat.choose_nk import ChooseNK
@@ -51,10 +54,11 @@ def ChooseNK(n, k):
# sage.combinat.subset. It might be good to move them somewhere else.
def rank(comb, n, check=True):
"""
- Returns the rank of comb in the subsets of range(n) of size k.
+ Return the rank of ``comb`` in the subsets of ``range(n)`` of size ``k``
+ where ``k`` is the length of ``comb``.
The algorithm used is based on combinadics and James McCaffrey's
- MSDN article. See: http://en.wikipedia.org/wiki/Combinadic
+ MSDN article. See: :wikipedia:`Combinadic`.
EXAMPLES::
@@ -84,6 +88,11 @@ def rank(comb, n, check=True):
Traceback (most recent call last):
...
ValueError: comb must be a subword of (0,1,...,n)
+
+ sage: choose_nk.rank([1,2], 3)
+ 2
+ sage: choose_nk.rank([0,1,2], 3)
+ 0
"""
k = len(comb)
if check:
diff --git a/src/sage/combinat/combination.py b/src/sage/combinat/combination.py
index 3a32c55..f2a7066 100644
--- a/src/sage/combinat/combination.py
+++ b/src/sage/combinat/combination.py
@@ -217,7 +217,7 @@ class Combinations_mset(CombinatorialClass):
sage: repr(Combinations(range(4)))
'Combinations of [0, 1, 2, 3]'
"""
- return "Combinations of %s" % self.mset
+ return "Combinations of {}".format(self.mset)
def __iter__(self):
"""
@@ -334,7 +334,7 @@ class Combinations_msetk(CombinatorialClass):
sage: repr(Combinations([1,2,2,3],2))
'Combinations of [1, 2, 2, 3] of length 2'
"""
- return "Combinations of %s of length %s" % (self.mset, self.k)
+ return "Combinations of {} of length {}".format(self.mset, self.k)
def __iter__(self):
"""
@@ -363,7 +363,7 @@ class Combinations_msetk(CombinatorialClass):
12
"""
items = map(self.mset.index, self.mset)
- return ZZ(gap.eval("NrCombinations(%s,%s)" % (items,ZZ(self.k))))
+ return ZZ(gap.eval("NrCombinations({},{})".format( items,ZZ(self.k))) )
class Combinations_setk(Combinations_msetk):
diff --git a/src/sage/combinat/split_nk.py b/src/sage/combinat/split_nk.py
index 61d98ae..93ae9f7 100644
--- a/src/sage/combinat/split_nk.py
+++ b/src/sage/combinat/split_nk.py
@@ -27,6 +27,9 @@ def SplitNK(n, k):
Returns the combinatorial class of splits of a the set range(n)
into a set of size k and a set of size n-k.
+ This was deprecated in :trac:`10534`. Use instead
+ :class:`OrderedSetPartitions`.
+
EXAMPLES::
sage: from sage.combinat.split_nk import SplitNK
diff --git a/src/sage/combinat/subset.py b/src/sage/combinat/subset.py
index cb49045..2284c81 100644
--- a/src/sage/combinat/subset.py
+++ b/src/sage/combinat/subset.py
@@ -281,7 +281,7 @@ class Subsets_s(Parent):
sage: repr(Subsets([1,2,3])) #indirect doctest
'Subsets of {1, 2, 3}'
"""
- return "Subsets of %s" %self._s
+ return "Subsets of {}".format(self._s)
def __contains__(self, value):
"""
@@ -362,8 +362,8 @@ class Subsets_s(Parent):
"""
k = ZZ_0
while k <= self._s.cardinality():
- for ss in Subsets_sk(self._s, k):
- yield ss
+ for ss in Subsets_sk(self._s, k)._fast_iterator():
+ yield self.element_class(ss)
k += 1
def random_element(self):
@@ -410,13 +410,13 @@ class Subsets_s(Parent):
if sub not in Sets():
ssub = Set(sub)
if len(sub) != len(ssub):
- raise ValueError("repeated elements in %s"%sub)
+ raise ValueError("repeated elements in {}".format(sub))
sub = ssub
try:
index_list = sorted(self._s.rank(x) for x in sub)
except (ValueError,IndexError):
- raise ValueError("%s is not a subset of %s"%(
+ raise ValueError("{} is not a subset of {}".format(
Set(sub), self._s))
n = self._s.cardinality()
@@ -457,7 +457,7 @@ class Subsets_s(Parent):
Workaround for returning non elements.
See the extensive documentation in
- :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet`.
+ :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet.__call__`.
TESTS::
@@ -544,7 +544,7 @@ class Subsets_sk(Subsets_s):
Subsets_s.__init__(self, s)
self._k = Integer(k)
if self._k < 0:
- raise ValueError("the integer k (=%d) should be non-negative" % k)
+ raise ValueError("the integer k (={}) should be non-negative".format(k))
def _repr_(self):
"""
@@ -553,7 +553,7 @@ class Subsets_sk(Subsets_s):
sage: repr(Subsets(3,2)) #indirect doctest
'Subsets of {1, 2, 3} of size 2'
"""
- return Subsets_s._repr_(self) + " of size %s" % (self._k)
+ return Subsets_s._repr_(self) + " of size {}".format(self._k)
def __contains__(self, value):
"""
@@ -742,13 +742,13 @@ class Subsets_sk(Subsets_s):
n = self._s.cardinality()
if self._k != sub.cardinality() or self._k > n:
- raise ValueError("%s is not a subset of length %d of %s"%(
+ raise ValueError("{} is not a subset of length {} of {}".format(
sub, self._k, self._s))
try:
index_list = sorted(self._s.rank(x) for x in sub)
except ValueError:
- raise ValueError("%s is not a subset of length %d of %s"%(
+ raise ValueError("{} is not a subset of length {} of {}".format(
sub, self._k, self._s))
return choose_nk.rank(index_list, n)
@@ -880,7 +880,7 @@ class SubMultiset_s(Parent):
sage: S = Subsets([1, 2, 2, 3], submultiset=True); S #indirect doctest
SubMultiset of [1, 2, 2, 3]
"""
- return "SubMultiset of %s" % dict_to_list(self._d)
+ return "SubMultiset of {}".format(dict_to_list(self._d))
def __eq__(self, other):
r"""
@@ -1035,7 +1035,7 @@ class SubMultiset_s(Parent):
Workaround for returning non elements.
See the extensive documentation in
- :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet`.
+ :meth:`sage.sets.finite_enumerated_set.FiniteEnumeratedSet.__call__`.
TESTS::
@@ -1163,7 +1163,7 @@ class SubMultiset_sk(SubMultiset_s):
sage: repr(S) #indirect doctest
'SubMultiset of [1, 2, 2, 3] of size 3'
"""
- return "%s of size %s" % (SubMultiset_s._repr_(self), self._k)
+ return "{} of size {}".format(SubMultiset_s._repr_(self), self._k)
def __contains__(self, s):
"""
diff --git a/src/sage/combinat/subword.py b/src/sage/combinat/subword.py
index 3c2bff5..8af8d76 100644
--- a/src/sage/combinat/subword.py
+++ b/src/sage/combinat/subword.py
@@ -81,16 +81,16 @@ def _stringification(data):
def Subwords(w, k=None, element_constructor=None):
"""
- Returns the set of subwords of ``w``.
+ Return the set of subwords of ``w``.
INPUT:
- - ``w`` - a word (can be a list, a string, a tuple or a word)
+ - ``w`` -- a word (can be a list, a string, a tuple or a word)
- - ``k`` - an optional integer to specify the length of subwords
+ - ``k`` -- an optional integer to specify the length of subwords
- - ``element_constructor`` - an optional function that will be used to build
- the subwords.
+ - ``element_constructor`` -- an optional function that will be used
+ to build the subwords
EXAMPLES::
@@ -160,7 +160,7 @@ def Subwords(w, k=None, element_constructor=None):
class Subwords_w(Parent):
r"""
- Subwords of a given word
+ Subwords of a given word.
"""
def __init__(self, w, element_constructor):
"""
@@ -199,7 +199,7 @@ class Subwords_w(Parent):
def __reduce__(self):
r"""
- Pickle (how to construct back the object)
+ Pickle (how to construct back the object).
TESTS::
@@ -222,7 +222,7 @@ class Subwords_w(Parent):
sage: repr(Subwords([1,2,3])) # indirect doctest
'Subwords of [1, 2, 3]'
"""
- return "Subwords of %s" % repr(self._w)
+ return "Subwords of {!r}".format(self._w)
def __contains__(self, w):
"""
@@ -282,7 +282,7 @@ class Subwords_w(Parent):
def random_element(self):
r"""
- Return a random subword with uniform law
+ Return a random subword with uniform law.
EXAMPLES::
@@ -310,11 +310,12 @@ class Subwords_w(Parent):
sage: Subwords('123').list()
['', '1', '2', '3', '12', '13', '23', '123']
"""
- return itertools.chain(*[Subwords_wk(self._w,i,self._build) for i in range(0,len(self._w)+1)])
+ return itertools.chain(*[ Subwords_wk(self._w,i,self._build)
+ for i in range(len(self._w)+1) ])
class Subwords_wk(Subwords_w):
r"""
- Subwords with fixed length of a given word
+ Subwords with fixed length of a given word.
"""
def __init__(self, w, k, element_constructor):
"""
@@ -345,7 +346,7 @@ class Subwords_wk(Subwords_w):
def __reduce__(self):
r"""
- Pickle (how to construct back the object)
+ Pickle (how to construct back the object).
TESTS::
@@ -365,7 +366,7 @@ class Subwords_wk(Subwords_w):
sage: repr(Subwords([1,2,3],2)) # indirect doctest
'Subwords of [1, 2, 3] of length 2'
"""
- return "%s of length %s" % (Subwords_w.__repr__(self), self._k)
+ return "{} of length {}".format(Subwords_w.__repr__(self), self._k)
def __contains__(self, w):
"""
@@ -441,7 +442,7 @@ class Subwords_wk(Subwords_w):
def random_element(self):
r"""
- Return a random subword of given length with uniform law
+ Return a random subword of given length with uniform law.
EXAMPLES::
@@ -489,11 +490,11 @@ class Subwords_wk(Subwords_w):
def smallest_positions(word, subword, pos=0):
"""
- Returns the smallest positions for which subword appears as a
- subword of word. If pos is specified, then it returns the positions
- of the first appearance of subword starting at pos.
+ Return the smallest positions for which ``subword`` appears as a
+ subword of ``word``. If ``pos`` is specified, then it returns the positions
+ of the first appearance of subword starting at ``pos``.
- If subword is not found in word, then it returns False.
+ If ``subword`` is not found in ``word``, then return ``False``.
EXAMPLES::