broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    dose = min(Fraction(str(x['mg_per_kg'])) * Fraction(str(x['weight_kg'])), x['max_mg'])
    per_ml = Fraction(x['conc_mg'], x['conc_ml'])
    vol = dose / per_ml
    grad = {1: Fraction(1, 100), 5: Fraction(1, 5), 10: Fraction(1, 2)}[x['syringe_ml']]
    v = math.floor(vol / grad + Fraction(1, 2)) * grad
    if v * per_ml > x['max_mg']:
        v = math.floor(vol / grad) * grad
    if v >= x['syringe_ml']:
        return {'status': 'SPLIT', 'syringes': math.ceil(v / x['syringe_ml'])}
    return {'status': 'OK', 'volume_ml': float(v), 'dose_mg': float(v * per_ml)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('suspension per 5 mL label',
   {'weight_kg': 13, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 9, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 3.8, 'dose_mg': 91.2}),
  ('10 mL syringe graduations',
   {'weight_kg': 21, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 195, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 180.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 39, 'mg_per_kg': 5, 'max_mg': 200, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.1', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 5, 'max_mg': 1000, 'conc_mg': 10, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 31, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 14, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 10, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.2, 'dose_mg': 100.8}),
  ('10 mL syringe graduations',
   {'weight_kg': 22, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 215, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 43, 'mg_per_kg': 5, 'max_mg': 220, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.2', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 10, 'max_mg': 1000, 'conc_mg': 20, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 32, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 15, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 225.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 11, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.6, 'dose_mg': 110.4}),
  ('10 mL syringe graduations',
   {'weight_kg': 23, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.0, 'dose_mg': 160.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 235, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 47, 'mg_per_kg': 5, 'max_mg': 240, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.05', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 30, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 34, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 16, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 12, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('10 mL syringe graduations',
   {'weight_kg': 24, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.5, 'dose_mg': 170.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 255, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 51, 'mg_per_kg': 5, 'max_mg': 260, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.15', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 20, 'max_mg': 1000, 'conc_mg': 40, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 36, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 17, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 13, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('10 mL syringe graduations',
   {'weight_kg': 25, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 9.0, 'dose_mg': 180.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 275, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 55, 'mg_per_kg': 5, 'max_mg': 280, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.0, 'dose_mg': 280.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.12', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 25, 'max_mg': 1000, 'conc_mg': 50, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 37, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    dose = min(Fraction(str(x['mg_per_kg'])) * Fraction(str(x['weight_kg'])), x['max_mg'])
    per_ml = Fraction(x['conc_mg'], x['conc_ml'])
    vol = dose / per_ml
    grad = {1: Fraction(1, 100), 5: Fraction(1, 5), 10: Fraction(1, 2)}[x['syringe_ml']]
    v = math.floor(vol / grad + Fraction(1, 2)) * grad
    if v * per_ml > x['max_mg']:
        v = math.floor(vol / grad) * grad
    if vol > x['syringe_ml']:
        return {'status': 'SPLIT', 'syringes': math.ceil(v / x['syringe_ml'])}
    return {'status': 'OK', 'volume_ml': float(v), 'dose_mg': float(v * per_ml)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('suspension per 5 mL label',
   {'weight_kg': 13, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 9, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 3.8, 'dose_mg': 91.2}),
  ('10 mL syringe graduations',
   {'weight_kg': 21, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 195, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 180.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 39, 'mg_per_kg': 5, 'max_mg': 200, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.1', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 5, 'max_mg': 1000, 'conc_mg': 10, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 31, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 14, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 10, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.2, 'dose_mg': 100.8}),
  ('10 mL syringe graduations',
   {'weight_kg': 22, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 215, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 43, 'mg_per_kg': 5, 'max_mg': 220, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.2', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 10, 'max_mg': 1000, 'conc_mg': 20, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 32, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 15, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 225.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 11, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.6, 'dose_mg': 110.4}),
  ('10 mL syringe graduations',
   {'weight_kg': 23, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.0, 'dose_mg': 160.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 235, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 47, 'mg_per_kg': 5, 'max_mg': 240, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.05', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 30, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 34, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 16, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 12, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('10 mL syringe graduations',
   {'weight_kg': 24, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.5, 'dose_mg': 170.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 255, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 51, 'mg_per_kg': 5, 'max_mg': 260, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.15', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 20, 'max_mg': 1000, 'conc_mg': 40, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 36, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 17, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 13, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('10 mL syringe graduations',
   {'weight_kg': 25, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 9.0, 'dose_mg': 180.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 275, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 55, 'mg_per_kg': 5, 'max_mg': 280, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.0, 'dose_mg': 280.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.12', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 25, 'max_mg': 1000, 'conc_mg': 50, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 37, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A dose that exactly fills the syringe is sent for splitting.

Input {weight_kg, mg_per_kg, max_mg, conc_mg, conc_ml (label strength conc_mg per conc_ml), syringe_ml (1, 5 or 10)}. dose = min(mg_per_kg*weight, max_mg); volume = dose/(conc_mg/conc_ml). Graduations: 1 mL syringe 0.01 mL, 5 mL syringe 0.2 mL, 10 mL syringe 0.5 mL. Round half-up to the graduation, but if that delivers more than max_mg round down instead. A rounded volume above syringe capacity returns {status SPLIT, syringes = ceil(volume/capacity)}; otherwise {status OK, volume_ml, dose_mg delivered}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    dose = min(Fraction(str(x['mg_per_kg'])) * Fraction(str(x['weight_kg'])), x['max_mg'])
    per_ml = Fraction(x['conc_mg'], x['conc_ml'])
    vol = dose / per_ml
    grad = {1: Fraction(1, 100), 5: Fraction(1, 5), 10: Fraction(1, 2)}[x['syringe_ml']]
    v = math.floor(vol / grad + Fraction(1, 2)) * grad
    if v * per_ml > x['max_mg']:
        v = math.floor(vol / grad) * grad
    if v > x['syringe_ml']:
        return {'status': 'SPLIT', 'syringes': int(v // x['syringe_ml'])}
    return {'status': 'OK', 'volume_ml': float(v), 'dose_mg': float(v * per_ml)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('suspension per 5 mL label',
   {'weight_kg': 13, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 9, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 3.8, 'dose_mg': 91.2}),
  ('10 mL syringe graduations',
   {'weight_kg': 21, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 195, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 180.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 39, 'mg_per_kg': 5, 'max_mg': 200, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.1', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 5, 'max_mg': 1000, 'conc_mg': 10, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 31, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 14, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 10, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.2, 'dose_mg': 100.8}),
  ('10 mL syringe graduations',
   {'weight_kg': 22, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 215, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 43, 'mg_per_kg': 5, 'max_mg': 220, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.2', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 10, 'max_mg': 1000, 'conc_mg': 20, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 32, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 15, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 225.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 11, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.6, 'dose_mg': 110.4}),
  ('10 mL syringe graduations',
   {'weight_kg': 23, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.0, 'dose_mg': 160.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 235, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 47, 'mg_per_kg': 5, 'max_mg': 240, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.05', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 30, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 34, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 16, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 12, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('10 mL syringe graduations',
   {'weight_kg': 24, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.5, 'dose_mg': 170.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 255, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 51, 'mg_per_kg': 5, 'max_mg': 260, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.15', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 20, 'max_mg': 1000, 'conc_mg': 40, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 36, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 17, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 13, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('10 mL syringe graduations',
   {'weight_kg': 25, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 9.0, 'dose_mg': 180.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 275, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 55, 'mg_per_kg': 5, 'max_mg': 280, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.0, 'dose_mg': 280.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.12', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 25, 'max_mg': 1000, 'conc_mg': 50, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 37, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    dose = min(Fraction(str(x['mg_per_kg'])) * Fraction(str(x['weight_kg'])), x['max_mg'])
    per_ml = Fraction(x['conc_mg'], x['conc_ml'])
    vol = dose / per_ml
    grad = {1: Fraction(1, 100), 5: Fraction(1, 5), 10: Fraction(1, 2)}[x['syringe_ml']]
    v = math.floor(vol / grad + Fraction(1, 2)) * grad
    if v * per_ml > x['max_mg']:
        v = math.floor(vol / grad) * grad
    if v > x['syringe_ml']:
        return {'status': 'SPLIT', 'syringes': int(v // x['syringe_ml']) + 1}
    return {'status': 'OK', 'volume_ml': float(v), 'dose_mg': float(v * per_ml)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('suspension per 5 mL label',
   {'weight_kg': 13, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 9, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 3.8, 'dose_mg': 91.2}),
  ('10 mL syringe graduations',
   {'weight_kg': 21, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 195, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 180.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 39, 'mg_per_kg': 5, 'max_mg': 200, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.1', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 5, 'max_mg': 1000, 'conc_mg': 10, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 31, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 14, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.0, 'dose_mg': 200.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 10, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.2, 'dose_mg': 100.8}),
  ('10 mL syringe graduations',
   {'weight_kg': 22, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.5, 'dose_mg': 150.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 215, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 200.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 43, 'mg_per_kg': 5, 'max_mg': 220, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.2', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 10, 'max_mg': 1000, 'conc_mg': 20, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 32, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2})],
 [('suspension per 5 mL label',
   {'weight_kg': 15, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 4.5, 'dose_mg': 225.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 11, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 4.6, 'dose_mg': 110.4}),
  ('10 mL syringe graduations',
   {'weight_kg': 23, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.0, 'dose_mg': 160.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 235, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.5, 'dose_mg': 220.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 47, 'mg_per_kg': 5, 'max_mg': 240, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.05', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 30, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 34, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 16, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 12, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('10 mL syringe graduations',
   {'weight_kg': 24, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 8.5, 'dose_mg': 170.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 255, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.0, 'dose_mg': 240.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 51, 'mg_per_kg': 5, 'max_mg': 260, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.15', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 20, 'max_mg': 1000, 'conc_mg': 40, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 36, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})],
 [('suspension per 5 mL label',
   {'weight_kg': 17, 'mg_per_kg': 15, 'max_mg': 500, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 250.0}),
  ('5 mL syringe graduations',
   {'weight_kg': 13, 'mg_per_kg': 10, 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('10 mL syringe graduations',
   {'weight_kg': 25, 'mg_per_kg': 7, 'max_mg': 500, 'conc_mg': 100, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 9.0, 'dose_mg': 180.0}),
  ('rounding up would exceed maximum',
   {'weight_kg': 40, 'mg_per_kg': 20, 'max_mg': 275, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 6.5, 'dose_mg': 260.0}),
  ('rounding up lands exactly on maximum',
   {'weight_kg': 55, 'mg_per_kg': 5, 'max_mg': 280, 'conc_mg': 200, 'conc_ml': 5, 'syringe_ml': 10},
   {'status': 'OK', 'volume_ml': 7.0, 'dose_mg': 280.0}),
  ('volume rounds down to capacity',
   {'weight_kg': 10, 'mg_per_kg': '12.12', 'max_mg': 500, 'conc_mg': 120, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'OK', 'volume_ml': 5.0, 'dose_mg': 120.0}),
  ('exact multiple of syringe capacity',
   {'weight_kg': 20, 'mg_per_kg': 25, 'max_mg': 1000, 'conc_mg': 50, 'conc_ml': 1, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 2}),
  ('split across syringes',
   {'weight_kg': 37, 'mg_per_kg': 15, 'max_mg': 1000, 'conc_mg': 250, 'conc_ml': 5, 'syringe_ml': 5},
   {'status': 'SPLIT', 'syringes': 3})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A 9 mL dose in 5 mL syringes reports a single syringe.

Input {weight_kg, mg_per_kg, max_mg, conc_mg, conc_ml (label strength conc_mg per conc_ml), syringe_ml (1, 5 or 10)}. dose = min(mg_per_kg*weight, max_mg); volume = dose/(conc_mg/conc_ml). Graduations: 1 mL syringe 0.01 mL, 5 mL syringe 0.2 mL, 10 mL syringe 0.5 mL. Round half-up to the graduation, but if that delivers more than max_mg round down instead. A rounded volume above syringe capacity returns {status SPLIT, syringes = ceil(volume/capacity)}; otherwise {status OK, volume_ml, dose_mg delivered}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x['age_days'] <= 28 and x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg * 10 < Fraction(str(lo)) * 9:
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x['age_days'] < 28 * 7 and x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg * 10 < Fraction(str(lo)) * 9:
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A 28-day-old infant is screened against the neonatal range.

Input {dose_mg, weight_kg (or null), age_days, range [lo, hi] mg/kg, optional neonatal_range, abs_max_mg}. Missing weight returns NO_WEIGHT. A dose above abs_max_mg returns OVER_ABSOLUTE. Neonates (age_days < 28) use neonatal_range when present. Toy 10% tolerance: UNDER when mg/kg < 0.9*lo, OVER when mg/kg > 1.1*hi, else OK. Return {flag, mg_per_kg (2 dp)}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg * 10 < Fraction(str(lo)) * 9:
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x['age_days'] < 365 and x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg * 10 < Fraction(str(lo)) * 9:
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A 3-month-old is screened against the neonatal range whenever the drug has one.

Input {dose_mg, weight_kg (or null), age_days, range [lo, hi] mg/kg, optional neonatal_range, abs_max_mg}. Missing weight returns NO_WEIGHT. A dose above abs_max_mg returns OVER_ABSOLUTE. Neonates (age_days < 28) use neonatal_range when present. Toy 10% tolerance: UNDER when mg/kg < 0.9*lo, OVER when mg/kg > 1.1*hi, else OK. Return {flag, mg_per_kg (2 dp)}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x['age_days'] < 28 and x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg * 10 < Fraction(str(lo)) * 11:
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    if x['weight_kg'] is None:
        return {'flag': 'NO_WEIGHT'}
    dose = Fraction(str(x['dose_mg']))
    if dose > x['abs_max_mg']:
        return {'flag': 'OVER_ABSOLUTE'}
    lo, hi = x['range']
    if x['age_days'] < 28 and x.get('neonatal_range'):
        lo, hi = x['neonatal_range']
    per_kg = dose / Fraction(str(x['weight_kg']))
    if per_kg < Fraction(str(lo)):
        flag = 'UNDER'
    elif per_kg * 10 > Fraction(str(hi)) * 11:
        flag = 'OVER'
    else:
        flag = 'OK'
    return {'flag': flag, 'mg_per_kg': round(float(per_kg), 2)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('within range control',
   {'dose_mg': 310, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.5}),
  ('neonate on day 27',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 13,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 91,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 91, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.1}),
  ('clear underdose',
   {'dose_mg': 79, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.9}),
  ('overdose tolerance edge',
   {'dose_mg': 242, 'weight_kg': 11, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 65, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 15.38}),
  ('absolute maximum',
   {'dose_mg': 1060, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 100, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 320, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.0}),
  ('neonate on day 27',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 4.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 14,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 4.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 92,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 92, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.2}),
  ('clear underdose',
   {'dose_mg': 78, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.8}),
  ('overdose tolerance edge',
   {'dose_mg': 264, 'weight_kg': 12, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 70, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 14.29}),
  ('absolute maximum',
   {'dose_mg': 1070, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 200, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 330, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 16.5}),
  ('neonate on day 27',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.0}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 15,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.0}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 93,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 93, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.3}),
  ('clear underdose',
   {'dose_mg': 77, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.7}),
  ('overdose tolerance edge',
   {'dose_mg': 286, 'weight_kg': 13, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 75, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 13.33}),
  ('absolute maximum',
   {'dose_mg': 1080, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 300, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 340, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.0}),
  ('neonate on day 27',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 5.33}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 16,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.33}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 94,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 94, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.4}),
  ('clear underdose',
   {'dose_mg': 76, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.6}),
  ('overdose tolerance edge',
   {'dose_mg': 308, 'weight_kg': 14, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 80, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 12.5}),
  ('absolute maximum',
   {'dose_mg': 1090, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 400, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})],
 [('within range control',
   {'dose_mg': 350, 'weight_kg': 20, 'age_days': 2000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 17.5}),
  ('neonate on day 27',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 27,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'OVER', 'mg_per_kg': 5.67}),
  ('infant on day 28 uses standard range',
   {'dose_mg': 17,
    'weight_kg': 3,
    'age_days': 28,
    'range': [10, 20],
    'neonatal_range': [3, 5],
    'abs_max_mg': 100},
   {'flag': 'UNDER', 'mg_per_kg': 5.67}),
  ('older infant ignores neonatal range',
   {'dose_mg': 40,
    'weight_kg': 4,
    'age_days': 95,
    'range': [8, 12],
    'neonatal_range': [2, 4],
    'abs_max_mg': 100},
   {'flag': 'OK', 'mg_per_kg': 10.0}),
  ('underdose within tolerance',
   {'dose_mg': 95, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 9.5}),
  ('clear underdose',
   {'dose_mg': 75, 'weight_kg': 10, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'UNDER', 'mg_per_kg': 7.5}),
  ('overdose tolerance edge',
   {'dose_mg': 330, 'weight_kg': 15, 'age_days': 1000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 22.0}),
  ('dose exactly at absolute maximum',
   {'dose_mg': 1000, 'weight_kg': 85, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OK', 'mg_per_kg': 11.76}),
  ('absolute maximum',
   {'dose_mg': 1100, 'weight_kg': 60, 'age_days': 9000, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'OVER_ABSOLUTE'}),
  ('missing weight',
   {'dose_mg': 100, 'weight_kg': None, 'age_days': 500, 'range': [10, 20], 'abs_max_mg': 1000},
   {'flag': 'NO_WEIGHT'})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Doses slightly below the range are flagged as underdoses despite the tolerance.

Input {dose_mg, weight_kg (or null), age_days, range [lo, hi] mg/kg, optional neonatal_range, abs_max_mg}. Missing weight returns NO_WEIGHT. A dose above abs_max_mg returns OVER_ABSOLUTE. Neonates (age_days < 28) use neonatal_range when present. Toy 10% tolerance: UNDER when mg/kg < 0.9*lo, OVER when mg/kg > 1.1*hi, else OK. Return {flag, mg_per_kg (2 dp)}.

split: open-access