Reputation: 3494
I have a list of date ranges.
05/01/2012 - 07/01/2012
07/02/2012 - 09/05/2012
01/01/2012 - 03/31/2012
08/01/2012 - 12/31/2012
Now the problem is to verify whether the given list covers all the dates in a specific range. For ex, this list covers all the dates in range 05/01/2012 - 12/31/2012, but non 01/01/2012-12/31/2012 because April dates are not covered in this list. Assumptions are,
What I have so far is to test the outliers like,
Finding the smallest start date and largest end date - If the start and/or end date of your test date range are less/greater than these dates respectively, test fails.
But what is the best strategy to check for any missed dates in the middle? Thanks in advance!
Upvotes: 1
Views: 2228
Reputation: 533710
The simplest strategy is to parse all the dates involved. O(N)
Sort the dates by the starting date. O(N log N)
time
Compare the end date with the next start date (ignoring any end dates which go backwards) O(N)
If the one end to the next start date is more than a day you have a gap.
Upvotes: 3
Reputation: 4077
try something like the following (after sorting the list range by min date):-
i=0
a=min_date at 0 position
b=max_date corresponding to a
while list has elements
c= min_date at (i+1) position
d=max_date corresponding to c
if (b< (c - 1 day))
then there is a gap;
else
{
if(b<d) // done to handle the situation :- 06/07/2012 - 12/31/2012
// 08/07/2012 - 11/31/2012. Then b will still remain 12/31/2012 after if condition
{
b=d;
}
}
i=i+2;
end while;
The above will only find any missing date in the list.
Upvotes: 0
Reputation: 19223
The first thing to do is fix your input data.
So your ranges would become
01/01/2012 - 03/31/2012
05/01/2012 - 07/01/2012
07/02/2012 - 09/05/2012
09/06/2012 - 12/31/2012
Then you just have to find the missing dates, see if you can figure out how to do that.
Upvotes: 2
Reputation: 1623
Generate every date in the ranges, put them in a list and test to see if your candidate is in that list.
Upvotes: 0
Reputation: 51553
Convert the dates to the number of days since 1 January 1970 (or some other reference date).
Then your problem becomes find the missing integers.
Upvotes: 0