broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    s,e=x['study']; a=x['age']; tot=0; deaths=0
    for b,en,ex,died in x['records']:
        bs=b+365*a; be=bs+365
        lo=max(s,en,bs); hi=min(e,ex,be)
        counted=died and lo<=ex<min(e,be)
        if counted: deaths+=1
        if counted: hi=be
        tot+=max(0,hi-lo)
    return [tot,deaths]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1646, 20187, 20406, False], [-1852, 20095, 20577, False], [-1617, 20154, 20514, False], [-1795, 19880, 20864, False], [-2031, 19846, 20364, True]], 'method': 'exact'}, [1300, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1795, 20149, 20843, False]], 'method': 'exact'}, [686, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1640, 19886, 20758, False], [-2010, 20199, 20626, False], [-2086, 19856, 20816, False], [-2011, 20172, 20892, False], [-1704, 19877, 20272, True]], 'method': 'actuarial'}, [1047, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1976, 19813, 20729, False]], 'method': 'actuarial'}, [654, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1717, 19878, 20681, False], [-1678, 20196, 20770, False], [-2186, 20016, 20415, False]], 'method': 'exact'}, [793, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2064, 19900, 20737, False], [-2068, 19900, 20448, False], [-2155, 19900, 20867, False]], 'method': 'actuarial'}, [508, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2070, 19840, 20456, False], [-1999, 20154, 20680, False], [-2193, 19855, 20494, False], [-2156, 19996, 20689, False], [-1701, 20077, 20310, True]], 'method': 'exact'}, [599, 1]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1711, 19808, 20863, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1665, 19831, 20488, False], [-1673, 19955, 20442, False], [-1850, 20050, 20697, False], [-2078, 19804, 20804, False], [-1601, 20194, 20313, True]], 'method': 'actuarial'}, [1385, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1605, 19948, 20570, False]], 'method': 'actuarial'}, [640, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1889, 19936, 20521, False], [-1640, 20120, 20507, False], [-1778, 20160, 20852, False]], 'method': 'exact'}, [939, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1890, 19900, 20544, False], [-1719, 19900, 20802, False], [-2117, 19900, 20531, False]], 'method': 'actuarial'}, [878, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1741, 20077, 20701, False], [-1769, 20197, 20455, False], [-2071, 19974, 20741, False], [-1918, 19823, 20436, False], [-2098, 20169, 20358, True]], 'method': 'exact'}, [1164, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1954, 20112, 20568, False]], 'method': 'exact'}, [564, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1887, 19974, 20561, False], [-2071, 20133, 20760, False], [-1820, 20134, 20483, False], [-1975, 20119, 20850, False], [-1795, 20071, 20207, True]], 'method': 'actuarial'}, [1273, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-2147, 19920, 20609, False]], 'method': 'actuarial'}, [483, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1894, 20110, 20820, False], [-1682, 19889, 20808, False], [-2155, 19896, 20593, False]], 'method': 'exact'}, [736, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2018, 19900, 20548, False], [-1644, 19900, 20877, False], [-2200, 19900, 20475, False]], 'method': 'actuarial'}, [677, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1863, 20066, 20531, False], [-2075, 19963, 20495, False], [-1993, 19890, 20763, False], [-1827, 19962, 20674, False], [-2180, 20079, 20226, True]], 'method': 'exact'}, [1169, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1679, 20195, 20683, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2025, 19978, 20453, False], [-2178, 19958, 20450, False], [-2000, 19923, 20448, False], [-2144, 19842, 20894, False], [-2062, 19838, 20339, True]], 'method': 'actuarial'}, [916, 0]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1658, 20133, 20633, False]], 'method': 'actuarial'}, [730, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2119, 19951, 20507, False], [-1690, 20001, 20675, False], [-1814, 19802, 20650, False]], 'method': 'exact'}, [876, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1893, 19900, 20703, False], [-2022, 19900, 20661, False], [-1898, 19900, 20856, False]], 'method': 'actuarial'}, [973, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1825, 20155, 20608, False], [-1632, 20145, 20432, False], [-1827, 20020, 20829, False], [-2080, 19871, 20894, False], [-2071, 20138, 20394, True]], 'method': 'exact'}, [1055, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1652, 20173, 20716, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2038, 19972, 20521, False], [-1836, 19923, 20435, False], [-2081, 20049, 20457, False], [-1816, 19815, 20798, False], [-1964, 19855, 20159, True]], 'method': 'actuarial'}, [1393, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1744, 19944, 20873, False]], 'method': 'actuarial'}, [730, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1879, 19856, 20519, False], [-1807, 19835, 20774, False], [-2006, 19972, 20562, False]], 'method': 'exact'}, [989, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1878, 19900, 20430, False], [-2072, 19900, 20760, False], [-1895, 19900, 20822, False]], 'method': 'actuarial'}, [923, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]]]
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):
    s,e=x['study']; a=x['age']; tot=0; deaths=0
    for b,en,ex,died in x['records']:
        bs=b+365*a; be=bs+365
        lo=max(s,en,bs); hi=min(e,ex,be)
        counted=died and lo<=ex<min(e,be)
        if counted: deaths+=1
        if x['method']=='actuarial': hi=be
        tot+=max(0,hi-lo)
    return [tot,deaths]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1646, 20187, 20406, False], [-1852, 20095, 20577, False], [-1617, 20154, 20514, False], [-1795, 19880, 20864, False], [-2031, 19846, 20364, True]], 'method': 'exact'}, [1300, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1795, 20149, 20843, False]], 'method': 'exact'}, [686, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1640, 19886, 20758, False], [-2010, 20199, 20626, False], [-2086, 19856, 20816, False], [-2011, 20172, 20892, False], [-1704, 19877, 20272, True]], 'method': 'actuarial'}, [1047, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1976, 19813, 20729, False]], 'method': 'actuarial'}, [654, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1717, 19878, 20681, False], [-1678, 20196, 20770, False], [-2186, 20016, 20415, False]], 'method': 'exact'}, [793, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2064, 19900, 20737, False], [-2068, 19900, 20448, False], [-2155, 19900, 20867, False]], 'method': 'actuarial'}, [508, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2070, 19840, 20456, False], [-1999, 20154, 20680, False], [-2193, 19855, 20494, False], [-2156, 19996, 20689, False], [-1701, 20077, 20310, True]], 'method': 'exact'}, [599, 1]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1711, 19808, 20863, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1665, 19831, 20488, False], [-1673, 19955, 20442, False], [-1850, 20050, 20697, False], [-2078, 19804, 20804, False], [-1601, 20194, 20313, True]], 'method': 'actuarial'}, [1385, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1605, 19948, 20570, False]], 'method': 'actuarial'}, [640, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1889, 19936, 20521, False], [-1640, 20120, 20507, False], [-1778, 20160, 20852, False]], 'method': 'exact'}, [939, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1890, 19900, 20544, False], [-1719, 19900, 20802, False], [-2117, 19900, 20531, False]], 'method': 'actuarial'}, [878, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1741, 20077, 20701, False], [-1769, 20197, 20455, False], [-2071, 19974, 20741, False], [-1918, 19823, 20436, False], [-2098, 20169, 20358, True]], 'method': 'exact'}, [1164, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1954, 20112, 20568, False]], 'method': 'exact'}, [564, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1887, 19974, 20561, False], [-2071, 20133, 20760, False], [-1820, 20134, 20483, False], [-1975, 20119, 20850, False], [-1795, 20071, 20207, True]], 'method': 'actuarial'}, [1273, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-2147, 19920, 20609, False]], 'method': 'actuarial'}, [483, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1894, 20110, 20820, False], [-1682, 19889, 20808, False], [-2155, 19896, 20593, False]], 'method': 'exact'}, [736, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2018, 19900, 20548, False], [-1644, 19900, 20877, False], [-2200, 19900, 20475, False]], 'method': 'actuarial'}, [677, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1863, 20066, 20531, False], [-2075, 19963, 20495, False], [-1993, 19890, 20763, False], [-1827, 19962, 20674, False], [-2180, 20079, 20226, True]], 'method': 'exact'}, [1169, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1679, 20195, 20683, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2025, 19978, 20453, False], [-2178, 19958, 20450, False], [-2000, 19923, 20448, False], [-2144, 19842, 20894, False], [-2062, 19838, 20339, True]], 'method': 'actuarial'}, [916, 0]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1658, 20133, 20633, False]], 'method': 'actuarial'}, [730, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-2119, 19951, 20507, False], [-1690, 20001, 20675, False], [-1814, 19802, 20650, False]], 'method': 'exact'}, [876, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1893, 19900, 20703, False], [-2022, 19900, 20661, False], [-1898, 19900, 20856, False]], 'method': 'actuarial'}, [973, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]], [['exact mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-1825, 20155, 20608, False], [-1632, 20145, 20432, False], [-1827, 20020, 20829, False], [-2080, 19871, 20894, False], [-2071, 20138, 20394, True]], 'method': 'exact'}, [1055, 0]], ['exact death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1652, 20173, 20716, False]], 'method': 'exact'}, [730, 0]], ['exact late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'exact'}, [50, 1]], ['exact death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'exact'}, [230, 0]], ['exact death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'exact'}, [0, 0]], ['actuarial mixed cohort', {'study': [20000, 20730], 'age': 60, 'records': [[-2038, 19972, 20521, False], [-1836, 19923, 20435, False], [-2081, 20049, 20457, False], [-1816, 19815, 20798, False], [-1964, 19855, 20159, True]], 'method': 'actuarial'}, [1393, 1]], ['actuarial death on next birthday', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20000, 20465, True], [-1744, 19944, 20873, False]], 'method': 'actuarial'}, [730, 0]], ['actuarial late entrant death', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20250, 20300, True]], 'method': 'actuarial'}, [215, 1]], ['actuarial death after study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1400, 20100, 20750, True]], 'method': 'actuarial'}, [230, 0]], ['actuarial death before age band', {'study': [20000, 20730], 'age': 60, 'records': [[-1600, 20000, 20200, True]], 'method': 'actuarial'}, [0, 0]], ['exact survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1879, 19856, 20519, False], [-1807, 19835, 20774, False], [-2006, 19972, 20562, False]], 'method': 'exact'}, [989, 0]], ['actuarial survivors only', {'study': [20000, 20730], 'age': 60, 'records': [[-1878, 19900, 20430, False], [-2072, 19900, 20760, False], [-1895, 19900, 20822, False]], 'method': 'actuarial'}, [923, 0]], ['death on first observed day', {'study': [20000, 20730], 'age': 60, 'records': [[-1800, 20150, 20150, True]], 'method': 'exact'}, [0, 1]], ['death near study end', {'study': [20000, 20730], 'age': 60, 'records': [[-1270, 20000, 20727, True]], 'method': 'actuarial'}, [365, 1]]]]
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:
Exact-method exposure is extended for deaths.

Input study [s, e) days, age a, method exact|actuarial, records [[birth, entry, exit, died]] with 365-day years. Age band [birth+365a, +365). Observation [max(s,entry,band start), min(e,exit,band end)); a death at exit counts if lo<=exit<min(e, band end). Actuarial exposure extends a counted death to the band end. Return [total exposure days, deaths].

split: open-access


meaning: Fixture-count band, not measured model difficulty

deps: stdlib

prompt:
Survival probabilities are far too high.

Input A, B, c, age, t (floats). B<0, c<1 or A<=-B -> 'invalid parameters'. Cumulative hazard H = A t + B c^x (c^t-1)/ln c (c=1: (A+B) t). Return [exp(-H), mu at x+t = A + B c^(x+t)] rounded to 8.

split: open-access


prompt:
Survival from age x is computed as if from age x+t.

Input A, B, c, age, t (floats). B<0, c<1 or A<=-B -> 'invalid parameters'. Cumulative hazard H = A t + B c^x (c^t-1)/ln c (c=1: (A+B) t). Return [exp(-H), mu at x+t = A + B c^(x+t)] rounded to 8.

split: open-access


prompt:
Accident-hazard contribution does not grow with duration.

Input A, B, c, age, t (floats). B<0, c<1 or A<=-B -> 'invalid parameters'. Cumulative hazard H = A t + B c^x (c^t-1)/ln c (c=1: (A+B) t). Return [exp(-H), mu at x+t = A + B c^(x+t)] rounded to 8.

split: open-access


prompt:
The reported hazard lags by the projection duration.

Input A, B, c, age, t (floats). B<0, c<1 or A<=-B -> 'invalid parameters'. Cumulative hazard H = A t + B c^x (c^t-1)/ln c (c=1: (A+B) t). Return [exp(-H), mu at x+t = A + B c^(x+t)] rounded to 8.

split: open-access


prompt:
Constant-hazard parameterisations understate mortality.

Input A, B, c, age, t (floats). B<0, c<1 or A<=-B -> 'invalid parameters'. Cumulative hazard H = A t + B c^x (c^t-1)/ln c (c=1: (A+B) t). Return [exp(-H), mu at x+t = A + B c^(x+t)] rounded to 8.

split: open-access


prompt:
Single-year rates do not compound back to the group rate.

Input groups [[start age, width n, nqx per 100000]]; groups must be contiguous from the first start (else 'gap at <start>'). An nqx of 100000 is the open group and ends the table. Each closed group yields n equal single-year rates 1-(1-nqx)^(1/n) rounded to 8.

split: open-access


prompt:
Infant and 1-4 groups trigger a false gap.

Input groups [[start age, width n, nqx per 100000]]; groups must be contiguous from the first start (else 'gap at <start>'). An nqx of 100000 is the open group and ends the table. Each closed group yields n equal single-year rates 1-(1-nqx)^(1/n) rounded to 8.

split: open-access


prompt:
Output includes certain-death rates for every open-group year.

Input groups [[start age, width n, nqx per 100000]]; groups must be contiguous from the first start (else 'gap at <start>'). An nqx of 100000 is the open group and ends the table. Each closed group yields n equal single-year rates 1-(1-nqx)^(1/n) rounded to 8.

split: open-access


prompt:
Adult-only tables are rejected with a gap.

Input groups [[start age, width n, nqx per 100000]]; groups must be contiguous from the first start (else 'gap at <start>'). An nqx of 100000 is the open group and ends the table. Each closed group yields n equal single-year rates 1-(1-nqx)^(1/n) rounded to 8.

split: open-access


prompt:
Overlapping groups duplicate ages in the output.

Input groups [[start age, width n, nqx per 100000]]; groups must be contiguous from the first start (else 'gap at <start>'). An nqx of 100000 is the open group and ends the table. Each closed group yields n equal single-year rates 1-(1-nqx)^(1/n) rounded to 8.

split: open-access


prompt:
Groups with early deaths get the wrong nqx.

Input groups [[width n (None = open), deaths, population (<=0 -> 'invalid population'), nax as [num, den] years]]. m = deaths/pop; closed nqx = min(1, n m/(1+(n-a) m)), nLx = n l_{x+n} + a d; open q=1, L = l/m (m=0 -> 'open interval without deaths'). Radix 100000. Return [q list rounded 6, e0 = sum L / radix rounded 4].

split: open-access


prompt:
Life expectancy is overstated for infant groups.

Input groups [[width n (None = open), deaths, population (<=0 -> 'invalid population'), nax as [num, den] years]]. m = deaths/pop; closed nqx = min(1, n m/(1+(n-a) m)), nLx = n l_{x+n} + a d; open q=1, L = l/m (m=0 -> 'open interval without deaths'). Radix 100000. Return [q list rounded 6, e0 = sum L / radix rounded 4].

split: open-access


prompt:
Very high mortality groups produce negative survivors.

Input groups [[width n (None = open), deaths, population (<=0 -> 'invalid population'), nax as [num, den] years]]. m = deaths/pop; closed nqx = min(1, n m/(1+(n-a) m)), nLx = n l_{x+n} + a d; open q=1, L = l/m (m=0 -> 'open interval without deaths'). Radix 100000. Return [q list rounded 6, e0 = sum L / radix rounded 4].

split: open-access


prompt:
e0 is understated by the open-interval person-years.

Input groups [[width n (None = open), deaths, population (<=0 -> 'invalid population'), nax as [num, den] years]]. m = deaths/pop; closed nqx = min(1, n m/(1+(n-a) m)), nLx = n l_{x+n} + a d; open q=1, L = l/m (m=0 -> 'open interval without deaths'). Radix 100000. Return [q list rounded 6, e0 = sum L / radix rounded 4].

split: open-access


prompt:
Separation factors are scaled by the interval width.

Input groups [[width n (None = open), deaths, population (<=0 -> 'invalid population'), nax as [num, den] years]]. m = deaths/pop; closed nqx = min(1, n m/(1+(n-a) m)), nLx = n l_{x+n} + a d; open q=1, L = l/m (m=0 -> 'open interval without deaths'). Radix 100000. Return [q list rounded 6, e0 = sum L / radix rounded 4].

split: open-access


prompt:
Dependent death rates are too low.

Input qd, qw (associated single-decrement rates per 100000, equal lengths else 'length mismatch'), timing udd|eoy, radix. UDD in each decrement: q(d)=q'd(1-q'w/2), q(w)=q'w(1-q'd/2). eoy (lapses at year end): q(d)=q'd, q(w)=(1-q'd)q'w. Each year counts are rounded half-up and subtracted from l. Return [[deaths, lapses] per year, final l].

