Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 7046234

Browse files
committed
30187: count -> number_of_occurrences_of_letter
1 parent 5ec24db commit 7046234

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

src/sage/combinat/words/finite_word.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@
225225
from sage.rings.all import Integer, Infinity, ZZ, QQ
226226
from sage.sets.set import Set
227227

228+
from sage.misc.superseded import deprecated_function_alias
229+
228230
class FiniteWord_class(Word_class):
229231
def __str__(self):
230232
r"""
@@ -567,13 +569,15 @@ def content(self, n=None):
567569
sage: w.content()
568570
[0, 1, 0, 1]
569571
"""
572+
from collections import Counter
573+
c = Counter(self)
570574
if n is not None:
571575
alphabet = range(1,n+1)
572576
elif not self.parent().alphabet().cardinality() == +Infinity:
573577
alphabet = self.parent().alphabet()
574578
else:
575-
alphabet = sorted(self.letters())
576-
return [self.count(i) for i in alphabet]
579+
alphabet = sorted(c.keys())
580+
return [Integer(c[a]) for a in alphabet]
577581

578582
def is_yamanouchi(self, n=None):
579583
r"""
@@ -4624,6 +4628,33 @@ def nb_subword_occurrences_in(self, other):
46244628
# return only the number of occurrences of self
46254629
return occ[-1]
46264630

4631+
def number_of_occurrences_of_letter(self, letter):
4632+
r"""
4633+
Return the number of occurrences of ``letter`` in ``self``.
4634+
4635+
INPUT:
4636+
4637+
- ``letter`` - a letter
4638+
4639+
OUTPUT:
4640+
4641+
- integer
4642+
4643+
EXAMPLES::
4644+
4645+
sage: Word('abbabaab').number_of_occurrences_of_letter('a')
4646+
4
4647+
sage: Word('abbabaab').number_of_occurrences_of_letter('ab')
4648+
0
4649+
4650+
.. SEEALSO::
4651+
4652+
:meth:`sage.combinat.words.finite_word.FiniteWord_class.nb_factor_occurrences_in`
4653+
4654+
"""
4655+
return Integer(sum(1 for a in self if a == letter))
4656+
count = deprecated_function_alias(30187, number_of_occurrences_of_letter)
4657+
46274658
def _return_words_list(self, fact):
46284659
r"""
46294660
Return the return words as a list in the order they appear in the word.
@@ -5415,17 +5446,6 @@ def iterated_left_palindromic_closure(self, f=None):
54155446
f = WordMorphism(f)
54165447
return f(self).reversal().iterated_right_palindromic_closure(f=f)
54175448

5418-
def count(self, letter):
5419-
r"""
5420-
Count the number of occurrences of ``letter`` in ``self``.
5421-
5422-
EXAMPLES::
5423-
5424-
sage: Word('abbabaab').count('a')
5425-
4
5426-
"""
5427-
return Integer(sum(1 for a in self if a == letter))
5428-
54295449
def balance(self):
54305450
r"""
54315451
Return the balance of ``self``.

src/sage/combinat/words/word_datatypes.pyx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ cdef class WordDatatype_list(WordDatatype):
273273

274274
__add__ = __mul__
275275

276-
def count(self, a):
276+
def number_of_occurrences_of_letter(self, a):
277277
r"""
278278
Returns the number of occurrences of the letter ``a`` in the word
279279
``self``.
@@ -289,13 +289,17 @@ cdef class WordDatatype_list(WordDatatype):
289289
EXAMPLES::
290290
291291
sage: w = Word([0,1,1,0,1])
292-
sage: w.count(0)
292+
sage: w.number_of_occurrences_of_letter(0)
293293
2
294-
sage: w.count(1)
294+
sage: w.number_of_occurrences_of_letter(1)
295295
3
296-
sage: w.count(2)
296+
sage: w.number_of_occurrences_of_letter(2)
297297
0
298298
299+
.. SEEALSO::
300+
301+
:meth:`sage.combinat.words.finite_word.FiniteWord_class.nb_factor_occurrences_in`
302+
299303
"""
300304
return self._data.count(a)
301305

@@ -607,7 +611,7 @@ cdef class WordDatatype_str(WordDatatype):
607611

608612
__add__ = __mul__
609613

610-
def count(self, letter):
614+
def number_of_occurrences_of_letter(self, letter):
611615
r"""
612616
Count the number of occurrences of ``letter``.
613617
@@ -622,15 +626,27 @@ cdef class WordDatatype_str(WordDatatype):
622626
EXAMPLES::
623627
624628
sage: w = Word("abbabaabababa")
625-
sage: w.count('a')
629+
sage: w.number_of_occurrences_of_letter('a')
626630
7
627-
sage: w.count('b')
631+
sage: w.number_of_occurrences_of_letter('b')
628632
6
629-
sage: w.count('c')
633+
sage: w.number_of_occurrences_of_letter('c')
630634
0
631635
636+
::
637+
638+
sage: w.number_of_occurrences_of_letter('abb')
639+
0
640+
641+
.. SEEALSO::
642+
643+
:meth:`sage.combinat.words.finite_word.FiniteWord_class.nb_factor_occurrences_in`
644+
632645
"""
633-
return self._data.count(letter)
646+
if len(letter) == 1:
647+
return self._data.count(letter)
648+
else:
649+
return 0
634650

635651
def split(self, sep=None, maxsplit=None):
636652
r"""

src/sage/combinat/words/word_generators.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,12 @@ def standard_factorization(self):
290290
break
291291
u = v
292292
w1, w2 = self[:index+1], self[index+1:]
293-
return Factorization([LowerChristoffelWord(w1.count(1),w1.count(0)),
294-
LowerChristoffelWord(w2.count(1),w2.count(0))])
293+
w10 = w1.number_of_occurrences_of_letter(0)
294+
w11 = w1.number_of_occurrences_of_letter(1)
295+
w20 = w2.number_of_occurrences_of_letter(0)
296+
w21 = w2.number_of_occurrences_of_letter(1)
297+
return Factorization([LowerChristoffelWord(w11,w10),
298+
LowerChristoffelWord(w21,w20)])
295299

296300
def __reduce__(self):
297301
r"""

0 commit comments

Comments
 (0)