Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Lightbulb Operating by colinmcnicholl
from datetime import datetime, timedelta
from typing import List, Optional, Union, Tuple
from collections import defaultdict
from itertools import zip_longest
def get_bulb_id_times_dict(els):
"""Input: els as a list containing datetime objects and possibly two tuples
of datetime objects and integer, the integer being the bulb number.
Output: A dictionary whose keys are integers representing the bulb numbers
and values as a list of datetime objects respresenting the on/off switching
times for the bulb.
"""
times = defaultdict(list)
for elem in els:
if isinstance(elem, tuple):
elem, light = elem
else:
light = 1
times[light].append(elem)
return times
def all_bulb_on_times_exceed_operating(time_on_list, operating):
"""Input:
- a list of datetime.timedelta objects representing the duration(s) in
seconds the bulb was on, for each period it was swiched on.
- a datetime.timedelta object being how long the bulb can work
in seconds.
Output: True if all the durations are greater than operating, else False.
"""
return all([time_on > operating for time_on in time_on_list])
def sum_light(els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
operating: Optional[timedelta] = None) -> int:
"""
how long the light bulb has been turned on
"""
times = get_bulb_id_times_dict(els)
on_durations_dict = {}
for k, v in times.items():
on_times = [(off - on) for on, off in zip(v[::2], v[1::2])]
on_durations_dict[k] = on_times
start_watching = start_watching or min(t[0] for t in times.values())
end_watching = end_watching or max(t[-1] for t in times.values())
if operating:
for bulb_id, time_on_durations in on_durations_dict.items():
need_to_trim = all_bulb_on_times_exceed_operating(time_on_durations, operating)
if need_to_trim:
start = times[bulb_id][0]
end = start + operating
times[bulb_id] = [start, end]
else:
continue
intervals = []
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:
intervals.append((max(start_watching, start), min(end_watching, end)))
intervals.sort()
return sum((end - start).total_seconds() for start, end in merge_intervals(intervals))
def merge_intervals(intervals):
"""Input: intervals as a list of datetime objects.
Output: a list of datetime intervals such that there are no
overlapping intervals.
"""
non_overlap_intervals = []
for start, end in intervals:
if non_overlap_intervals and start <= non_overlap_intervals[0][1]:
non_overlap_intervals[0][1] = max(non_overlap_intervals[0][1], end)
else:
non_overlap_intervals.append([start, end])
return non_overlap_intervals
if __name__ == '__main__':
print("Example:")
print(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),
],
start_watching=datetime(2015, 1, 12, 10, 0, 10),
end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=5)))
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(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, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(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, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(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, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(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, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
], 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(els=[
(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),
], start_watching=datetime(2015, 1, 10, 0, 0, 0), end_watching=datetime(2015, 1, 16, 0, 0, 0)) == 345600
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
], operating=timedelta(seconds=100)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
], operating=timedelta(seconds=5)) == 5
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),
], operating=timedelta(seconds=100)) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 30),
(datetime(2015, 1, 12, 10, 0, 30), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], operating=timedelta(seconds=100)) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 30),
(datetime(2015, 1, 12, 10, 0, 30), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], operating=timedelta(seconds=20)) == 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),
], operating=timedelta(seconds=10)) == 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),
], start_watching=datetime(2015, 1, 12, 10, 0, 20), operating=timedelta(seconds=100)) == 50
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),
], start_watching=datetime(2015, 1, 12, 10, 0, 20), operating=timedelta(seconds=10)) == 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),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=20)) == 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),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=10)) == 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),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=5)) == 10
print("The forth mission in series is completed? Click 'Check' to earn cool rewards!")
Feb. 3, 2021