Dear Monks
I want to find the largest block of similar items in the input. The code I have written is here.
#!/usr/local/bin/perl -w use strict; my $string = 'abbbcddddfegrgrgddeeeeedd'; my @chars = split //,$string; my $hold = ''; my $currentSequence = ''; my $longestSeq = ''; my $currentPosition = 0; my $longestSequenceStartPosition; foreach (@chars){ my $currentCharacter = $_; $currentPosition += 1; if($hold eq $currentCharacter){ $currentSequence .= $currentCharacter; } else{ if(length($longestSeq) < length($currentSequence)){ $longestSeq = $currentSequence; $longestSequenceStartPosition = ($currentPosition -1) - length +($longestSeq); } $currentSequence = $currentCharacter; } $hold = $currentCharacter; } print "$string\n"; print "$longestSeq\n"; my $answer = '-' x length($string); substr($answer,$longestSequenceStartPosition,length($longestSeq)) = $l +ongestSeq; print "$answer\n";
This works well if may not be efficient. It finds the first entry of largest block.
$string = 'abbbcddddfegrgrgddeeeeedd';
$answer = '------------------eeeee--'
I want to do it in a better way, find all the blocks, starting from largest to smallest and want to extend to 2-D and 3-D and may be higher dimensions. This is not from a programming contest or a homework
Thanks,
Looking for visual solution
Artist

Edit kudra, 2002-10-04 Removed unclosed pre tag


In reply to Block Search by artist

Title:
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, 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:
    & &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.