Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Finding longest palindrome from a string

by Random_Walk (Parson)
on Aug 13, 2004 at 09:29 UTC ( #382586=note: print w/ replies, xml ) Need Help??


in reply to Finding longest palindrome from a string

Nice puzzle, I offer my humble attempt
sub Random_Walk { my ($left, $right, $pal, $i)=("", "", "", 1); my $test=join " ", @ARGV; for (; $i<((length $test)/2)+2; $i++) { $left.="(.)"; $right="(\\$i)".$right; if ($test=~/$left.?$right/) {$pal=$&; next} return $pal; } }
I tried a recursive one first which was fun but I had to do lots of work to cut the string into different slices so regex it is.

cnn, Thanks for the hint on code blanking.

Off by two error fixed in the for loop

Added .? in the middle to allow palindromes symetrical around a single character (borrowed that idea from ccn ;)


Comment on Re: Finding longest palindrome from a string
Download Code
Replies are listed 'Best First'.
Re^2: Finding longest palindrome from a string
by ccn (Vicar) on Aug 13, 2004 at 09:32 UTC
    I do not know how to black the code out

    You can use

    <table><tr><td bgcolor="#000000" color="#000000"> [[[ YOUR CODE HERE ]]] </td></tr></table>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-07-30 02:32 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 (269 votes), past polls