Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Finding longest palindrome from a string by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found