split: open-access


prompt:
Dependent lapse counts are overstated.

Input qd, qw (associated single-decrement rates per 100000, equal lengths else 'length mismatch'), timing udd|eoy, radix. UDD in each decrement: q(d)=q'd(1-q'w/2), q(w)=q'w(1-q'd/2). eoy (lapses at year end): q(d)=q'd, q(w)=(1-q'd)q'w. Each year counts are rounded half-up and subtracted from l. Return [[deaths, lapses] per year, final l].

split: open-access


prompt:
Lapses are counted on lives who died during the year.

Input qd, qw (associated single-decrement rates per 100000, equal lengths else 'length mismatch'), timing udd|eoy, radix. UDD in each decrement: q(d)=q'd(1-q'w/2), q(w)=q'w(1-q'd/2). eoy (lapses at year end): q(d)=q'd, q(w)=(1-q'd)q'w. Each year counts are rounded half-up and subtracted from l. Return [[deaths, lapses] per year, final l].

split: open-access


prompt:
The in-force count only declines by deaths.

Input qd, qw (associated single-decrement rates per 100000, equal lengths else 'length mismatch'), timing udd|eoy, radix. UDD in each decrement: q(d)=q'd(1-q'w/2), q(w)=q'w(1-q'd/2). eoy (lapses at year end): q(d)=q'd, q(w)=(1-q'd)q'w. Each year counts are rounded half-up and subtracted from l. Return [[deaths, lapses] per year, final l].

