AI-Assisted Coding Lab · Find Free Time in a Calendar

45:00left of 45:00

Find Free Time in a Calendar

The scheduling assistant needs a person's free time for a day, worked out from their busy intervals. The README has the rules, and free_time.py has the time helpers and the outline. Build it with the assistant: accept, edit, or reject each suggestion, and keep the tests green.

free_time.py1 of 3

Files

free_time.py

Python · Editable
DAY_START = "09:00"
DAY_END = "17:00"
def to_minutes(hhmm):
"""'09:30' -> 570"""
hours, minutes = hhmm.split(":")
return int(hours) * 60 + int(minutes)
def to_clock(minutes):
"""570 -> '09:30'"""
return f"{minutes // 60:02d}:{minutes % 60:02d}"
def free_slots(busy, day_start=DAY_START, day_end=DAY_END, min_minutes=30):
"""Free slots in the working day that last at least min_minutes, as ("HH:MM", "HH:MM") pairs."""
start, end = to_minutes(day_start), to_minutes(day_end)
intervals = [(to_minutes(a), to_minutes(b)) for a, b in busy]
gaps = find_gaps(merge_busy(intervals), start, end)
return [(to_clock(a), to_clock(b)) for a, b in gaps]
def merge_busy(intervals):
"""Merges overlapping and touching (start, end) intervals. Returns them sorted by start."""
raise NotImplementedError
def find_gaps(merged, start, end):
"""The gaps between sorted, non-overlapping busy intervals, clipped to start and end."""
raise NotImplementedError

Test Output

No runs yet.

Assistant

Scripted
  1. Send the first prompt to start.

Next Prompt

Write merge_busy. Overlapping and touching intervals should merge into one.