Palindrome finder: help find ways to speed up?

Jon Brown dogen at mindspring.com
Tue Apr 8 17:06:53 EDT 2003


Steven,

Actually the program does exactly what you describe, except from the inside
out because it starts in the middle, and it cuts out of the loop if the
characters don't match.

To look for pals it spins off big, big numbers of combinations, true enough.
That's what behind the need for speed. Since Python is inherently on the
pokey side, I expected the slowness but wonder if I'm missing a cool way to
pick up the pace. But I suspect the solution will involve C++, and I've
never linked C/C++ with Python.

Jonedary 

> A smarter search would do better.  One approach would be to build
> the palindrome from both ends instead of from just one end.  For
> example, suppose your code understood that a palindrome starting
> with 'orange' must end with a word 'o', 'ro', 'aro', 'naro',
> 'gnaro', 'egnaro', or a word ending with 'egnaro'.  If there are
> no such words, it can skip 1/n of the search space, namely all the
> combinations which start with 'orange'.





More information about the Python-list mailing list