split: open-access


prompt:
Missing lapse rates silently shorten the projection.

Input qd, qw (associated single-decrement rates per 100000, equal lengths else 'length mismatch'), timing udd|eoy, radix. UDD in each decrement: q(d)=q'd(1-q'w/2), q(w)=q'w(1-q'd/2). eoy (lapses at year end): q(d)=q'd, q(w)=(1-q'd)q'w. Each year counts are rounded half-up and subtracted from l. Return [[deaths, lapses] per year, final l].

split: open-access


prompt:
Reserves are too low because the start-of-year premium is not accumulated.

Input q (per 100000 by policy year), P (annual net premium, cents, paid at start of year), S (death benefit at year end), i (per mille). Starting from 0V=0: (k+1)V = ((kV+P)(1+i) - q S)/p carried exactly; each reserve is reported rounded half away from zero. If p=0 report None and stop.

split: open-access


prompt:
Reserves per survivor are understated.

Input q (per 100000 by policy year), P (annual net premium, cents, paid at start of year), S (death benefit at year end), i (per mille). Starting from 0V=0: (k+1)V = ((kV+P)(1+i) - q S)/p carried exactly; each reserve is reported rounded half away from zero. If p=0 report None and stop.

split: open-access


prompt:
Later reserves drift from the exact recursion.

