artist has asked for the wisdom of the Perl Monks concerning the following question:
This works well if may not be efficient. It finds the first entry of largest block.#!/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";
$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
Edit kudra, 2002-10-04 Removed unclosed pre tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Block Search
by davorg (Chancellor) on Oct 04, 2002 at 16:34 UTC | |
by artist (Parson) on Oct 04, 2002 at 17:07 UTC | |
by demerphq (Chancellor) on Oct 04, 2002 at 17:13 UTC | |
|
Re: Block Search
by waswas-fng (Curate) on Oct 04, 2002 at 16:19 UTC | |
|
Re: Block Search
by meetraz (Hermit) on Oct 04, 2002 at 20:18 UTC | |
by blakem (Monsignor) on Oct 04, 2002 at 21:22 UTC | |
by meetraz (Hermit) on Oct 04, 2002 at 21:44 UTC | |
|
Re: Block Search
by BrowserUk (Patriarch) on Oct 04, 2002 at 21:34 UTC | |
by blakem (Monsignor) on Oct 04, 2002 at 21:55 UTC |