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

Re: Finding longest palindrome from a string

by Limbic~Region (Chancellor)
on Aug 13, 2004 at 12:09 UTC ( #382624=note: print w/replies, xml ) Need Help??

in reply to Finding longest palindrome from a string



When you mentioned in #perl last night that you intended to make a SoPW post about this, I assumed you would have also mentioned that it was for a job application. I haven't had a chance to refine my non-regex solution on my scratch pad, but here it is for the record.

#!/usr/bin/perl use strict; use warnings; print longest_palindrome( 'sacascascsacascascascadvgkgjsflfjgfk' ); sub longest_palindrome { my %lookup; my ($index, $record) = (-1, 0); push @{ $lookup{ substr($_[0], $_, 1) } } , $_ for 0 .. (length $_ +[0]) - 1; for my $letter ( keys %lookup ) { my $last = $#{ $lookup{ $letter } }; for my $start ( 0 .. $last - 1 ) { for my $end ( reverse ( $start + 1 .. $last ) ) { my $pos = $lookup{ $letter }[ $start ]; my $length = $lookup{ $letter }[ $end ] - $pos + 1; if ( $length > $record ) { my $palindrome = substr($_[0], $pos, $length); if ( $palindrome eq reverse $palindrome ) { ($index, $record) = ($pos, $length); last; } } } } } return substr($_[0], $index, $record); }

Cheers - L~R

Update: While I do not know if you intended to use any of the responses to your advantage in applying for the job, I do know you got the problem from the ad as you gave me the link in IRC. Additionally, you indicated you intended to apply for the job. Shame on me for not wording my admonishment better.

Update 2: Corrected another logic flaw pointed out by ccn, which makes it even slower :-(

Replies are listed 'Best First'.
•Re^2: Finding longest palindrome from a string
by merlyn (Sage) on Aug 13, 2004 at 14:10 UTC
    When you mentioned in #perl last night that you intended to make a SoPW post about this, I assumed you would have also mentioned that it was for a job application.
    Limbic~Region, thanks for that revelation. I've both notified the original job posting author directly, and posted this travesty in use.perl blog for awareness.

    I can only hope that I never meet BUU in person, especially at a job hiring situtation. This is despicable. This is worse than homework, because it's about getting a job, by cheating.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Methinks this might be a slight over-reaction (Update: or might have been, based on your original text), given that BUU did post his original solution to the problem. He may well have intended to go to the interview, describe his code, and then say "here are some alternatives that I solicited on Perl Monks", which is a valid thing to do when trying to solve a job-related Perl problem. One could say that he is showing initiative. Sure, it would have been polite to point this out in the OP. Maybe I'm too forgiving, but I always try to see the positive side.
        I know if I were an interviewer asking that question, I would have explicitly said "using whatever resources you have available... here's a terminal" if I had intended that. Instead, the question appears to be about a person's personal skills and knowledge.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      Not only is it cheating... but it's also plagiarism (of a sort)... no where does he mention that he ripped off this brain teaser from someone else. (Namely me, my boss, and a friend.)
      ------------ :Wq Not an editor command: Wq
        Let me put it this way. If you can conclusively prove that your boss was the first person to ever think of this problem, I will be more than happy to credit it to your boss, and mention your bosses name if I ever use it again.

      Your reaction to notify the author is exactly what I would have done.

      But no more. I voted "keep" in your consideration for deletion. Deleting BUU's node alone would be ineffective as it leaves all the replies with solutions lingering, but these replies are valid contributions in their own right and deletion is not justified merely because they're in reply to a deceptive node. Further, any possible damage is already done at this point and cannot be averted by deleting the parent node, which would rob the replies of their context. An editorial amendment of the node might be called for, but deleting it would be counterproductive.

      Makeshifts last the longest.

      Let me repeat myself in large letters merlyn.


      . Do you get it now? I DO NOT WANT TO CHEAT TO GET THE JOB. I just *assumed* other people would enjoy solving this problem, since I enjoyed writing the code.

      Shame on you merlyn, for the same reasons as limbic~region. You have *no* evidence that I have ever cheated to get a job (because I never have or will). All you have is some unfounded slander from limbic~region saying that "I want to cheat" or some such, and you launch off to critically castigate me, not only here, but posted to, a much more public site, WITHOUT EVEN CONSIDERING THAT THERE BE MORE TO THE ISSUE. Now I have large amounts of slander over something I HAVE NEVER DONE, WILL NEVER DO, AND HAD NO INTENTION OF DOING, and some people will probably never believe it, all because of some baseless slander.
        First, it's libel, not slander. Libel is printed, slander is spoken.

        Second, I never said you cheated to get a job. Only that this might be an indication that you are. In absence of further evidence, it still remains a possible explanation of the visible facts.

        Third, I will now need to take you on your word that you didn't not intend to cheat, even though this is inconsistent (in my mind) with the rest of your actions around this issue. I understand what you are saying, but it still smells.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        update: OK, having read L-R's description, I'm more convinced you just lied to me in attempting to defend yourself. Admittedly, it's hearsay, but I'm now saying it smells even worse.
        I wouldn't sweat merlyn's moral outrage too much, considering the source.
Re^2: Finding longest palindrome from a string
by BUU (Prior) on Aug 13, 2004 at 20:51 UTC


    Why the shame? Because you have attacked me for I haven't done, wouldn't do and HAD NO INTENTION OF DOING. This has absolutely *no* relationship to aforesaid job search other than the problem is mostly the same ( I don't think the problem is even exactly the same ). I had NO FUCKING INTENTION of posting this "just so I could steal peoples work to get a job by cheating". None. If I had that intention, it would have been 10 times easier to steal it by googling for solutions, instead of risking my stealing being found out by someone, as the boss for said job could easily read perlmonks and see where I got my solution. This would be much more difficult if I got it off google.

    But I HAD NO INTENTION OF DOING THIS. But instead of even verifying that I was going to, or had done it, you just blatantly attacked me over the *possibility* of me doing it.

    For your information, the reason I posted this question, with any mention of where I got it from, is I just assumed that it was a challenge lots of other people would enjoy also. And, judging by the sheer number of responses, lots of other people enjoyed writing a solution to it. Generally enabling this many people to write some cool perl for a solution would make me very happy, but in this case I find all my happiness is ruined by your slander.
      My better judgement says I shouldn't reply to this at all but... you appear to be quoting me:

      "just so I could steal peoples work to get a job by cheating"

      I never said that. In fact, I am the one in IRC that gave you the benefit of the doubt when others thought you intended to be unscrupulous. What I was addressing is:
      • You did indeed get the problem from the job ad
      • You did indicate you intended to apply for that position saying that you would use the fact you were a Saint at PerlMonks to your advantage
      • You posted the question here without reference to the first two points
      This all transpired on Freenode's #perl of which I do not log. Should you want to dispute any of this I am sure someone has these logs and it will be easy to determine. I certainly would not have said shame on you for anything if you had just acknowledged this when you posted.

      Cheers - L~R

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://382624]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-03-28 09:25 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.