Datatypes for finite words - Combinatorics

Skip to content
Back to top
View this page
Edit this page

# Datatypes for finite words

class sage.combinat.words.word_datatypes.WordDatatype

  Bases: `object`

  The generic WordDatatype class.

  Any word datatype must contain two attributes (at least):

  - `_parent`

  - `_hash`

  They are automatically defined here and it’s not necessary (and forbidden) to define them anywhere else.

class sage.combinat.words.word_datatypes.WordDatatype_list

  Bases: `WordDatatype`

  Datatype class for words defined by lists.

  length()

    Return the length of the word.

    EXAMPLES:

    Sage

        sage: w = Word([0,1,1,0])
        sage: w.length()
        4

    Python

        >>> from sage.all import *
        >>> w = Word([Integer(0),Integer(1),Integer(1),Integer(0)])
        >>> w.length()
        4

  number_of_letter_occurrences(a)

    Return the number of occurrences of the letter `a` in the word `self`.

    INPUT:

    - `a` – a letter

    OUTPUT: integer

    EXAMPLES:

    Sage

        sage: w = Word([0,1,1,0,1])
        sage: w.number_of_letter_occurrences(0)
        2
        sage: w.number_of_letter_occurrences(1)
        3
        sage: w.number_of_letter_occurrences(2)
        0

    Python

        >>> from sage.all import *
        >>> w = Word([Integer(0),Integer(1),Integer(1),Integer(0),Integer(1)])
        >>> w.number_of_letter_occurrences(Integer(0))
        2
        >>> w.number_of_letter_occurrences(Integer(1))
        3
        >>> w.number_of_letter_occurrences(Integer(2))
        0

    See also

    `sage.combinat.words.finite_word.FiniteWord_class.number_of_factor_occurrences()`

