runtime
319 / 500
question 319 Iterators & Generators · hard

Activity sessions per contributor

A contributor's events belong to one session while they come no more than gap_minutes apart; a longer gap starts a new session. Write sessions(events, gap_minutes): return one (actor, start, end, count) tuple per session, ordered by start time and then actor. Events arrive in no particular order.

Functionsessions(events, gap_minutes)
eventsdictionaries with actor and created_at ("2025-03-03T12:04:14Z")
Returnsa list of (actor, start, end, count), start and end as the original timestamps; a gap of exactly gap_minutes stays in the same session
Examples
callsessions([{'actor': 'data_fox514', 'created_at': '2025-03-03T12:04:14Z'}, {'actor': 'data_fox514', 'created_at': '2025-03-03T12:20:00Z'}, {'actor': 'data_fox514', 'created_at': '2025-03-03T14:00:00Z'}], 30)returns[('data_fox514', '2025-03-03T12:04:14Z', '2025-03-03T12:20:00Z', 2), ('data_fox514', '2025-03-03T14:00:00Z', '2025-03-03T14:00:00Z', 1)]
callsessions([{'actor': 'stackkoi785', 'created_at': '2025-03-03T12:06:10Z'}, {'actor': 'data_fox514', 'created_at': '2025-03-03T12:04:14Z'}, {'actor': 'stackkoi785', 'created_at': '2025-03-03T12:10:00Z'}, {'actor': 'data_fox514', 'created_at': '2025-03-03T12:08:00Z'}], 15)returns[('data_fox514', '2025-03-03T12:04:14Z', '2025-03-03T12:08:00Z', 2), ('stackkoi785', '2025-03-03T12:06:10Z', '2025-03-03T12:10:00Z', 2)]
callsessions([], 30)returns[]

Loading your progress…

Activity sessions per contributor — Python practice · Runtime