Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
An event feed arrives unordered and you want one summary per user: how many events, and the first and last timestamps.
Write summarise_runs(events, key_field) using itertools.groupby. It groups adjacent equal keys, which is the entire difficulty and the reason this is worth doing rather than reaching for a dict.
Function to write
summarise_runs(events: list[dict], key_field: str) -> list[dict]A list of per-key summaries, ordered by key, with the first and last timestamps.
How to approach it
Count the groups the starter produces for two distinct users.
Sample cases
+ 2 held back until you submit
a key whose events are not adjacent
User a appears either side of user b, so an ungrouped input produces three groups for two users.
Input
Argument 1
| u | at |
|---|---|
| b | 2026-09-01T10:00:00Z |
| a | 2026-09-01T09:00:00Z |
| a | 2026-09-01T11:00:00Z |
Argument 2
'u'Returns
| key | events | first | last |
|---|---|---|---|
| a | 2 | 2026-09-01T09:00:00Z | 2026-09-01T11:00:00Z |
| b | 1 | 2026-09-01T10:00:00Z | 2026-09-01T10:00:00Z |
already grouped and ordered
Input that is already sorted comes back as one group with the right first and last.
Input
Argument 1
| u | at |
|---|---|
| a | 2026-09-01T09:00:00Z |
| a | 2026-09-01T10:00:00Z |
Argument 2
'u'Returns
| key | events | first | last |
|---|---|---|---|
| a | 2 | 2026-09-01T09:00:00Z | 2026-09-01T10:00:00Z |
no events
An empty feed summarises to an empty list rather than raising on an empty group.
Input
Argument 1
[] (empty list)
Argument 2
'u'Returns
[] (empty list)
Constraints
{'key': ..., 'events': <count>, 'first': ..., 'last': ...}.first and last come from the timestamp order within the key, not from input order.groupby groups adjacent runs, so the input has to be sorted by exactly the same key first.None rather than raising, and the sort must survive comparing it with a string.Worked example
Three events arrive: user b, then user a, then user a again.
Ungrouped, groupby yields three groups for two users — a twice, because its two events are not adjacent. The counts are all wrong and nothing raises, which is why this is a bug people ship. Sorting by u first collapses them to two groups, and sorting by at within that gives first and last for free.
What this tests
That groupby has a precondition the caller is responsible for, and that laziness has a cost: a group is a live view, so storing one for later gives you an empty list with no error to explain it.
summarise_runs(events: list[dict], key_field: str) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.