Complete worked example
python
def inspect_example(rows, maximum_bad_percent, allow_empty):
if type(maximum_bad_percent) is not int or not 0 <= maximum_bad_percent <= 100:
raise ValueError('integer percentage required')
accepted = [r for r in rows if isinstance(r.get('id'), str) and r['id'] and type(r.get('amount_cents')) is int and r['amount_cents'] >= 0]
bad = len(rows) - len(accepted)
unique = len({r['id'] for r in accepted}) == len(accepted)
within_budget = bad * 100 <= len(rows) * maximum_bad_percent
nonempty = bool(rows) or allow_empty
can_publish = unique and within_budget and nonempty
return {'accepted': len(accepted), 'quarantined': bad, 'canPublish': can_publish}- Reasoning
- The program first divides rows into accepted and rejected collections. It then calculates uniqueness, bad-row budget and nonempty policy independently, returning both observations and canPublish. The budget compares rejected count times one hundred with total count times the configured percentage.