Revenue rollup from nested JSON orders
A marketplace exports its orders as nested JSON. Finance in Zurich needs the revenue per customer country in US dollars. The input has two lines: line 1 is a JSON object of exchange rates ({"EUR": 1.08, "USD": 1}, the multiplier that converts one unit of that currency to USD), line 2 is a JSON array of orders.
Each order looks like {"id": 7, "status": "paid", "currency": "EUR", "customer": {"name": "...", "country": "DE"}, "items": [{"sku": "A", "qty": 2, "price": 40, "discount_pct": 10}], "shipping": 5}. The discount_pct key of an item and the shipping key of an order may be missing (treat as 0). Only orders with status paid or shipped count. All prices and shipping are in the order's own currency.
The local total of an order is the sum over its items of qty * price * (100 - discount_pct) / 100, plus the shipping. Its USD value is the local total times the currency's rate. For each country, sum the USD values of its orders first and round only once at the end.
Task: print one line per country that has at least one counted order, as CC amount orders=k (amount with exactly 2 decimals, k the number of counted orders), sorted by amount descending and then country code ascending. If no order counts, print No revenue.
Sample tests
Input
{"EUR":1.25,"USD":1,"PKR":0.0035}
[{"id":1,"status":"paid","currency":"EUR","customer":{"name":"Anna","country":"DE"},"items":[{"sku":"A","qty":2,"price":40,"discount_pct":10},{"sku":"B","qty":1,"price":20}],"shipping":8},{"id":2,"status":"cancelled","currency":"USD","customer":{"name":"Bo","country":"US"},"items":[{"sku":"A","qty":9,"price":100}]},{"id":3,"status":"shipped","currency":"PKR","customer":{"name":"Hira","country":"PK"},"items":[{"sku":"C","qty":3,"price":2500,"discount_pct":20}],"shipping":300},{"id":4,"status":"paid","currency":"USD","customer":{"name":"Cy","country":"US"},"items":[{"sku":"D","qty":1,"price":99}]},{"id":5,"status":"paid","currency":"EUR","customer":{"name":"Jan","country":"DE"},"items":[],"shipping":4}]Expected output
DE 130.00 orders=2 US 99.00 orders=1 PK 22.05 orders=1
Input
{"USD":1}
[{"id":1,"status":"refunded","currency":"USD","customer":{"name":"X","country":"CA"},"items":[{"sku":"A","qty":1,"price":10}]}]Expected output
No revenue
Your answer is also checked against 2 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
