Arrival Times
Source: Dec 2023, Task 2 (DTU 02002/02003). Study solution — not an official answer key.
Problem
Scheduled times are strings 'hh:mm' (24-hour). Add the same delay in minutes to each, wrapping past midnight, and format again as 'hh:mm' with two digits.
Write arrival_times(schedule, delay) returning a list of strings.
Example
>>> arrival_times(['12:37', '08:10'], 25)
['13:02', '08:35']
Approach
Convert each time to minutes since midnight, add delay, mod 24*60, then format with zero-padded hours and minutes.
Solution
Show solution Hide solution
Solution
def arrival_times(schedule, delay):
result = []
for time_str in schedule:
hours, minutes = map(int, time_str.split(":"))
total = (hours * 60 + minutes + delay) % (24 * 60)
result.append(f"{total // 60:02d}:{total % 60:02d}")
return result