Hi Bryan, it's me again,
I had a bug - the if statement when stuffing the %ranges hash
was wrong. Here is the corrected code:
use strict;
use warnings;
my @starts = (3, 4, 15, 16);
my @stops = (5, 10, 20, 19);
my %ranges;
PAIR:
for (my $i = 0; $i <= $#starts; ++$i) {
for (keys %ranges) {
if ($starts[$i] > $_ and $starts[$i] < $ranges{$_}) {
$ranges{$_} = $stops[$i] if $stops[$i] > $ranges{$_};
next PAIR;
}
}
$ranges{$starts[$i]} = $stops[$i];
}
print "$_ => $ranges{$_}\n" for (sort { $a <=> $b } keys %ranges);
my $elapsed = 0;
$elapsed += $ranges{$_} - $_ for (keys %ranges);
print "elapsed = $elapsed\n";
Aloha => Beau.
-----Original Message-----
From: Bryan R Harris [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, May 29, 2002 1:58 PM
To: [EMAIL PROTECTED]
Subject: union of times algorithm
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.
I have to do this probably a million+ times. Any ideas on how I could go
about this?
TIA.
- Bryan
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]