Time calculator
A clock only has 24 hours - the 25th hour is really 1:00 the next day. The
% (modulo) operator is exactly the tool for that wraparound: total_minutes % (24 * 60) gives you the minute-of-day no matter how many times past
midnight total_minutes has rolled, and total_minutes // (24 * 60)
(integer division) tells you how many full days it rolled past.
The trick to this whole problem is working in one flat unit - total minutes
since midnight - instead of juggling hours and minutes separately. Convert
the start time to minutes, add the duration, then convert back: the hour is
minutes // 60 and the minute is minutes % 60.
Your task: write add_duration(start_hour, start_min, duration_min).
start_hour is 0-23 and start_min is 0-59 (24-hour time).
duration_min is how many minutes to add. Return a tuple (end_hour, end_min, days_later), where days_later is 0 if the end time lands on
the same day, 1 if it rolls past one midnight, and so on.
You'll practice:
- Converting hours and minutes into one flat total, and back
- Using
// and % together to wrap a value and count how many times it wrapped
Related reading: Modular Arithmetic: Clock Math →