broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(raw / 5 + Fraction(1, 2)) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt >= Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(raw / 5 + Fraction(1, 2)) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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:
Patients a few kilograms over ideal weight are dosed on an adjusted weight instead of ideal weight.

Input {sex, height_in, weight, unit ("kg" or "lb", 1 kg = 2.2 lb), mg_per_kg, max_mg}. Toy rule: IBW = 50 kg (sex "M") or 45.5 kg (otherwise) + 2.3 kg per inch above 60 in (never below the base). Dosing weight is actual weight when below IBW, IBW when actual is within 100-120% of IBW inclusive, and IBW + 0.4*(actual-IBW) when actual exceeds 120% of IBW. Dose = mg_per_kg * dosing weight rounded half-up to the nearest 5 mg, then capped at max_mg. Return {basis, dosing_weight_kg (1 decimal), dose_mg}.

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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * wt, 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(raw / 5 + Fraction(1, 2)) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = wt - Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(raw / 5 + Fraction(1, 2)) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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:
Obese patients receive a dosing weight larger than their actual weight.

Input {sex, height_in, weight, unit ("kg" or "lb", 1 kg = 2.2 lb), mg_per_kg, max_mg}. Toy rule: IBW = 50 kg (sex "M") or 45.5 kg (otherwise) + 2.3 kg per inch above 60 in (never below the base). Dosing weight is actual weight when below IBW, IBW when actual is within 100-120% of IBW inclusive, and IBW + 0.4*(actual-IBW) when actual exceeds 120% of IBW. Dose = mg_per_kg * dosing weight rounded half-up to the nearest 5 mg, then capped at max_mg. Return {basis, dosing_weight_kg (1 decimal), dose_mg}.

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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(raw / 5) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = round(raw / 5) * 5
    dose = min(dose, x['max_mg'])
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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 that land exactly half-way between 5 mg steps, and many others, are rounded down.

Input {sex, height_in, weight, unit ("kg" or "lb", 1 kg = 2.2 lb), mg_per_kg, max_mg}. Toy rule: IBW = 50 kg (sex "M") or 45.5 kg (otherwise) + 2.3 kg per inch above 60 in (never below the base). Dosing weight is actual weight when below IBW, IBW when actual is within 100-120% of IBW inclusive, and IBW + 0.4*(actual-IBW) when actual exceeds 120% of IBW. Dose = mg_per_kg * dosing weight rounded half-up to the nearest 5 mg, then capped at max_mg. Return {basis, dosing_weight_kg (1 decimal), dose_mg}.

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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(min(raw, x['max_mg']) / 5 + Fraction(1, 2)) * 5
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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):
    w = Fraction(str(x['weight']))
    wt = w / Fraction(11, 5) if x['unit'] == 'lb' else w
    hin = x['height_in']
    base = Fraction(50) if x['sex'] == 'M' else Fraction(91, 2)
    ibw = base + Fraction(23, 10) * max(0, hin - 60)
    if wt < ibw:
        dw, basis = wt, 'actual'
    elif wt > Fraction(6, 5) * ibw:
        dw, basis = ibw + Fraction(2, 5) * (wt - ibw), 'adjusted'
    else:
        dw, basis = ibw, 'ideal'
    raw = Fraction(str(x['mg_per_kg'])) * dw
    dose = math.floor(min(raw, x['max_mg']) / 5) * 5
    return {'basis': basis, 'dosing_weight_kg': round(float(dw), 1), 'dose_mg': dose}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 154, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 70.0, 'dose_mg': 70}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 51, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 60, 'weight': '60', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 105, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 85.8, 'dose_mg': 170}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 59, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 61.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.25', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 15}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1003},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1003})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 176, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 75}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 52, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 65, 'weight': '73.8', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 61.5, 'dose_mg': 125}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 110, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 175}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 60, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 62.0, 'dose_mg': 60}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.45', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 25}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1004},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1004})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 198, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 79.8, 'dose_mg': 80}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 56, 'weight': 53, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 70, 'weight': '87.6', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 73.0, 'dose_mg': 145}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 115, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 89.8, 'dose_mg': 180}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 61, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 63.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.65', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 35}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1008},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1008})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 220, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 83.8, 'dose_mg': 85}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 57, 'weight': 54, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'ideal', 'dosing_weight_kg': 45.5, 'dose_mg': 45}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 75, 'weight': '101.4', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 84.5, 'dose_mg': 170}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 120, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 91.8, 'dose_mg': 185}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 62, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 64, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 64.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '0.85', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 45}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1009},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1009})],
 [('pound entry converted to kilograms',
   {'sex': 'M', 'height_in': 70, 'weight': 242, 'unit': 'lb', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 87.8, 'dose_mg': 90}),
  ('pound entry below ideal weight',
   {'sex': 'F', 'height_in': 64, 'weight': 110, 'unit': 'lb', 'mg_per_kg': 2, 'max_mg': 2000},
   {'basis': 'actual', 'dosing_weight_kg': 50.0, 'dose_mg': 100}),
  ('short stature keeps base ideal weight',
   {'sex': 'F', 'height_in': 58, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 2000},
   {'basis': 'adjusted', 'dosing_weight_kg': 49.3, 'dose_mg': 50}),
  ('exactly 120 percent of ideal stays ideal',
   {'sex': 'M', 'height_in': 80, 'weight': '115.2', 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 96.0, 'dose_mg': 190}),
  ('obese patient uses adjusted weight',
   {'sex': 'M', 'height_in': 70, 'weight': 125, 'unit': 'kg', 'mg_per_kg': 2, 'max_mg': 5000},
   {'basis': 'adjusted', 'dosing_weight_kg': 93.8, 'dose_mg': 190}),
  ('within ideal band control',
   {'sex': 'F', 'height_in': 64, 'weight': 63, 'unit': 'kg', 'mg_per_kg': 3, 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 54.7, 'dose_mg': 165}),
  ('underweight uses actual weight',
   {'sex': 'M', 'height_in': 72, 'weight': 65, 'unit': 'kg', 'mg_per_kg': 1, 'max_mg': 5000},
   {'basis': 'actual', 'dosing_weight_kg': 65.0, 'dose_mg': 65}),
  ('half-way dose rounds up to next 5 mg',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': '1.05', 'max_mg': 5000},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 55}),
  ('ceiling applied after rounding',
   {'sex': 'M', 'height_in': 60, 'weight': 55, 'unit': 'kg', 'mg_per_kg': 22, 'max_mg': 1013},
   {'basis': 'ideal', 'dosing_weight_kg': 50.0, 'dose_mg': 1013})]]
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:
When max_mg is not a multiple of 5 the returned dose exceeds the maximum by a few milligrams.

Input {sex, height_in, weight, unit ("kg" or "lb", 1 kg = 2.2 lb), mg_per_kg, max_mg}. Toy rule: IBW = 50 kg (sex "M") or 45.5 kg (otherwise) + 2.3 kg per inch above 60 in (never below the base). Dosing weight is actual weight when below IBW, IBW when actual is within 100-120% of IBW inclusive, and IBW + 0.4*(actual-IBW) when actual exceeds 120% of IBW. Dose = mg_per_kg * dosing weight rounded half-up to the nearest 5 mg, then capped at max_mg. Return {basis, dosing_weight_kg (1 decimal), dose_mg}.

split: open-access