Input q (per 100000 by policy year), P (annual net premium, cents, paid at start of year), S (death benefit at year end), i (per mille). Starting from 0V=0: (k+1)V = ((kV+P)(1+i) - q S)/p carried exactly; each reserve is reported rounded half away from zero. If p=0 report None and stop.

split: open-access


prompt:
An undefined terminal reserve is reported as zero.

Input q (per 100000 by policy year), P (annual net premium, cents, paid at start of year), S (death benefit at year end), i (per mille). Starting from 0V=0: (k+1)V = ((kV+P)(1+i) - q S)/p carried exactly; each reserve is reported rounded half away from zero. If p=0 report None and stop.

split: open-access


prompt:
Deficient-premium years show zero reserves and later reserves drift.

Input q (per 100000 by policy year), P (annual net premium, cents, paid at start of year), S (death benefit at year end), i (per mille). Starting from 0V=0: (k+1)V = ((kV+P)(1+i) - q S)/p carried exactly; each reserve is reported rounded half away from zero. If p=0 report None and stop.

split: open-access


prompt:
The fund per survivor omits the benefit of survivorship.

Input l (from issue age), n (0<=n<len(l) else 'invalid term'), i, g (premium escalation per mille), P. l_n=0 -> 'no survivors'. nEx = (l_n/l_0) v^n. Fund per survivor at n = sum_{k<n} P(1+g)^k (1+i)^(n-k) l_k/l_n (premiums at the start of each year). Return [nEx rounded 8, fund rounded half-up].

split: open-access


prompt:
Premiums paid at the start of the year lose a year of interest.

Input l (from issue age), n (0<=n<len(l) else 'invalid term'), i, g (premium escalation per mille), P. l_n=0 -> 'no survivors'. nEx = (l_n/l_0) v^n. Fund per survivor at n = sum_{k<n} P(1+g)^k (1+i)^(n-k) l_k/l_n (premiums at the start of each year). Return [nEx rounded 8, fund rounded half-up].

split: open-access


prompt:
nEx equals the survival probability.

Input l (from issue age), n (0<=n<len(l) else 'invalid term'), i, g (premium escalation per mille), P. l_n=0 -> 'no survivors'. nEx = (l_n/l_0) v^n. Fund per survivor at n = sum_{k<n} P(1+g)^k (1+i)^(n-k) l_k/l_n (premiums at the start of each year). Return [nEx rounded 8, fund rounded half-up].

split: open-access


prompt:
The fund includes a premium due on the maturity date.

Input l (from issue age), n (0<=n<len(l) else 'invalid term'), i, g (premium escalation per mille), P. l_n=0 -> 'no survivors'. nEx = (l_n/l_0) v^n. Fund per survivor at n = sum_{k<n} P(1+g)^k (1+i)^(n-k) l_k/l_n (premiums at the start of each year). Return [nEx rounded 8, fund rounded half-up].

split: open-access


prompt:
Every premium is one escalation step too high.

Input l (from issue age), n (0<=n<len(l) else 'invalid term'), i, g (premium escalation per mille), P. l_n=0 -> 'no survivors'. nEx = (l_n/l_0) v^n. Fund per survivor at n = sum_{k<n} P(1+g)^k (1+i)^(n-k) l_k/l_n (premiums at the start of each year). Return [nEx rounded 8, fund rounded half-up].

split: open-access


prompt:
Cohort rates show no calendar-year effect.

Input table {year: [q per 100000 by age from 0]}, cohort birth year, ages a0..a1. Cohort rate at age a is table[cohort+a][a]; years after the last tabulated year use the last year; years before the first -> 'missing year <y>'; missing ages -> 'missing age <a>'. Return [rates, survival over the ages rounded 8].

split: open-access


prompt:
Recent cohorts cannot be projected.

Input table {year: [q per 100000 by age from 0]}, cohort birth year, ages a0..a1. Cohort rate at age a is table[cohort+a][a]; years after the last tabulated year use the last year; years before the first -> 'missing year <y>'; missing ages -> 'missing age <a>'. Return [rates, survival over the ages rounded 8].

split: open-access


prompt:
Old cohorts are given rates from later years.

Input table {year: [q per 100000 by age from 0]}, cohort birth year, ages a0..a1. Cohort rate at age a is table[cohort+a][a]; years after the last tabulated year use the last year; years before the first -> 'missing year <y>'; missing ages -> 'missing age <a>'. Return [rates, survival over the ages rounded 8].

