Datatypes for finite words
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.
TESTS:
sage: w = Word([0,1,1,0,0,1])
sage: isinstance(w, sage.combinat.words.word_datatypes.WordDatatype)
True
Bases: sage.combinat.words.word_datatypes.WordDatatype
Datatype class for words defined by lists.
Returns the number of occurrences of the letter a in the word self.
INPUT:
OUTPUT:
EXAMPLES:
sage: w = Word([0,1,1,0,1])
sage: w.count(0)
2
sage: w.count(1)
3
sage: w.count(2)
0
Return the length of the word.
EXAMPLES:
sage: w = Word([0,1,1,0])
sage: w.length()
4
Bases: sage.combinat.words.word_datatypes.WordDatatype
Datatype for words defined by strings.
Count the number of occurrences of letter.
INPUT:
OUTPUT:
EXAMPLES:
sage: w = Word("abbabaabababa")
sage: w.count('a')
7
sage: w.count('b')
6
sage: w.count('c')
0
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:
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
Test whether self has other as a prefix.
INPUT:
OUTPUT:
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
TESTS:
sage: ab = Word('ab')
sage: abba = Word(['a','b','b','a'])
sage: ab.has_prefix(abba)
False
sage: abba.has_prefix(ab)
True
Test whether self has other as a suffix.
INPUT:
OUTPUT:
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
Test whether self is a prefix of other.
INPUT:
OUTPUT:
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
TESTS:
sage: ab = Word('ab')
sage: abba = Word(['a','b','b','a'])
sage: ab.is_prefix(abba)
True
sage: abba.is_prefix(ab)
False
Test whether self is a suffix of other.
INPUT:
OUTPUT:
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
TESTS:
sage: w = Word("abbabaabababa")
sage: u = Word(['a','b','a','b','a'])
sage: w.is_suffix(u)
False
sage: u.is_suffix(w)
True
Return the length of the word.
EXAMPLES:
sage: w = Word("abbabaabababa")
sage: w.length()
13
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:
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.
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:
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
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:
OUTPUT:
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.
Bases: sage.combinat.words.word_datatypes.WordDatatype
Datatype class for words defined by tuples.
Return the length of the word.
EXAMPLES:
sage: w = Word((0,1,1,0))
sage: w.length()
4