Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Merge datetime intervals solution in Clear category for Multiple Lightbulbs by Phil15
from datetime import datetime
from itertools import zip_longest
from typing import List, Optional, Tuple, Union
def sum_light(
els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
) -> int:
"""how long the light bulb has been turned on"""
# List times for each "light id".
times: Dict[int, List[datetime]] = {}
for elem in els:
if isinstance(elem, tuple):
elem, light = elem
else:
light = 1
times.setdefault(light, []).append(elem)
# Default watching times.
start_watching = start_watching or min(t[0] for t in times.values())
end_watching = end_watching or max(t[~0] for t in times.values())
# List intervals of light, merge them.
intervals = sorted(
(max(start_watching, start), min(end_watching, end))
for t in times.values()
for start, end in zip_longest(t[::2], t[1::2], fillvalue=end_watching)
if start < end_watching and end > start_watching
)
return sum((end - start).total_seconds() for start, end in merge_intervals(intervals))
def merge_intervals(intervals): # Note that it take sorted intervals.
res = []
for start, end in intervals:
if res and start <= res[~0][1]:
res[~0][1] = max(res[~0][1], end)
else:
res.append([start, end])
return res
if __name__ == '__main__':
print("Example:")
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
]) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 11, 0, 0), 2),
(datetime(2015, 1, 12, 11, 1, 0), 2),
]) == 70
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
]) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
]) == 40
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 0), 3),
(datetime(2015, 1, 12, 10, 1, 20), 3),
]) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 50)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 10)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 50)) == 0
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 20), 2),
(datetime(2015, 1, 12, 10, 1, 40), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 50
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 30), datetime(2015, 1, 12, 10, 1, 0)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 20), datetime(2015, 1, 12, 10, 1, 0)) == 40
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 0, 30)) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 1, 0)) == 40
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 0, 10)) == 0
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 20)) == 10
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 20)) == 10
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light([
(datetime(2015, 1, 11, 0, 0, 0), 3),
datetime(2015, 1, 12, 0, 0, 0),
(datetime(2015, 1, 13, 0, 0, 0), 3),
(datetime(2015, 1, 13, 0, 0, 0), 2),
datetime(2015, 1, 14, 0, 0, 0),
(datetime(2015, 1, 15, 0, 0, 0), 2),
], datetime(2015, 1, 10, 0, 0, 0), datetime(2015, 1, 16, 0, 0, 0)) == 345600
print("The forth mission in series is completed? Click 'Check' to earn cool rewards!")
Nov. 18, 2020