Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Finding longest palindrome from a string

by cLive ;-) (Parson)
on Aug 13, 2004 at 09:42 UTC ( #382591=note: print w/ replies, xml ) Need Help??


in reply to Finding longest palindrome from a string

Sorry, I can't name the sub after myself since ;-) doesn't compile :)

sub cLive { my $rev = reverse $_[0]; my $len=''; my $d; for (0..length($_[0])-1) { my $c= join'',map { substr($rev,$_,1) eq substr($_[0],$_,1) ? +1 : 0 } 0..length($_[0])-1; my $match = (sort {length($a)<=>length($b)} $c =~ /(1+)/g)[-1] +; $match>$len and $len=$match and $d=$c; $rev = substr($rev,1).substr($rev,0,1); } $d=~s/(.*)($len).*/substr($_[0],length($1),length($len))/e; return $d; }
cLive ;-)

ps - if more than one longest palindrome, first is returned.


Comment on Re: Finding longest palindrome from a string
Download Code
My name doesn't compile (was Re: Finding longest palindrome ...)
by hv (Parson) on Aug 13, 2004 at 12:09 UTC

    my $name = 'cLive ;-)'; no strict 'refs'; *$name = sub { print "This is not my name\n" }; $name->();

    Hugo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2015-07-07 05:46 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 (87 votes), past polls