Grouping Consecutive Dates Together
Solution 1:
defgroup(iterable):
myIter = iter(iterable)
run = [next(myIter)]
defcontinuesRun(x):
return run[-1]==x-1for x in myIter:
if continuesRun(x):
run.append(x)
else:
yield run
run = [x]
yield run
Demo:
>>> list( group([1,10,11,12,20,21]) )
[[1], [10, 11, 12], [20, 21]]
To apply this to your situation, define the function continuesRun
like so, in pseudocode:
def continuesRun(date):
previousDate = run[-1]
return previousDate==date-1dayor (previousDate.weekday==Friday and previousDate==date-3day)
sidenote: It seems slightly morally/pragmatically wrong, in my personal opinion, to count sickdays spans bordering weekends as potentially 2 or 4 days longer. But if you have good reason to do so, who am I to judge. =) To count those, post-process your runs: add 2 if the first day was Monday, and add 2 if the last day was Friday, then add len(d for d in range(run[-1]-run[0]) if (run[0]+d*day).isWeekend())
. Of course this does not count holidays, in which case you would do .isHoliday() or .isWeekend()
and make the "add 2" logic exactly like the len(...)
logic, by iterating back until you find a non-holiday, and penalizing the person for each holiday-or-weekend thus adjacent to the run.
Post a Comment for "Grouping Consecutive Dates Together"