Datatypes for finite words#
- class sage.combinat.words.word_datatypes.WordDatatype[source]#
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[source]#
Bases:
WordDatatype
Datatype class for words defined by lists.
- length()[source]#
Return the length of the word.
EXAMPLES:
sage: w = Word([0,1,1,0]) sage: w.length() 4
>>> from sage.all import * >>> w = Word([Integer(0),Integer(1),Integer(1),Integer(0)]) >>> w.length() 4
- number_of_letter_occurrences(a)[source]#
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
>>> 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
- class sage.combinat.words.word_datatypes.WordDatatype_str[source]#
Bases:
WordDatatype
Datatype for words defined by strings.
- find(sub, start=0, end=None)[source]#
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
>>> 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)[source]#
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
>>> 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)[source]#
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
>>> 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)[source]#
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
>>> 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)[source]#
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
>>> 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()[source]#
Return the length of the word.
EXAMPLES:
sage: w = Word("abbabaabababa") sage: w.length() 13
>>> from sage.all import * >>> w = Word("abbabaabababa") >>> w.length() 13
- number_of_letter_occurrences(letter)[source]#
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
>>> 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: w.number_of_letter_occurrences('abb') 0
>>> from sage.all import * >>> w.number_of_letter_occurrences('abb') 0
- partition(sep)[source]#
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]
>>> from sage.all import * >>> w = Word("MyTailorIsPoor") >>> 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
>>> 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: 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
>>> 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)[source]#
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
>>> 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)[source]#
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 (default: None)maxsplit
– positive integer (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]
>>> 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: w.split() [word: My, word: tailor, word: is, word: poor]
>>> 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: 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]
>>> 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: w = Word("3230301030323212323032321") sage: w.split("32") [word: , word: 30301030, word: , word: 12, word: 30, word: , word: 1]
>>> 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: 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
>>> 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[source]#
Bases:
WordDatatype
Datatype class for words defined by tuples.