Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Concise algorithm solution in Speedy category for Lightbulb Start Watching by Igor_Sekretarev
from datetime import datetime
from typing import List, Optional
def sum_light(els: List[datetime], start_watching: Optional[datetime] = None) -> int:
return sum(
(max(start_watching or els[i+1], els[i+1]) -
max(start_watching or els[i], els[i])).total_seconds()
for i in range(0, len(els), 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, 5)))
assert sum_light(els=[
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
],
start_watching=datetime(2015, 1, 12, 10, 0, 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)) == 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, 11, 0, 0)) == 610
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, 0, 10)) == 600
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)) == 620
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, 10, 11),
datetime(2015, 1, 12, 12, 10, 11),
], datetime(2015, 1, 12, 12, 10, 11)) == 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, 11, 10, 11),
datetime(2015, 1, 12, 12, 10, 11),
], datetime(2015, 1, 12, 12, 9, 11)) == 60
print("The second mission in series is done? Click 'Check' to earn cool rewards!")
May 2, 2021
Comments: