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: w = Word([0,1,1,0]) sage: w.length() 4
- number_of_letter_occurrences(a)#
Returns the number of occurrences of the letter
a
in the wordself
.INPUT:
a
- a letter
OUTPUT:
integer
EXAMPLES:
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
- class sage.combinat.words.word_datatypes.WordDatatype_str#
Bases:
WordDatatype
Datatype for words defined by strings.
- 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
hasother
as a prefix.INPUT:
other
- a word (an instance ofWord_class
) or astr
.
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
hasother
as a suffix.INPUT:
other
- a word (an instance ofWord_class
) or astr
.
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 ofother
.INPUT:
other
- a word (an instance ofWord_class
) or astr
.
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 ofother
.INPUT:
other
- a word (an instance ofWord_class
) or astr
.
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
- number_of_letter_occurrences(letter)#
Count the number of occurrences of
letter
.INPUT:
letter
- a letter
OUTPUT:
integer
EXAMPLES:
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
sage: w.number_of_letter_occurrences('abb') 0
- 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()
forstr
.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()
forstr
.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:
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