1 | initial version |
Surprisingly, the answer is actually correct. As often, it is a matter of definition!
Let us walk through this.
Define a random word and give it a name:
sage: w = Words('ab', 20).random_element()
sage: w
baabaaaaaaabbabbbbba
Count for 'aa'
in w
:
sage: w.count('aa')
0
Since we may be surprised, let us read the documentation
for the count
method:
sage: w.count?
or its source code:
sage: w.count??
Oh, so count
counts the occurrences of letters.
Here, w
is a letter on the alphabet {'a', 'b'}
,
and 'aa'
is not a letter in that alphabet, so the count is zero.
Compare:
sage: w = Words(['a', 'b', 'aa'], 10).random_element()
sage: w
word: aa,aa,a,aa,a,a,a,b,a,aa
sage: w.count('aa')
4
So how do we count factors? or subwords?
sage: W = w.parent()
sage: f = W('aa')
sage: f.nb_factor_occurrences_in(w)
7
sage: f.nb_subword_occurrences_in(w)
55
Conclusion: the documentation for "count" should maybe
include this discussion, or at least point to the
nb_factor_occurrences
method.
This is now tracked at
2 | No.2 Revision |
Surprisingly, the answer is actually correct. As often, it is a matter of definition!
Let us walk through this.
Define a random word and give it a name:
sage: w = Words('ab', 20).random_element()
sage: w
baabaaaaaaabbabbbbba
Count for 'aa'
in w
:
sage: w.count('aa')
0
Since we may be surprised, let us read the documentation
for the count
method:
sage: w.count?
or its source code:
sage: w.count??
Oh, so count
counts the occurrences of letters.
Here, w
is a letter on the alphabet {'a', 'b'}
,
and 'aa'
is not a letter in that alphabet, so the count is zero.
Compare:
sage: w = Words(['a', 'b', 'aa'], 10).random_element()
sage: w
word: aa,aa,a,aa,a,a,a,b,a,aa
sage: w.count('aa')
4
So how do we count factors? or subwords?
Get hold of the set of words.
sage: W = w.parent()
sage: W
Finite words over {'a', 'b'}
Define the factor we are looking for:
sage: f = W('aa')
Count its occurrences as a factor or a subword in w
.
sage: f.nb_factor_occurrences_in(w)
7
sage: f.nb_subword_occurrences_in(w)
55
Conclusion: the documentation for "count" should maybe
include this discussion, or at least point to the
nb_factor_occurrences
method.
This is now tracked at
3 | No.3 Revision |
Surprisingly, the answer is actually correct. As often, it is a matter of definition!
Let us walk through this.
count
count?Define a random word and give it a name:
sage: w = Words('ab', 20).random_element()
sage: w
baabaaaaaaabbabbbbba
Count for 'aa'
in w
:
sage: w.count('aa')
0
Since we may be surprised, let us read the documentation
for the count
method:
sage: w.count?
or its source code:
sage: w.count??
Oh, so count
counts the occurrences of letters.
Here, w
is a letter word on the alphabet {'a', 'b'}
,
and 'aa'
is not a letter in that alphabet, so alphabet.
So the count is of how many times 'aa'
appears in w
as a letter must be zero.
Compare:
sage: w = Words(['a', 'b', 'aa'], 10).random_element()
sage: w
word: aa,aa,a,aa,a,a,a,b,a,aa
sage: w.count('aa')
4
So how do we count factors? or subwords?
Get hold of the set of words.
sage: W = w.parent()
sage: W
Finite words over {'a', 'b'}
Define the factor we are looking for:
sage: f = W('aa')
Count its occurrences as a factor or a subword in w
.
sage: f.nb_factor_occurrences_in(w)
7
sage: f.nb_subword_occurrences_in(w)
55
Conclusion: the documentation
The question raises a valid point! The documentation
forcount
should nb_factor_occurrences
This is now tracked at