-
-
Notifications
You must be signed in to change notification settings - Fork 619
Words: improve count
consistency across datatype representation
#30187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
comment:1
According to the documentation sage: W = Words('ab', 20)
sage: u = W.random_element()
sage: v = Word('aaabaaababaaaaabbbbb')
sage: type(u)
<class 'sage.combinat.words.word.FiniteWord_list'>
sage: type(v)
<class 'sage.combinat.words.word.FiniteWord_str'>
sage: v.count
<built-in method count of FiniteWord_str object at 0x7f3853f0cc50>
sage: u.count
<built-in method count of FiniteWord_list object at 0x7f3853f12a48> The method The generic method is: sage: sage.combinat.words.finite_word.FiniteWord_class.count??
Signature: sage.combinat.words.finite_word.FiniteWord_class.count(self, letter)
Source:
def count(self, letter):
r"""
Count the number of occurrences of ``letter`` in ``self``.
EXAMPLES::
sage: Word('abbabaab').count('a')
4
"""
return Integer(sum(1 for a in self if a == letter)) The method sage: sage.combinat.words.word_datatypes.WordDatatype_list.count??
Source:
def count(self, a):
r"""
Returns the number of occurrences of the letter ``a`` in the word
``self``.
INPUT:
- ``a`` - a letter
OUTPUT:
- integer
EXAMPLES::
sage: w = Word([0,1,1,0,1])
sage: w.count(0)
2
sage: w.count(1)
3
sage: w.count(2)
0
"""
return self._data.count(a) The method sage: sage.combinat.words.word_datatypes.WordDatatype_str.count??
Source:
def count(self, letter):
r"""
Count the number of occurrences of ``letter``.
INPUT:
- ``letter`` - a letter
OUTPUT:
- integer
EXAMPLES::
sage: w = Word("abbabaabababa")
sage: w.count('a')
7
sage: w.count('b')
6
sage: w.count('c')
0
"""
return self._data.count(letter) But, I agree that it could be more robust (provide an error message if the letter is not in the alphabet? or return zero? or advertise about |
Author: Sébastien Labbé |
Commit: |
Branch: u/slabbe/30187 |
comment:3
Salut Samuel, tu veux faire le review? |
comment:4
|
comment:5
In principal the change is ok, also I would have expected What I don't like is the mess with the names. For
Could we maybe add aliases
and likewise for This would make it much easier to find and in my opinion would make #30143 obsolete. |
comment:7
I rebased the branch on top of beta11. I also removed the deprecation: it was not the original goal of the ticket. Also, maybe it is desirable to have (I saw your recent comment just now after I force-pushed the branch.) |
Reviewer: Jonathan Kliem |
comment:8
Works for me. What do you think of those aliases? I missed that you added the |
comment:9
I still want to fix the |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:11
Those methods names ( Anyway, since the set of There are still other stuff that I dislike in that module, like |
comment:12
Ready for review! |
comment:13
doc does not build
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:15
oups, forgot to check that. Now doc builds ok on my machine. |
comment:16
I would suggest: - sage: w[1:].first_occurrence(u)
- 8
+ sage: w.first_occurrence(u, start=1)
+ 9 |
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
|
comment:18
rebased on 9.2.beta12 + fixed |
This comment has been minimized.
This comment has been minimized.
comment:19
updating description of ticket |
This comment has been minimized.
This comment has been minimized.
count
consistency across alphabetscount
consistency across datatype representation
comment:22
Sorry, two more things:
|
comment:23
Sorry, I didn't read closely. Of course The comment about the function not terminating would still be nice. |
comment:24
The patchbot is morally green. Once you added this comment, you can put this on positive review on my behalf. (I think the comment is needed, as you claimed that the function returns |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:26
Doc builds ok on my side. |
Changed branch from u/slabbe/30187 to |
sagemathgh-39262: Add script for checking for old deprecations <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> To find deprecations that can now safely removed. Example: ``` python tools/check_deprecations.py src/sage/combinat Found 9 deprecations. 100%| Deprecations over one year ago: File: src\sage\combinat\permutation.py, PR: sagemath#26810, Closed Date: 2019-01-23 File: src\sage\combinat\permutation.py, PR: sagemath#27467, Closed Date: 2019-06-12 File: src\sage\combinat\words\finite_word.py, PR: sagemath#30187, Closed Date: 2020-09-15 File: src\sage\combinat\designs\difference_family.py, PR: sagemath#35211, Closed Date: 2023-04-01 ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39262 Reported by: Tobias Diez Reviewer(s): Frédéric Chapoton, Tobias Diez
Before this ticket,
count
depends on the representation (list vs str) in a surprising way, see this question on ask:because
The proposed solution is to fix
count
so that it behaves like list.count and tuple.count:In the proposed branch, the method
count
is now an alias to the methodnumber_of_letter_occurrences
.Doing so, we also deprecate few methods in favor of:
See also:
nb_factor_occurrences
incount
documentation #30143: mentionnb_factor_occurrences
incount
documentationCC: @seblabbe @slel
Component: combinatorics
Keywords: words, count
Author: Sébastien Labbé
Branch/Commit:
4c9870d
Reviewer: Jonathan Kliem
Issue created by migration from https://trac.sagemath.org/ticket/30187
The text was updated successfully, but these errors were encountered: