Exercise · intermediate · 25 min
Validate quantities and prices against ranges
An inventory feed has impossible quantities, zero prices and unknown countries. Apply the validation rules in order and report the first rule each row breaks.
The rules
These rules decide what a correct result is. Your answer is checked on this dataset and on hidden datasets that follow the same rules.
Input inventory(id, sku, quantity, unit_price, country) — text. id is unique; sku is always present. Normalize country: remove ASCII whitespace at both ends, then uppercase. Quarantine each invalid row with the FIRST failing reason, checked in this order: 1. missing_quantity quantity is missing 2. bad_quantity quantity is not a whole number: optional "-" then digits only 3. quantity_out_of_range quantity is less than 1 or greater than 500 4. bad_price unit_price is missing, or is not: optional "-", digits, optionally "." and 1–2 digits 5. price_out_of_range unit_price is zero or negative 6. bad_country country is not one of US, CA, MX (a missing country is also bad_country) Outputs (row order does not matter) clean(id text, sku text, quantity int64, unit_price decimal(10,2), country text) exceptions(id text, reason text)
The data
inventory · 21 rows, first 12 shown
| id | sku | quantity | unit_price | country |
|---|---|---|---|---|
| invybqz | SKU-6252 | 1 | 760.28 | ·CA |
| invf1ge | SKU-7738 | 501 | 580.05 | MX |
| invoi41 | SKU-0003 | 12 | -1.00 | USA |
| inveree | SKU-5191 | 23 | 572.57 | ·CA |
| inv29z8 | SKU-0001 | 0 | 0.00 | UK |
| invvnhm | SKU-4924 | 434 | 524.17 | US |
| invkmms | SKU-4496 | 7.5 | 573.38 | CA |
| inveec2 | SKU-6847 | 1 | 0 | us |
| invezdp | SKU-0936 | 436 | 956.62 | US |
| invgt2u | SKU-9259 | 440 | 1,200.00 | MX |
| inv4nh6 | SKU-6643 | 72 | 1,200.00 | MX |
| invi6w4 | SKU-0002 | missing | abc | missing |
What you practise
- validation rules
- ranges
- type conversion
- allowed values
- quarantine
Your answer is also checked on hidden datasets generated from the same rules.
Sign in to bookmark this and keep private notes.