Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Lightbulb End Watching by pokosasa
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:
if len(els) == 0:
return 0
if end_watching and end_watching <= els[0]:
return 0
if len(els) == 1:
return int((end_watching - max(els[0], start_watching)).total_seconds())
if start_watching and els[1] <= start_watching:
return sum_light(els[2:], start_watching, end_watching)
start_point = max(els[0], start_watching) if start_watching else els[0]
end_point = min(els[1],end_watching) if end_watching else els[1]
return int((end_point - start_point).total_seconds()) + sum_light(els[2:], start_watching, end_watching)
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. 17, 2020
Comments: