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
Raw rows arrive as dicts of strings. Everything downstream would rather have a record whose fields are named, typed and complete — so the conversion happens once, here, and after it nobody writes a defensive .get() again.
Write to_orders(rows) using a dataclass for the record shape. Return the records that satisfy the contract and the rows that cannot.
Function to write
to_orders(rows: list[dict]) -> dictA dict with the contract-satisfying `records` as plain dicts, and the `rejected` rows.
How to approach it
Write the dataclass first. The conversion follows from its fields.
Sample cases
+ 2 held back until you submit
a complete row
Every field present: the record comes back typed, with no warnings.
Input
Argument 1
| order_id | amount | buyer | region |
|---|---|---|---|
| 1042 | 12.50 | Amir | eu |
Returns
{
'records': [
{
'order_id': 1042,
'amount': '12.50',
'buyer': 'Amir',
'region': 'eu',
'warnings': []
}
],
'rejected': []
}optional fields missing
Defaults are applied and each one is recorded as a warning rather than applied silently.
Input
Argument 1
| order_id | amount |
|---|---|
| 1043 | 5.00 |
Returns
{
'records': [
{
'order_id': 1043,
'amount': '5.00',
'buyer': 'unknown',
'region': 'unknown',
'warnings': [
'buyer defaulted',
'region defaulted'
]
}
],
'rejected': []
}no rows
An empty batch produces two empty lists, and does not build a record out of nothing.
Input
Argument 1
[] (empty list)
Returns
{
'records': [],
'rejected': []
}Constraints
order_id (int), amount (a decimal string), buyer and region (text, defaulting to unknown), and warnings (a list).order_id and amount are required. Missing, blank, or unconvertible means the row is rejected as {'row': <index>, 'missing': [<field>]}.amount is parsed with Decimal and returned as a string, so the digits survive and the result can be serialised.<field> defaulted to warnings, so the default is visible rather than silent.order_id, amount, buyer, region, warnings.Worked example
A row with order_id of 1043 and amount of 5.00, and nothing else, satisfies the contract.
It comes back with buyer and region both unknown — and with two warnings saying so. That second half is the design decision: a default applied silently is indistinguishable downstream from a value the source actually sent, and the warning is what lets somebody notice the producer stopped sending a column.
What this tests
That a dataclass is a contract rather than a shorthand for __init__, that a mutable field default needs default_factory or every instance shares one list, and that a default is a decision worth recording.
to_orders(rows: list[dict]) -> 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.