class sage.combinat.words.word_datatypes.WordDatatype_str

  Bases: `WordDatatype`

  Datatype for words defined by strings.

  find(sub, start=0, end=None)

    Return the index of the first occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure.

    INPUT:

    - `sub` – string or word to search for

    - `start` – nonnegative integer (default: 0) specifying the position from which to start the search.

    - `end` – nonnegative integer (default: `None`); specifying the position at which the search must stop. If `None`, then the search is performed up to the end of the string.

    OUTPUT: nonnegative integer or \(-1\)

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: w.find("a")
        0
        sage: w.find("a", 4)
        5
        sage: w.find("a", 4, 5)
        -1

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> w.find("a")
        0
        >>> w.find("a", Integer(4))
        5
        >>> w.find("a", Integer(4), Integer(5))
        -1

  has_prefix(other)

    Test whether `self` has `other` as a prefix.

    INPUT:

    - `other` – a word (an instance of `Word_class`) or a `str`

    OUTPUT: boolean

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: u = Word("abbab")
        sage: w.has_prefix(u)
        True
        sage: u.has_prefix(w)
        False
        sage: u.has_prefix("abbab")
        True

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> u = Word("abbab")
        >>> w.has_prefix(u)
        True
        >>> u.has_prefix(w)
        False
        >>> u.has_prefix("abbab")
        True

  has_suffix(other)

    Test whether `self` has `other` as a suffix.

    INPUT:

    - `other` – a word (an instance of `Word_class`) or a `str`

    OUTPUT: boolean

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: u = Word("ababa")
        sage: w.has_suffix(u)
        True
        sage: u.has_suffix(w)
        False
        sage: u.has_suffix("ababa")
        True

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> u = Word("ababa")
        >>> w.has_suffix(u)
        True
        >>> u.has_suffix(w)
        False
        >>> u.has_suffix("ababa")
        True

  is_prefix(other)

    Test whether `self` is a prefix of `other`.

    INPUT:

    - `other` – a word (an instance of `Word_class`) or a `str`

    OUTPUT: boolean

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: u = Word("abbab")
        sage: w.is_prefix(u)
        False
        sage: u.is_prefix(w)
        True
        sage: u.is_prefix("abbabaabababa")
        True

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> u = Word("abbab")
        >>> w.is_prefix(u)
        False
        >>> u.is_prefix(w)
        True
        >>> u.is_prefix("abbabaabababa")
        True

  is_suffix(other)

    Test whether `self` is a suffix of `other`.

    INPUT:

    - `other` – a word (an instance of `Word_class`) or a `str`

    OUTPUT: boolean

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: u = Word("ababa")
        sage: w.is_suffix(u)
        False
        sage: u.is_suffix(w)
        True
        sage: u.is_suffix("abbabaabababa")
        True

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> u = Word("ababa")
        >>> w.is_suffix(u)
        False
        >>> u.is_suffix(w)
        True
        >>> u.is_suffix("abbabaabababa")
        True

  length()

    Return the length of the word.

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: w.length()
        13

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> w.length()
        13

  number_of_letter_occurrences(letter)

    Count the number of occurrences of `letter`.

    INPUT:

    - `letter` – a letter

    OUTPUT: integer

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: w.number_of_letter_occurrences('a')
        7
        sage: w.number_of_letter_occurrences('b')
        6
        sage: w.number_of_letter_occurrences('c')
        0

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> w.number_of_letter_occurrences('a')
        7
        >>> w.number_of_letter_occurrences('b')
        6
        >>> w.number_of_letter_occurrences('c')
        0

    Sage

        sage: w.number_of_letter_occurrences('abb')
        0

    [Python]

        >>> from sage.all import *
        >>> w.number_of_letter_occurrences('abb')
        0

    See also

    `sage.combinat.words.finite_word.FiniteWord_class.number_of_factor_occurrences()`

  partition(sep)

    Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. The concatenation of the terms in the list gives back the initial word.

    See also the split method.

    Note

    This just wraps Python’s builtin `str::partition()` for `str`.

    INPUT:

    - `sep` – string or word

    EXAMPLES:

    Sage

        sage: w = Word("MyTailorIsPoor")
        sage: w.partition("Tailor")
        [word: My, word: Tailor, word: IsPoor]

    Python

        >>> from sage.all import *
        >>> w = Word("MyTailorIsPoor")
        >>> w.partition("Tailor")
        [word: My, word: Tailor, word: IsPoor]

    Sage

        sage: w = Word("3230301030323212323032321210121232121010")
        sage: l = w.partition("323")
        sage: print(l)
        [word: , word: 323, word: 0301030323212323032321210121232121010]
        sage: sum(l, Word('')) == w
        True

    [Python]

        >>> from sage.all import *
        >>> w = Word("3230301030323212323032321210121232121010")
        >>> l = w.partition("323")
        >>> print(l)
        [word: , word: 323, word: 0301030323212323032321210121232121010]
        >>> sum(l, Word('')) == w
        True

    If the separator is not a string an error is raised:

    Sage

        sage: w = Word("le papa du papa du papa etait un petit pioupiou")
        sage: w.partition(Word(['p','a','p','a']))
        Traceback (most recent call last):
        ...
        ValueError: the separator must be a string

    Python

        >>> from sage.all import *
        >>> w = Word("le papa du papa du papa etait un petit pioupiou")
        >>> w.partition(Word(['p','a','p','a']))
        Traceback (most recent call last):
        ...
        ValueError: the separator must be a string

  rfind(sub, start=0, end=None)

    Return the index of the last occurrence of sub in self, such that sub is contained within self[start:end]. Returns -1 on failure.

    INPUT:

    - `sub` – string or word to search for

    - `start` – nonnegative integer (default: 0) specifying the position at which the search must stop.

    - `end` – nonnegative integer (default: `None`); specifying the position from which to start the search. If `None`, then the search is performed up to the end of the string.

    OUTPUT: nonnegative integer or \(-1\)

    EXAMPLES:

    Sage

        sage: w = Word("abbabaabababa")
        sage: w.rfind("a")
        12
        sage: w.rfind("a", 4, 8)
        6
        sage: w.rfind("a", 4, 5)
        -1

    Python

        >>> from sage.all import *
        >>> w = Word("abbabaabababa")
        >>> w.rfind("a")
        12
        >>> w.rfind("a", Integer(4), Integer(8))
        6
        >>> w.rfind("a", Integer(4), Integer(5))
        -1

  split(sep=None, maxsplit=None)

    Return a list of words, using sep as a delimiter string. If maxsplit is given, at most maxsplit splits are done.

    See also the partition method.

    Note

    This just wraps Python’s builtin `str::split()` for `str`.

    INPUT:

    - `sep` – string or word (default: `None`)

    - `maxsplit` – positive integer (default: `None`)

    OUTPUT: list of words

    EXAMPLES:

    You can split along white space to find words in a sentence:

    Sage

        sage: w = Word("My tailor is poor")
        sage: w.split(" ")
        [word: My, word: tailor, word: is, word: poor]

    Python

        >>> from sage.all import *
        >>> w = Word("My tailor is poor")
        >>> w.split(" ")
        [word: My, word: tailor, word: is, word: poor]

    The python behavior is kept when no argument is given:

    Sage

        sage: w.split()
        [word: My, word: tailor, word: is, word: poor]

    [Python]

        >>> from sage.all import *
        >>> w.split()
        [word: My, word: tailor, word: is, word: poor]

    You can split in two words letters to get the length of blocks in the other letter:

    Sage

        sage: w = Word("ababbabaaba")
        sage: w.split('a')
        [word: , word: b, word: bb, word: b, word: , word: b, word: ]
        sage: w.split('b')
        [word: a, word: a, word: , word: a, word: aa, word: a]

    Python

        >>> from sage.all import *
        >>> w = Word("ababbabaaba")
        >>> w.split('a')
        [word: , word: b, word: bb, word: b, word: , word: b, word: ]
        >>> w.split('b')
        [word: a, word: a, word: , word: a, word: aa, word: a]

    You can split along words:

    Sage

        sage: w = Word("3230301030323212323032321")
        sage: w.split("32")
        [word: , word: 30301030, word: , word: 12, word: 30, word: , word: 1]

    [Python]

        >>> from sage.all import *
        >>> w = Word("3230301030323212323032321")
        >>> w.split("32")
        [word: , word: 30301030, word: , word: 12, word: 30, word: , word: 1]

    If the separator is not a string a `ValueError` is raised:

    Sage

        sage: w = Word("le papa du papa du papa etait un petit pioupiou")
        sage: w.split(Word(['p','a','p','a']))
        Traceback (most recent call last):
        ...
        ValueError: the separator must be a string

    Python

        >>> from sage.all import *
        >>> w = Word("le papa du papa du papa etait un petit pioupiou")
        >>> w.split(Word(['p','a','p','a']))
        Traceback (most recent call last):
        ...
        ValueError: the separator must be a string

class sage.combinat.words.word_datatypes.WordDatatype_tuple

  Bases: `WordDatatype`

  Datatype class for words defined by tuples.

  length()

    Return the length of the word.

    EXAMPLES:

    Sage

        sage: w = Word((0,1,1,0))
        sage: w.length()
        4

    Python

        >>> from sage.all import *
        >>> w = Word((Integer(0),Integer(1),Integer(1),Integer(0)))
        >>> w.length()
        4