Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge branch 'public/combinat/words/longest_common_extension-23131' o…
Browse files Browse the repository at this point in the history
…f git://trac.sagemath.org/sage into t/23131/public/combinat/words/longest_common_extension-23131
  • Loading branch information
saliola committed Aug 25, 2017
2 parents effcedb + 8d3d3fa commit d17e0c8
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/sage/combinat/words/finite_word.py
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,70 @@ def is_cadence(self, seq):
return False
return True

def longest_forward_extension(self, x, y):
r"""
Compute the length of the longest factor of ``self`` that
starts at ``x`` and that matches a factor that starts at ``y``.
INPUT:
- ``x``, ``y`` -- positions in ``self``
EXAMPLES::
sage: w = Word('0011001')
sage: w.longest_forward_extension(0, 4)
3
sage: w.longest_forward_extension(0, 2)
0
sage: w.longest_forward_extension(-3, 2)
Traceback (most recent call last):
...
ValueError: x and y must be valid positions in self
"""
length = self.length()
if not (0 <= x and 0 <= y and x < length and y < length):
raise ValueError("x and y must be valid positions in self")
l = 0
while x < length and y < length and self[x] == self[y]:
l += 1
x += 1
y += 1
return l

def longest_backward_extension(self, x, y):
r"""
Compute the length of the longest factor of ``self`` that
ends at ``x`` and that matches a factor that ends at ``y``.
INPUT:
- ``x``, ``y`` -- positions in ``self``
EXAMPLES::
sage: w = Word('0011001')
sage: w.longest_backward_extension(6, 2)
3
sage: w.longest_backward_extension(1, 4)
1
sage: w.longest_backward_extension(1, 3)
0
sage: w.longest_backward_extension(4, 23)
Traceback (most recent call last):
...
ValueError: x and y must be valid positions in self
"""
length = self.length()
if not (0 <= x and 0 <= y and x < length and y < length):
raise ValueError("x and y must be valid positions in self")
l = 0
while x >= 0 and y >= 0 and self[x] == self[y]:
l += 1
x -= 1
y -= 1
return l

def longest_common_suffix(self, other):
r"""
Return the longest common suffix of ``self`` and ``other``.
Expand Down

0 comments on commit d17e0c8

Please sign in to comment.