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
A loader rejects rows it cannot parse and returns a report of what it dropped. The report can be started fresh, or continued from one the caller already has.
Write collect_rejected(rows, reason, batch=None). It appends one record per rejected row and returns the report. The starter code has the bug this question is about; run it twice before you change anything.
Function to write
collect_rejected(rows: list, reason: str, batch: list | None = None) -> listA list of rejection records, in input order, starting from `batch` when one was supplied.
How to approach it
Run the starter twice with no batch. The second result is the question.
Sample cases
+ 2 held back until you submit
one rejected row
The ordinary case: one row in, one rejection record out, carrying the reason.
Input
Argument 1
| id | amount |
|---|---|
| 1042 | 12,5 |
Argument 2
'bad amount'Returns
[
{
'row': {
'id': 1042,
'amount': '12,5'
},
'reason': 'bad amount'
}
]a second, independent call
Nothing from the previous call may appear here. This is the case the default argument breaks.
Input
Argument 1
| id | amount |
|---|---|
| 1043 | None |
Argument 2
'missing amount'Returns
[
{
'row': {
'id': 1043,
'amount': None
},
'reason': 'missing amount'
}
]nothing to reject
An empty input is a valid answer, not an error — an empty report comes back.
Input
Argument 1
[] (empty list)
Argument 2
'bad amount'Returns
[] (empty list)
Constraints
{'row': <the row>, 'reason': <the reason>}, in input order.batch is not supplied, the call starts from an empty report.batch must not see each other's rows. This is the whole question.batch is supplied, its rows come first and the new ones are appended after them.[] supplied a batch, and must not be treated as though they supplied nothing.Worked example
Call it once with bad amount and once with missing amount, supplying no batch either time. Each call must return exactly one record.
With the starter's batch=[] default the second call returns two records, because the default list was built once when the function was defined and both calls appended to the same object. Nothing errors; the report simply grows all day in a long-running worker.
What this tests
That a default argument is evaluated once, at definition, so a mutable default is shared state — and that the sentinel is tested with is None rather than with truthiness, because an empty list the caller meant is not the same as no argument.
collect_rejected(rows: list, reason: str, batch: list | None = None) -> listSubmit 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.