Your question is Format Data for UI Display. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Crunchyroll receives a flat array of episode records, but its watchlist UI displays episodes grouped by series and season. Implement a formatter that creates this nested display structure and derives a progress percentage and viewing status for every episode.
Implement format_watchlist(items), where items is a list of dictionaries. Each dictionary contains:
series_id: stringseries_title: stringseason: positive integerepisode: positive integerepisode_title: stringduration_seconds: positive integerposition_seconds: nonnegative integerwatched: booleanReturn a list of series dictionaries sorted by series_title without regard to case. Each series must contain seriesId, title, and seasons. Each season must contain seasonNumber and an episodes list. Sort seasons numerically and episodes numerically.
Each formatted episode must contain episodeNumber, title, progressPercent, and status. A watched episode has status Watched and progress 100. Otherwise, progress is the integer percentage of position_seconds / duration_seconds, capped at 100. Status is In Progress when progress is greater than zero, and Not Started otherwise.
def format_watchlist(items):