Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Finding longest palindrome from a string

by Aristotle (Chancellor)
on Aug 17, 2004 at 08:17 UTC ( #383581=note: print w/ replies, xml ) Need Help??


in reply to Re: Finding longest palindrome from a string
in thread Finding longest palindrome from a string

return $string . reverse $string;

That only generates even length palindromes. I instead suggest

return ( $string . ( rand < .5 ? $letters[ rand @letters ] : '' ) . reverse $string );

Also, Don't Repeat Yourself. That's far too much copy-paste code there. Look at the benchmark script I wrote: much less code, and it tells you exactly which input strings each solution failed on and what it produced instead.

Makeshifts last the longest.


Comment on Re^2: Finding longest palindrome from a string
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://383581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2015-07-02 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls