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
Finance wants revenue per region from the raw orders table. The table is loaded from three upstream systems and the amount column was never typed: some rows carry a number, some carry the number as a string, and a few carry something that is not a number at all. The region column is optional.
Write revenue_by_region(orders). Return one row per region, biggest first.
Function to write
revenue_by_region(orders: list[dict]) -> list[dict]One dict per region with the keys region, revenue and orders, ordered by revenue descending.
How to approach it
Parse the amount first and return None when it cannot be used, then group what survives.
Sample cases
+ 2 held back until you submit
a day of orders
Mixed casing, an amount sent as a string, an unparseable amount, a null region and a missing one.
Input
Argument 1
[
{
'order_id': 'o1',
'region': 'APAC',
'amount': 120.5
},
{
'order_id': 'o2',
'region': 'apac',
'amount': '80.25'
},
{
'order_id': 'o3',
'region': 'EMEA',
'amount': 200
},
{
'order_id': 'o4',
'region': None,
'amount': 15
},
{
'order_id': 'o5',
'region': 'EMEA',
'amount': 'not a number'
},
{
'order_id': 'o6',
'region': ' emea ',
'amount': 49.75
},
{
'order_id': 'o7',
'amount': 10
}
]Returns
| region | revenue | orders |
|---|---|---|
| emea | 249.75 | 2 |
| apac | 200.75 | 2 |
| unknown | 25 | 2 |
no orders at all
A quiet day returns an empty list rather than raising or returning None.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
one region only
A single region still comes back as a list of one row, with the count alongside the total.
Input
Argument 1
| order_id | region | amount |
|---|---|---|
| s1 | NA | 10 |
| s2 | NA | 20 |
Returns
| region | revenue | orders |
|---|---|---|
| na | 30 | 2 |
Constraints
True is not the number one. A boolean amount is unusable.unknown rather than being dropped.region, revenue and orders, where orders is the number of orders that contributed.Worked example
APAC arrives twice: once as 120.5 and once as the string 80.25. Both are real revenue, so the region totals 200.75 across 2 orders. Skipping the string version because it is not a number understates the region by forty percent, and nothing in the output would say so.
Order o5 has an amount of not a number. It is skipped, which means EMEA counts 2 orders rather than 3 even though three rows mention it. That is the correct answer and it is also the number somebody will query about, so the rule is worth stating out loud rather than leaving implicit in the code.
What this tests
Coercing values you did not type, and keeping a count beside a sum so the result can be sanity-checked. The boolean rule is the one that catches people: in Python True really is an int, so a naive numeric check accepts it.
revenue_by_region(orders: list[dict]) -> 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.