Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
One liner (long line) solution in Creative category for Lightbulb End Watching by CDG.Axel
from datetime import datetime
from typing import List, Optional
def sum_light(els: List[datetime],
start_watching: Optional[datetime] = datetime.min,
end_watching: Optional[datetime] = datetime.max) -> int:
"""
how long the light bulb has been turned on
min(b, end_watching) replace if b < end_watching: b = end_watching
max(a, start_watching) replace if a < start_watching: a = start_watching
max(0, ...) we need for cases whem start_watching > b or end_watching < a
"""
return sum(max(0, (min(b, end_watching) - max(a, start_watching)).total_seconds())
for a, b in zip(els[::2], els[1::2]+[end_watching]))
Nov. 27, 2021
Comments: