Exercise 2 solution from datetime import datetime def to_iso(text): try: dt = datetime.strptime(text, "%d %b %Y %I:%M %p") return dt.isoformat(timespec="minutes") except ValueError: return "invalid: " + text tests = [ "05 Mar 2026 09:07 PM", "05 Mar 2026 12:00 AM", "05 Mar 2026 12:00 PM", "05 Mar 2026 13:00 PM", "31 Apr 2026 01:00 AM", ] for t in tests: print(t, "->", to_iso(t)) Output (run, and exactly as shown): 05 Mar 2026 09:07 PM -> 2026-03-05T21:07 05 Mar 2026 12:00 AM -> 2026-03-05T00:00 05 Mar 2026 12:00 PM -> 2026-03-05T12:00 05 Mar 2026 13:00 PM -> invalid: 05 Mar 2026 13:00 PM 31 Apr 2026 01:00 AM -> invalid: 31 Apr 2026 01:00 AM Explanation: on a 12-hour clock, 12 AM is midnight (hour 0) and 12 PM is noon (hour 12); %I and %p handle both correctly. %I only accepts 01-12, so 13:00 PM fails, and April has 30 days, so 31 Apr fails. Both failures raise ValueError, which the function turns into a readable message instead of crashing.