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
The daily digest email lists the top products by revenue. It is currently a sort and a slice, and support keeps getting mail asking why two products swapped places on a day when neither sold anything — they are tied, and the sort is deciding between them by arrival order.
Write top_n(rows, field, n, tie_break). Return the n highest rows, ordered.
Function to write
top_n(rows: list[dict], field: str, n: int, tie_break: str) -> list[dict]Copies of the n highest-ranked rows, ordered by field descending then tie_break ascending.
How to approach it
Score each row first and drop the unrankable ones, then sort on a two-part key.
Sample cases
+ 2 held back until you submit
a leaderboard with a tie
Two products tie on 500.0, one revenue arrived as a string, and one row has no revenue at all.
Input
Argument 1
[
{
'sku': 'C3',
'revenue': 500
},
{
'sku': 'A1',
'revenue': 900
},
{
'sku': 'B2',
'revenue': 500
},
{
'sku': 'D4',
'revenue': '900'
},
{
'sku': 'E5',
'revenue': 120
},
{
'sku': 'F6'
}
]Argument 2
'revenue'Argument 3
3Argument 4
'sku'Returns
| sku | revenue |
|---|---|
| A1 | 900 |
| B2 | 500 |
| C3 | 500 |
asking for none
A top-zero report is empty rather than the whole list or an error.
Input
Argument 1
[
{
'sku': 'C3',
'revenue': 500
},
{
'sku': 'A1',
'revenue': 900
},
{
'sku': 'B2',
'revenue': 500
},
{
'sku': 'D4',
'revenue': '900'
},
{
'sku': 'E5',
'revenue': 120
},
{
'sku': 'F6'
}
]Argument 2
'revenue'Argument 3
0Argument 4
'sku'Returns
[] (empty list)
asking for more than exist
Requesting more rows than there are returns everything usable, without padding.
Input
Argument 1
[
{
'sku': 'C3',
'revenue': 500
},
{
'sku': 'A1',
'revenue': 900
},
{
'sku': 'B2',
'revenue': 500
},
{
'sku': 'D4',
'revenue': '900'
},
{
'sku': 'E5',
'revenue': 120
},
{
'sku': 'F6'
}
]Argument 2
'revenue'Argument 3
50Argument 4
'sku'Returns
| sku | revenue |
|---|---|
| A1 | 900 |
| B2 | 500 |
| C3 | 500 |
| E5 | 120 |
Constraints
field, highest first.tie_break field sorts earlier alphabetically comes first. Compare the tie-break as text.field is missing, null, a string, or a boolean has no rank. Leave it out entirely rather than treating it as zero.n less than or equal to zero returns an empty list.n larger than the number of usable rows returns all of them, with no padding.Worked example
C3 and B2 both have revenue of 500.0. Ranked by revenue alone, whichever appeared first in the input wins, so the digest reorders itself whenever the upstream file is written in a different order. With the tie-break, B2 is always second and C3 always third.
D4 sends its revenue as the string 900, which reads like the highest value on the table. It is left out, because a leaderboard that silently coerces types is one that ranks 90 above 900 the first time the strings differ in length. Dropping it is a decision the constraint makes explicit rather than a bug.
What this tests
Sorting on a composite key with two directions, and understanding that Python's sort is stable — which means the input order becomes the tie-break unless you give it one, quietly turning an ordering bug into a data-arrival bug.
top_n(rows: list[dict], field: str, n: int, tie_break: 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.