Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
add end time to els; min(end_watching or end, ...) solution in Clear category for Lightbulb End Watching by Phil15
from datetime import datetime
from typing import List, Optional
def sum_light(
els: List[datetime],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
) -> int:
"""how long the light bulb has been turned on"""
els.append(end_watching)
return sum(
(
min(end_watching or end, max(start_watching or end, end))
- min(end_watching or end, max(start_watching or start, start))
).total_seconds()
for start, end in zip(els[::2], els[1::2])
)
if __name__ == '__main__':
print("Example:")
print(sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10)))
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 7)) == 7
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 3),
datetime(2015, 1, 12, 10, 0, 10)) == 7
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
datetime(2015, 1, 12, 10, 0, 10),
datetime(2015, 1, 12, 10, 0, 20)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 30, 0),
datetime(2015, 1, 12, 11, 0, 0)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 0)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 10)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 9, 50, 0),
datetime(2015, 1, 12, 10, 0, 10)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 9, 0, 0),
datetime(2015, 1, 12, 10, 5, 0)) == 300
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
datetime(2015, 1, 12, 11, 10, 10),
],
datetime(2015, 1, 12, 11, 5, 0),
datetime(2015, 1, 12, 12, 0, 0)) == 310
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 11, 5, 0),
datetime(2015, 1, 12, 11, 10, 0)) == 300
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 10, 10, 0),
datetime(2015, 1, 12, 11, 0, 10)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 10, 10),
datetime(2015, 1, 12, 11, 0, 0),
],
datetime(2015, 1, 12, 9, 10, 0),
datetime(2015, 1, 12, 10, 20, 20)) == 610
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 10, 0),
datetime(2015, 1, 12, 10, 20, 20)) == 1220
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 9, 0),
datetime(2015, 1, 12, 10, 0, 0)) == 0
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
],
datetime(2015, 1, 12, 9, 9, 0),
datetime(2015, 1, 12, 10, 0, 10)) == 10
print("The third mission in series is completed? Click 'Check' to earn cool rewards!")
Nov. 10, 2020
Comments: