The title is the question. How would you write some code to find the longest palindrome from a string? Note that the entire string *may* or *may not* be a palindrome, you must find a substring from said string that is a palindrome. Also note that there are no "special characters" that you should ignore. So a palindrome is just the reverse of the string. So "foo bazzaboof" is not a palindrome because of the space.
Bonus points for speed, readability and general style. More bonus points for writing it in the form of a subroutine so I can benchmark them all. Name the sub after your self.
My attempt is as follows:
sub buu
{
my @p;
my $arg = shift;
my $p;
while( $arg =~ /((.).?\2)/g )
{
my $m = $1;
while( $arg =~ /((.)$m\2)/ )
{
$m = $1;
}
if( length($m) > length($p) )
{
$p = $m;
}
}
return $p;
}
|
Update to respond to the criticism:
Yes, I did get this problem from the job posting on jobs.perl.org. Yes I had every intention of eventually applying for that job. I had no intention of ripping off people's code to get said job. If I had any intention of using replies to this node for that job, I would have mentioned it. My intentions were simply to share a fun puzzle I found with this community I have grown to love.
Looking at just the facts, in the worst light, I can see how
limbic~region,
merlyn and others might misinterpret the facts to reach a damning conclusion. I can only say that when I wrote this node, the job had nothing to do with it, so I didn't bother to mention it. That is how my mind works. I can understand why people would have problems with me not mentioning the source, and in retrospect I probably should have credited the source for the problem. Also considering where I got the problem, perhaps I should have waited longer or asked before I "published" the problem. I did not consider the affect this would have on
etcshadow and his boss when I wrote this node, and I have already apologized to
etcshadow, later on in this thread. I apologize for any impact this may have had on his business.
Having expressed my apologies, I want to reiterate that I have never, nor shall I ever, do something unethical to gain an advantage when applying for a job. I hope my future contributions will make this clear.
Update2, minor formatting changes
Update3, more formatting changes, slightly less inflammatory. Original commented.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.