Inspired by a real-life scenario, which I have asked for an answer to here: https://superuser.com/questions/1312212/writing-a-formula-to-count-how-many-times-each-date-appears-in-a-set-of-date-ran
Given an array of timespans (or startdate-enddate pairs), output a count of how many timespans cover each day, for all days in the total range.
For example:
# Start End
1 2001-01-01 2001-01-01
2 2001-01-01 2001-01-03
3 2001-01-01 2001-01-02
4 2001-01-03 2001-01-03
5 2001-01-05 2001-01-05
Given the above data, the results should be as follows:
2001-01-01: 3 (Records 1,2,3)
2001-01-02: 2 (Records 2,3)
2001-01-03: 2 (Records 2,4)
2001-01-04: 0
2001-01-05: 1 (Record 5)
You only need to output the counts for each day (in order, sorted oldest-newest); not which records they appear in.
You can assume that each timespan only contains dates, not times; and so whole days are always represented.
I/O
Input can be any format that represents a set of timespans - so either a set of pairs of times, or a collection of (builtin) objects containing start- and end-dates. The date-times are limited to between 1901 and 2099, as is normal for PPCG challenges.
You can assume the input is pre-sorted however you like (specify in your answer). Input dates are inclusive (so the range includes the whole of the start and end dates).
You can also assume that, of the two dates in any given range, the first will be older or equal to the second (i.e. you won't have a negative date range).
Output is an array containing the count for each day, from the oldest to the newest in the input when sorted by Start Date.
So, the output for the above example would be {3,2,2,0,1}
It is possible that some days are not included in any time range, in which case 0
is output for that date.
Winning Criteria
This is code-golf, so lowest bytes wins. Usual exclusions apply
Pseudo-algorithm example
For each time range in input
If start is older than current oldest, update current oldest
If end is newer than current newest, update current newest
End For
For each day in range oldest..newest
For each time range
If timerange contains day
add 1 to count for day
End For
Output count array
Other algorithms to get to the same result are fine.
0
should be in a dictionary? It only appears to force the user to iterate frommin(input)
tomax(input)
, which doesn't seem to add anything to the core of the challenge (computing timespans). \$\endgroup\$