Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm insanely proud of this one as it represents the most interesting bit of coding I've had to do in a while. It even seems to work, although I did cheat by importing POSIX.

#! /usr/bin/perl -w use strict; use POSIX qw(ceil); sub elgon { my $string = shift; my @answers; my %char_hash = map { $_ => 1} split //, $string; foreach my $key (keys %char_hash) { my @appearances; for (my $i = 0; $i < length($string); $i++) { push (@appearances, $i) if substr($string, $i, ++ 1) eq $key; } foreach my $start(@appearances) { foreach my $finish(reverse @appearances) { next if $start >= $finish; my $half_length = ceil(($finish - $sta +rt + 1) / 2); push @answers, substr($string , ($star +t) , ($finish - $start + 1) ) if substr($string, $start , $half_length) eq reverse substr ($string, ($finish - $half_length + +1), $half_length); } } } return "FAILED!" if ! scalar(@answers); my $longest = ""; map { $longest = $_ if length($longest) < length($_) } @answer +s; return $longest; }

UPDATE: There may be a minor bug in my use of POSIX, however I may be able to get round this as substr() automagically rounds down fractional values which are passed to it.

UPDATE^2: Fencepost error in ceil() function call fixed.

UPDATE^3: The reason why it fails is that the ordering can sometimes be wrong if there is more than one palindrome in the same string, due to the randomisation as the hash keys are fetched. I'll fix this at some point in the near future...

UPDATE^4: Issue fixed; Was in fact due to boneheadedness on my part. Now should work nicely, although not as pretty as it was.

UPDATE^5: Prettified a bit.


"Stercus! Dixit Pooh. Eeyore, missilis lux navigii heffalumporum iaculas. Piglet, mecum ad cellae migratae secundae concurras."

In reply to Re: Finding longest palindrome from a string by Elgon
in thread Finding longest palindrome from a string by BUU

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

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (2)
    As of 2021-04-21 03:03 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found