Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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?
[shmem]: well, cadences are a bit different, also. But those are frills, too.
[shmem]: and the clarinet delivers a goof job, too.
[shmem]: oofs! s/oof/oob/
[shmem]: erm... again. s/oob/ood/

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-04-27 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (512 votes). Check out past polls.