#16935: Faster palindromes function for the Words library
-------------------------------------+-------------------------------------
Reporter: nadialafreniere | Owner:
Type: enhancement | Status: needs_work
Priority: major | Milestone: sage-6.4
Component: combinatorics | Resolution:
Keywords: words, finite | Merged in:
words, palindromes | Reviewers:
Authors: Nadia Lafrenière | Work issues:
Report Upstream: N/A | Commit:
Branch: | def64a1a59757069be2840256a41dbc5951d5e07
u/nadialafreniere/16935 | Stopgaps:
Dependencies: |
-------------------------------------+-------------------------------------
Comment (by slabbe):
12. The code of {{{length_longest_palindrome}}} method contains the same
line four times:
{{{
while j//2 - i >= 0 and tuple(self[j//2-i : j//2-i+1]) ==
tuple(f(self[j//2+i : j//2+i+1])):
i = i + 1
}}}
Something should be done to factorize the code a little bit. For instance
if {{{j%2 == 1}}}, then store the result of {{{j//2}}} into some variable
{{{K}}} and else store the variable {{{j/2}}} in {{{K}}}. Then, use
{{{K}}} and factorize the code.
13. Anyhow, in the line of code above, avoid the creation of a tuple.
Also, I suggest to avoid the slice and simply use a for loop for the
indexes you want to compare.
I am thinking of a code that would look like that (it is most probably
wrong as I did not think about the limit cases and even vs odd palindrome
length, etc.)
{{{
possible = max(j, self.length()-j)
i = m / 2
while self[j-i] == self[j+i]:
i += 1
return i
}}}
But you get what I mean: short and clean code do not need any comment.
--
Ticket URL: <http://trac.sagemath.org/ticket/16935#comment:12>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.