17
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.
3Is an array of integers required, or are we allowed to return something else, say a dictionary with keys as each date? If we are allowed to return a dictionary, then can we omit dates that are not in any of the timespans? – JungHwan Min – 2018-06-13T16:54:15.677
1can we take input as two lists, one with start dates and the other with corresponding end dates? – Giuseppe – 2018-06-13T20:15:13.160
Yeah, all of those things are fine, except omitting a date - I explicitly say that 0 should be output in that case – simonalexander2005 – 2018-06-13T21:01:49.807
3May I ask why
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). – JungHwan Min – 2018-06-13T21:43:00.843Has this been in the Sandbox for quite a while (more than a few months)? I could have sworn I've seen a similar challenge before, but am unable to find it.. – Kevin Cruijssen – 2018-06-14T08:05:40.750
Yeah, it's been there a while - I only just got around to moving it here as it had a few upvotes so I thought it worthwhile – simonalexander2005 – 2018-06-14T08:38:13.110
2@JungHwanMin I guess it doesn't alter it; but because I explicitly had it in the spec when I posted it, I don't want to go messing with it and make someone else redo their answer – simonalexander2005 – 2018-06-14T08:39:36.640
@simonalexander2005 Making 0 optional wouldn't invalidate anything. – JungHwan Min – 2018-06-14T12:20:31.897
@JungHwanMin making 0 optional could make some valid answer longer than necessary – edc65 – 2018-06-14T13:13:08.590
@edc65 How so? They could include the
0
if the version that has the0
is shorter. – JungHwan Min – 2018-06-14T13:26:23.217@JungHwanMin now the 0s are not optional. There are now valid answers that could be made shorter if 0 is optional. So people should modify their now valid answer to comply with the new spec, or leave a sub optimal answer. – edc65 – 2018-06-14T13:41:59.843
If returning a dictionary/object, does it need to be sorted by date or is it enough that the keys indicate with date is which? – Shaggy – 2018-06-15T09:37:42.493
Suggested test case:
2000-02-27 2000-03-02
– Adám – 2018-06-15T09:49:00.660@Jakob correct, added. – simonalexander2005 – 2018-06-15T12:57:58.360
@Shaggy no sort needed – simonalexander2005 – 2018-06-15T12:58:11.610