Drill

ProblemsPython › events

How long is this event in minutes

easyeventsPython

An event start and end are given as four-digit HHMM integers. Return the duration in minutes.

event_length_mins(start_hhmm: int, end_hhmm: int) → int

Solve it in the editor →

Where you start

def event_length_mins(start_hhmm: int, end_hhmm: int) -> int:
    

Worked examples

CallResult
event_length_mins(930, 1130)120
event_length_mins(0, 100)60
event_length_mins(2300, 100)120
event_length_mins(1200, 1200)0

Hint

Convert each to minutes (hh × 60 + mm); the answer is the difference mod 1440, so add 1440 when it comes out negative.

Reference solution in Python
def event_length_mins(start_hhmm: int, end_hhmm: int) -> int:
    s_min = (start_hhmm // 100) * 60 + (start_hhmm % 100)
    e_min = (end_hhmm // 100) * 60 + (end_hhmm % 100)
    diff = e_min - s_min
    return ((diff % 1440) + 1440) % 1440

The same problem in another language

More events problems in Python