Skip to content

Words: improve count consistency across datatype representation #30187

@slel

Description

@slel

Before this ticket, count depends on the representation (list vs str) in a surprising way, see this question on ask:

sage: s = 'aaabaaababaaaaabbbbb'
sage: u = Word(list(s))
sage: v = Word(s)
sage: u.count('ab')
0
sage: v.count('ab')
4

because

sage: type(v)
<class 'sage.combinat.words.word.FiniteWord_str'>
sage: type(u)
<class 'sage.combinat.words.word.FiniteWord_list'>

The proposed solution is to fix count so that it behaves like list.count and tuple.count:

sage: list(u).count('ab')
0
sage: list(v).count('ab')
0

In the proposed branch, the method count is now an alias to the method number_of_letter_occurrences.

Doing so, we also deprecate few methods in favor of:

_pos_in                    -> first_occurrence
first_pos_in               -> first_occurrence
factor_occurrences_in      -> factor_occurrences_iterator
nb_factor_occurrences_in   -> number_of_factor_occurrences
nb_subword_occurrences_in  -> number_of_subword_occurrences

See also:

CC: @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions