on Wed, 29 May 2002 23:58:19 GMT, [EMAIL PROTECTED] (Bryan
R Harris) wrote: 

> 
> I'm trying to come up with an algorithm that seems like it ought
> to be really easy, but it's turning out to be pretty tough...
> 
> Basically I have three pairs of start/stop times, e.g.:
> 
>    3, 5
>    4, 10
>   15, 20
> 
> I want the total time covered by all these ranges.  I can't just
> say (5-3 + 10-4 + 20-15) because the 3,5 and 4,10 ranges overlap. 
> The desired answer for this case is 13, 3 to 10 and 15 to 20.  I
> have the start times in two arrays, @starts and @stops.

If the start/stop pairs are integers (as shown in your example), you 
could stuff a time-slot array. Using Beau's testdata and notation:

    #! perl -w
    use strict;

    my @starts = (3,  4, 15, 16);
    my @stops  = (5, 10, 20, 19);
    my @time_slots = ();

    for (my $i=0; $i<@starts; $i++) {
        $time_slots[$_] = 1 for ( $starts[$i] .. $stops[$i] - 1 );
    }
   
    my $elapsed = 0;
    for (@time_slots) {
        $elapsed++ if $_;
    }
    print "elapsed = $elapsed\n";

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to