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: sage.combinat.words.word_datatypes.WordDatatype

Datatype class for words defined by lists.

count(a)

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
length()

Return the length of the word.

EXAMPLES:

sage: w = Word([0,1,1,0])
sage: w.length()
4
class sage.combinat.words.word_datatypes.WordDatatype_str

Bases: sage.combinat.words.word_datatypes.WordDatatype

Datatype for words defined by strings.

count(letter)

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
find(sub, start=0, end=None)

Returns 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 - non negative integer (default: 0) specifying the position from which to start the search.
  • end - non negative 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:

non negative integer or -1

EXAMPLES:

sage: w = Word("abbabaabababa")
sage: w.find("a")
0
sage: w.find("a", 4)
5
sage: w.find("a", 4, 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: 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
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: 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
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: 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
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: 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
length()

Return the length of the word.

EXAMPLES:

sage: w = Word("abbabaabababa")
sage: w.length()
13
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: w = Word("MyTailorIsPoor")
sage: w.partition("Tailor")
[word: My, word: Tailor, word: IsPoor]
sage: w = Word("3230301030323212323032321210121232121010")
sage: l = w.partition("323")
sage: print(l)
[word: , word: 323, word: 0301030323212323032321210121232121010]
sage: sum(l, Word('')) == w
True

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

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.
rfind(sub, start=0, end=None)

Returns 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 - non negative integer (default: 0) specifying the position at which the search must stop.
  • end - non negative 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:

non negative integer or -1

EXAMPLES:

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

Returns 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 (optional, default: None)
  • maxsplit - positive integer (optional, default: None)

OUTPUT:

  • a list of words

EXAMPLES:

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

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

The python behavior is kept when no argument is given:

sage: 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: 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]

You can split along words:

sage: w = Word("3230301030323212323032321")
sage: 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: 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.
class sage.combinat.words.word_datatypes.WordDatatype_tuple

Bases: sage.combinat.words.word_datatypes.WordDatatype

Datatype class for words defined by tuples.

length()

Return the length of the word.

EXAMPLES:

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