Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Finding longest palindrome from a string

by jdporter (Canon)
on Aug 13, 2004 at 14:31 UTC ( #382686=note: print w/replies, xml ) Need Help??


in reply to Finding longest palindrome from a string

I hate writing Perl that looks like Pascal. Oh well...
sub JDP::longest_palindrome # operates on $_ { my $pal; for my $i ( 0 .. length($_) ) { last if defined($pal) && length($_)-$i < length($pal); my $j = rindex $_, substr( $_, $i, 1 ); while ( $j > $i ) { my $s = substr $_, $i, $j-$i+1; if ( $s eq reverse $s ) # it's a palindrome { # but is it the longest yet found? $pal = $s unless defined $pal && length($pal) > length +($s); } $j--; $j = rindex $_, substr( $_, $i, 1 ), $j; } } $pal }

Edit by tye, change PRE to CODE around long lines, close FONT tag

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://382686]
help
Chatterbox?
[1nickt]: The Google-fu is strong with this one.
[1nickt]: I was looking at Best Buy since like the in-store return/repace plan ... but the 17" HP Envy they have is not this one ....
[1nickt]: May be brand new ... 0 reviews on the site.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-03-26 18:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (315 votes). Check out past polls.