Thursday, September 21, 2023
HomeSoftware EngineeringCalculating Easy Time Distinction in Python

# Calculating Easy Time Distinction in Python

## The problem#

On this problem, you can be given a sequence of occasions at which an alarm goes off. Your activity will probably be to find out the utmost time interval between alarms. Every alarm begins ringing in the beginning of the corresponding minute and rings for precisely one minute. The occasions within the array usually are not in chronological order. Ignore duplicate occasions, if any.

Examples:

``````# If the alarm goes off now, it won't go off for an additional 23 hours and 59 minutes.
remedy(["14:51"]) = "23:59"

# The max interval that the alarm won't go off is 11 hours and 40 minutes.
remedy(["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#

Choice 1:

``````from datetime import datetime

def remedy(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.substitute(day=2)]))
return '{:02}:{:02}'.format(*divmod(delta//60, 60))
``````

Choice 2:

``````def remedy(arr):
arr = sorted(int(m[:2]) * 60 + int(m[3:]) for m in set(arr))
arr += [arr + 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)
``````

Choice 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 remedy(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#

``````check.it("Fundamental checks")
check.assert_equals(remedy(["14:51"]), "23:59")
check.assert_equals(remedy(["23:00","04:22","18:05","06:24"]),"11:40")
check.assert_equals(remedy(["21:14", "15:34", "14:51", "06:25", "15:30"]),"09:10")
``````
RELATED ARTICLES