The problem
On this problem, you can be given a sequence of occasions at which an alarm goes off. Your activity will likely be to find out the utmost time interval between alarms. Every alarm begins ringing initially of the corresponding minute and rings for precisely one minute. The occasions within the array will not be in chronological order. Ignore duplicate occasions, if any.
Examples:
# If the alarm goes off now, it is not going to go off for one more 23 hours and 59 minutes.
clear up(["14:51"]) = "23:59"
# The max interval that the alarm is not going to go off is 11 hours and 40 minutes.
clear up(["23:00","04:22","18:05","06:24"]) == "11:40"
Within the second instance, the alarm goes off 4 occasions in a day.
The answer in Python code
Possibility 1:
from datetime import datetime
def clear up(arr):
dts = [datetime(2000, 1, 1, *map(int, x.split(':'))) for x in sorted(arr)]
delta = max(int((b - a).total_seconds() - 60) for a, b in zip(dts, dts[1:] + [dts[0].exchange(day=2)]))
return '{:02}:{:02}'.format(*divmod(delta//60, 60))
Possibility 2:
def clear up(arr):
arr = sorted(int(m[:2]) * 60 + int(m[3:]) for m in set(arr))
arr += [arr[0] + 1440]
h, m = divmod(max(arr[i + 1] - arr[i] - 1 for i in vary(len(arr) - 1)), 60)
return "{:02}:{:02}".format(h, m)
Possibility 3:
def minutes(s):
return int(s[0:2]) * 60 + int(s[-2:])
def timeformat(m):
return "{:02d}:{:02d}".format(m // 60, m % 60)
def clear up(arr):
arr = listing(set(arr))
m = [minutes(arr) for arr in sorted(arr)]
distinction = [(m[(i+1)%len(m)] - a - 1) % (60*24) for i, a in enumerate(m)]
return(timeformat(max(distinction)))
Take a look at circumstances to validate our resolution
take a look at.it("Primary exams")
take a look at.assert_equals(clear up(["14:51"]), "23:59")
take a look at.assert_equals(clear up(["23:00","04:22","18:05","06:24"]),"11:40")
take a look at.assert_equals(clear up(["21:14", "15:34", "14:51", "06:25", "15:30"]),"09:10")