split: open-access


prompt:
Cohort survival stops one age short.

Input table {year: [q per 100000 by age from 0]}, cohort birth year, ages a0..a1. Cohort rate at age a is table[cohort+a][a]; years after the last tabulated year use the last year; years before the first -> 'missing year <y>'; missing ages -> 'missing age <a>'. Return [rates, survival over the ages rounded 8].

split: open-access


prompt:
Cohort rates come from childhood ages.

Input table {year: [q per 100000 by age from 0]}, cohort birth year, ages a0..a1. Cohort rate at age a is table[cohort+a][a]; years after the last tabulated year use the last year; years before the first -> 'missing year <y>'; missing ages -> 'missing age <a>'. Return [rates, survival over the ages rounded 8].

split: open-access


prompt:
Quantiles are mirrored within the crossing year.

Input l (from age x) and p (per mille surviving, 0<p<1000 else 'invalid'; also l_0<=0). Find the first t with l_t <= l_0 p/1000 and interpolate linearly: t-1 + (l_{t-1}-target)/(l_{t-1}-l_t). No crossing -> 'beyond table'. Rounded to 6.

split: open-access


prompt:
Median lifetimes are a year too long.

Input l (from age x) and p (per mille surviving, 0<p<1000 else 'invalid'; also l_0<=0). Find the first t with l_t <= l_0 p/1000 and interpolate linearly: t-1 + (l_{t-1}-target)/(l_{t-1}-l_t). No crossing -> 'beyond table'. Rounded to 6.

split: open-access


prompt:
Non-median quantiles are reversed.

Input l (from age x) and p (per mille surviving, 0<p<1000 else 'invalid'; also l_0<=0). Find the first t with l_t <= l_0 p/1000 and interpolate linearly: t-1 + (l_{t-1}-target)/(l_{t-1}-l_t). No crossing -> 'beyond table'. Rounded to 6.

split: open-access


prompt:
p=0 or p=1000 returns a time instead of invalid.

Input l (from age x) and p (per mille surviving, 0<p<1000 else 'invalid'; also l_0<=0). Find the first t with l_t <= l_0 p/1000 and interpolate linearly: t-1 + (l_{t-1}-target)/(l_{t-1}-l_t). No crossing -> 'beyond table'. Rounded to 6.

split: open-access


prompt:
Crossings in the final year report beyond table.

Input l (from age x) and p (per mille surviving, 0<p<1000 else 'invalid'; also l_0<=0). Find the first t with l_t <= l_0 p/1000 and interpolate linearly: t-1 + (l_{t-1}-target)/(l_{t-1}-l_t). No crossing -> 'beyond table'. Rounded to 6.

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 + 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(22046, 10000) 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:
A 154 lb entry is dosed as if the patient weighed about 339 kg.

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) * (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)

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) * abs(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 shorter than 60 inches get an ideal weight below the sex base and are pushed into the adjusted band.

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