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
Metrics are stored long — one row per day, per metric, per value — because that shape survives new metrics being added. The dashboard needs them wide: one row per day with a column per metric. Not every metric is reported every day, and the collector occasionally sends the same day and metric twice.
Write pivot_rows(rows, index_field, column_field, value_field).
Function to write
pivot_rows(rows: list[dict], index_field: str, column_field: str, value_field: str) -> dictA dict with columns — the sorted column names — and rows, one rectangular wide row per index value.
How to approach it
Collect the two axes and the cells in one pass, then build the grid from them.
Sample cases
+ 2 held back until you submit
a metrics table
A repeated day and metric pair, a metric only one day has, a null value and a null index.
Input
Argument 1
| day | metric | value |
|---|---|---|
| 2026-03-01 | orders | 10 |
| 2026-03-01 | revenue | 250.5 |
| 2026-03-02 | orders | 4 |
| 2026-03-02 | orders | 3 |
| 2026-03-03 | refunds | 1 |
| 2026-03-01 | refunds | None |
| None | orders | 99 |
Argument 2
'day'Argument 3
'metric'Argument 4
'value'Returns
{
'columns': [
'orders',
'refunds',
'revenue'
],
'rows': [
{
'day': '2026-03-01',
'orders': 10,
'refunds': None,
'revenue': 250.5
},
{
'day': '2026-03-02',
'orders': 7,
'refunds': None,
'revenue': None
},
{
'day': '2026-03-03',
'orders': None,
'refunds': 1,
'revenue': None
}
]
}nothing to pivot
No rows means no columns and no wide rows, with both keys still present.
Input
Argument 1
[] (empty list)
Argument 2
'day'Argument 3
'metric'Argument 4
'value'Returns
{
'columns': [],
'rows': []
}one metric only
A single column still produces a wide table rather than the input unchanged.
Input
Argument 1
| day | metric | value |
|---|---|---|
| 2026-03-01 | orders | 1 |
| 2026-03-02 | orders | 2 |
Argument 2
'day'Argument 3
'metric'Argument 4
'value'Returns
{
'columns': [
'orders'
],
'rows': [
{
'day': '2026-03-01',
'orders': 1
},
{
'day': '2026-03-02',
'orders': 2
}
]
}Constraints
None, not zero. Nothing measured and a measured zero are different facts.Worked example
2026-03-02 reports orders twice, as 4 and 3. They are summed to 7 — the collector sent two partial counts, and keeping only one of them loses whichever it did not keep. A pivot that assigns rather than accumulates silently keeps the last one written, which depends on file order.
refunds is reported on 2026-03-03 and nowhere else, and on 2026-03-01 it arrives with a null value. Both days need a refunds cell — one empty, one holding 1 — and every other day needs one too. Filling those with zero would say refunds were measured and found to be none, which is a claim the data does not make and which makes an average over the column wrong.
What this tests
Building a rectangular result from sparse input, and the discipline of keeping absent distinct from zero. The two-pass structure — discover the axes, then fill the grid — is what makes the rectangle possible at all.
pivot_rows(rows: list[dict], index_field: str, column_field: str, value_field: str) -> dictSubmit 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.