Problems › Python › production
Defects per million parts
Quality reports the defect rate in parts per million, because "0.03%" is harder to compare across lines than "300 ppm".
- Rounded to the nearest whole ppm, halves going up.
- A run that produced nothing has no rate: return 0.
defect_ppm(defects: int, produced: int) → int
Where you start
def defect_ppm(defects: int, produced: int) -> int:
Worked examples
| Call | Result |
|---|---|
defect_ppm(3, 10000) | 300 |
defect_ppm(0, 10000) | 0 |
defect_ppm(1, 3) | 333333 |
defect_ppm(2, 3) | 666667 |
Hint
defects * 1000000 / produced, with half the divisor added before dividing so it rounds instead of truncating.
Reference solution in Python
def defect_ppm(defects: int, produced: int) -> int:
if produced <= 0:
return 0
return (defects * 1000000 + produced // 2) // produced