in reply to Searching parallel arrays.

When integers 1,2,3,4,5 will be spread accross the arrays, do you expect one (which?) or two contiguous sequences?

Replies are listed 'Best First'.
Re^2: Searching parallel arrays.
by BrowserUk (Patriarch) on Dec 08, 2006 at 19:27 UTC

    Good question++. Longer sequences are not expected but could occur.

    They can be reported as a single long sequence, or as multiple sequences of 4, whichever is easier for the algorithm.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks for that clarification, but you still haven't answered Corion's equally good objection++ above, namely:

      The rule is each array contains a sorted (ascending) sequence of non-contiguous, unique integers, but my @a3 = ( 205, 206, 315 ); contains the two contiguous numbers 205 and 206.

      Should we be reasoning based on your descripition of the problem, or on your supplied dataset?

      I'v tried to code it, without reading codes posted previously, just for enjoy. I'v been curious, how many rows it will has. O.K., it is not very perlish... Idea was very simple - zip the four arrays to one stream, fill circle buffer and test if last value of the buffer - 3 is equal to first value. Damn, 67 rows.
      use strict; use warnings; my @a1 = ( 100, 204, 208, 312 ); my @a2 = ( 102, 313, 409 ); my @a3 = ( 205, 206, 315 ); my @a4 = ( 207, 210, 314 ); my $a = [\@a1,\@a2,\@a3,\@a4]; my @ai = (0, 0, 0, 0); my ($a1a2, $a3a4) = (getLess(0), getLess(2)); my @circle = (getNext(),getNext(),getNext()); my $ci = 3; while (defined($circle[$ci] = getNext())) { if ($circle[$ci][2]-3 == $circle[($ci+1)%4][2]) { for (my $i = 1; $i < 5; $i++) { print $circle[($ci+$i)%4][2]. ' ['.$circle[($ci+$i)%4][0].','.$circle[($ci+$i)%4][1].'] '; } print "\n"; } $ci = ($ci+1)%4; } sub getLess { my $offset = shift; my $ret; if (defined $$a[$offset][$ai[$offset]]) { if (defined $$a[$offset+1][$ai[$offset+1]]) { $offset++ if $$a[$offset][$ai[$offset]] > $$a[$offset+1][$ai[$ +offset+1]]; } $ret = [$offset, $ai[$offset], $$a[$offset][$ai[$offset]]]; $ai[$offset]++; } else { $offset++; if (defined $$a[$offset][$ai[$offset]]) { $ret = [$offset, $ai[$offset], $$a[$offset][$ai[$offset]]]; $ai[$offset]++; } } return $ret; } sub getNext { my $ret; if (defined $a1a2) { if (defined $a3a4) { if ($$a1a2[2] < $$a3a4[2]) { $ret = $a1a2; $a1a2 = getLess(0); } else { $ret = $a3a4; $a3a4 = getLess(2); } } else { $ret = $a1a2; $a1a2 = getLess(0); } } else { if (defined $a3a4) { $ret = $a3a4; $a3a4 = getLess(2); } } return $ret; }
      updated: output is
      204 [0,1] 205 [2,0] 206 [2,1] 207 [3,0] 205 [2,0] 206 [2,1] 207 [3,0] 208 [0,2] 312 [0,3] 313 [1,1] 314 [3,2] 315 [2,2]