Sunday, February 26, 2023
HomeSoftware EngineeringCalculating Easy Time Distinction in Python

Calculating Easy Time Distinction in Python


The problem

On this problem, you can be given a collection of instances at which an alarm goes off. Your job might 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 instances within the array are usually not in chronological order. Ignore duplicate instances, if any.

Examples:

# If the alarm goes off now, it is not going to go off for an additional 23 hours and 59 minutes.
remedy(["14:51"]) = "23:59"

# The max interval that the alarm is not going to 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 instances 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[0].exchange(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[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)

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 answer

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto