[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Horrible algorithm



>>>>> "PS" == Peter Scott <Peter@PSDT.com> writes:

PS> The reason the first list contains 10:06 yet the interval is 5 minutes is 
PS> because there are events in the same group in A occurring later than 
PS> 10:00.  The process is: given an event in A at time a, find the next event 
PS> b in B occurring in [a,a+5]; if any events in A occur in (a,b] then add 
PS> them to the group, let the latest one be a', and adjust the interval for 
PS> finding new events in B for the group to be [a',a'+5].

PS> Any ideas for something better?

How does this look?

#!/usr/bin/perl -w
my @A = qw(1 2 4 7    21 37 38 39        51 52 53  71              90  100);
my @B = qw(   3 6 9 18           42 43 44        54  80 81 82 83 84  91   117);
my $D = 10;

push(@ao, $a = shift(@A));
push(@bo, $b = shift(@B));
while(@A && @B) {
    if ($B[0] <= $a + $D) {
	push(@bo, $b = shift(@B));
	next;
    }
    if ($A[0] <= $b) {
	push(@ao, $a = shift(@A));
	next;
    }

    print "A: ",join(",", @ao),"\t"
	,"B: ",join(",", @bo),"\n";

    @ao = @bo = ();
    push(@ao, $a = shift(@A));
    push(@bo, $b = shift(@B));
}

print "A: ",join(",", @ao),"\t"
    ,"B: ",join(",", @bo),"\n";

__END__
A: 1,2,4,7	B: 3,6,9
A: 21	B: 18
A: 37,38,39	B: 42,43,44
A: 51,52,53	B: 54
A: 71	B: 80,81
A: 90	B: 82,83,84,91
A: 100	B: 117

-- 
Chaim Frenkel					     Nonlinear Knowledge, Inc.
chaimf@pobox.com				               +1-718-236-0183

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe