broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    corr = corr - Fraction(str(x['iob']))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(math.floor(total * 2), 2)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(math.floor(total * 2), 2)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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:
Insulin on board keeps eating into the meal dose after the correction reaches zero.

Input {bg, target, isf, carbs_g, icr, iob, max_units}. Toy rule: bg < 70 returns HOLD with 0 units. correction = (bg-target)/isf; when positive, insulin on board is subtracted from it and the result floored at 0; a negative correction (bg below target) is kept and reduces the meal dose. meal = carbs_g/icr. total = max(0, meal + correction), capped at max_units, then rounded DOWN to 0.5 units. Return {action GIVE|HOLD, units}.

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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'] // x['icr'])
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(math.floor(total * 2), 2)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(round(x['carbs_g'] / x['icr']))
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(math.floor(total * 2), 2)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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 59 g meal with a 1:10 ratio is covered with only 5 units before correction.

Input {bg, target, isf, carbs_g, icr, iob, max_units}. Toy rule: bg < 70 returns HOLD with 0 units. correction = (bg-target)/isf; when positive, insulin on board is subtracted from it and the result floored at 0; a negative correction (bg below target) is kept and reduces the meal dose. meal = carbs_g/icr. total = max(0, meal + correction), capped at max_units, then rounded DOWN to 0.5 units. Return {action GIVE|HOLD, units}.

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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(round(total * 2), 2)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    total = min(total, Fraction(str(x['max_units'])))
    units = Fraction(math.floor(total), 1)
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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 5.9 unit calculation is delivered as 6 units.

Input {bg, target, isf, carbs_g, icr, iob, max_units}. Toy rule: bg < 70 returns HOLD with 0 units. correction = (bg-target)/isf; when positive, insulin on board is subtracted from it and the result floored at 0; a negative correction (bg below target) is kept and reduces the meal dose. meal = carbs_g/icr. total = max(0, meal + correction), capped at max_units, then rounded DOWN to 0.5 units. Return {action GIVE|HOLD, units}.

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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    units = min(Fraction(math.floor(total * 2), 2), Fraction(str(x['max_units'])))
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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):
    bg, tgt = x['bg'], x['target']
    if bg < 70:
        return {'action': 'HOLD', 'units': 0.0}
    corr = Fraction(bg - tgt, x['isf'])
    if corr > 0:
        corr = max(Fraction(0), corr - Fraction(str(x['iob'])))
    meal = Fraction(x['carbs_g'], x['icr'])
    total = max(Fraction(0), meal + corr)
    units = Fraction(math.floor(total * 2), 2)
    units = min(units, Fraction(math.floor(Fraction(str(x['max_units']))), 1))
    return {'action': 'GIVE', 'units': float(units)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('low glucose with meal is held',
   {'bg': 61, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 65, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 40, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 59, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.8'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 11},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 62, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 2, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 5.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 49, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '7.6'},
   {'action': 'GIVE', 'units': 7.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 12},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 63, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 75, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 9.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 60, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 38, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 3.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '4.7'},
   {'action': 'GIVE', 'units': 4.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 13},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 64, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.0}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 4, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 70, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 7.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 69, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 6.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '6.9'},
   {'action': 'GIVE', 'units': 6.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 14},
   {'action': 'GIVE', 'units': 2.0})],
 [('low glucose with meal is held',
   {'bg': 65, 'target': 110, 'isf': 40, 'carbs_g': 45, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'HOLD', 'units': 0.0}),
  ('glucose exactly 70 is dosed',
   {'bg': 70, 'target': 110, 'isf': 40, 'carbs_g': 90, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('high glucose with insulin on board',
   {'bg': 250, 'target': 120, 'isf': 40, 'carbs_g': 85, 'icr': 10, 'iob': 1, 'max_units': 15},
   {'action': 'GIVE', 'units': 10.5}),
  ('below target with insulin on board',
   {'bg': 90, 'target': 120, 'isf': 30, 'carbs_g': 50, 'icr': 10, 'iob': 5, 'max_units': 15},
   {'action': 'GIVE', 'units': 4.0}),
  ('insulin on board exceeds correction',
   {'bg': 160, 'target': 120, 'isf': 40, 'carbs_g': 80, 'icr': 10, 'iob': 3, 'max_units': 15},
   {'action': 'GIVE', 'units': 8.0}),
  ('pen resolution rounds down',
   {'bg': 120, 'target': 120, 'isf': 40, 'carbs_g': 29, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.5}),
  ('maximum bolus cap not on half unit',
   {'bg': 200, 'target': 120, 'isf': 40, 'carbs_g': 120, 'icr': 10, 'iob': 0, 'max_units': '10.6'},
   {'action': 'GIVE', 'units': 10.5}),
  ('correction only control',
   {'bg': 200, 'target': 100, 'isf': 50, 'carbs_g': 0, 'icr': 10, 'iob': 0, 'max_units': 15},
   {'action': 'GIVE', 'units': 2.0})]]
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 7.3 unit maximum produces a 7.3 unit dose that the pen cannot deliver.

Input {bg, target, isf, carbs_g, icr, iob, max_units}. Toy rule: bg < 70 returns HOLD with 0 units. correction = (bg-target)/isf; when positive, insulin on board is subtracted from it and the result floored at 0; a negative correction (bg below target) is kept and reduces the meal dose. meal = carbs_g/icr. total = max(0, meal + correction), capped at max_units, then rounded DOWN to 0.5 units. Return {action GIVE|HOLD, units}.

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):
    wt = Fraction(str(x['weight_kg']))
    a = x['aptt']
    r = Fraction(str(x['rate']))
    bolus, hold = 0, 0
    if a < 35:
        bolus, r = 80, r + 4
    elif a <= 45:
        bolus, r = 40, r + 2
    elif a <= 70:
        pass
    elif a <= 90:
        r = r - 2
    else:
        hold, r = 60, r - 3
    r = max(r, Fraction(0))
    bolus_units = math.floor(bolus * wt / 100 + Fraction(1, 2)) * 100
    return {'bolus_units': bolus_units, 'hold_min': hold, 'rate_u_kg_h': float(r), 'rate_u_h': math.floor(r * wt + Fraction(1, 2))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('very low aptt bolus',
   {'weight_kg': 71, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5700, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1562}),
  ('aptt exactly 35',
   {'weight_kg': 61, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2400, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1220}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 17},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 19.0, 'rate_u_h': 1520}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 56, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 81, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 96, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 126, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '81.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3300, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1381})],
 [('very low aptt bolus',
   {'weight_kg': 72, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1584}),
  ('aptt exactly 35',
   {'weight_kg': 62, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1240}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 18},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1600}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 57, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 82, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 97, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 127, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '86.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3500, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1466})],
 [('very low aptt bolus',
   {'weight_kg': 73, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1606}),
  ('aptt exactly 35',
   {'weight_kg': 63, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1260}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 19},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 21.0, 'rate_u_h': 1680}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 58, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 83, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 98, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 128, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '91.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3700, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1551})],
 [('very low aptt bolus',
   {'weight_kg': 74, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5900, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1628}),
  ('aptt exactly 35',
   {'weight_kg': 64, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1280}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 20},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1760}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 59, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 84, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 99, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 129, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '76.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3100, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1296})],
 [('very low aptt bolus',
   {'weight_kg': 75, 'aptt': 30, 'rate': 18},
   {'bolus_units': 6000, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1650}),
  ('aptt exactly 35',
   {'weight_kg': 65, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1300}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 21},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 23.0, 'rate_u_h': 1840}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 60, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 85, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 100, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 130, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '71.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 2900, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1211})]]
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):
    wt = Fraction(str(x['weight_kg']))
    wt = wt if wt < 150 else Fraction(120)
    a = x['aptt']
    r = Fraction(str(x['rate']))
    bolus, hold = 0, 0
    if a < 35:
        bolus, r = 80, r + 4
    elif a <= 45:
        bolus, r = 40, r + 2
    elif a <= 70:
        pass
    elif a <= 90:
        r = r - 2
    else:
        hold, r = 60, r - 3
    r = max(r, Fraction(0))
    bolus_units = math.floor(bolus * wt / 100 + Fraction(1, 2)) * 100
    return {'bolus_units': bolus_units, 'hold_min': hold, 'rate_u_kg_h': float(r), 'rate_u_h': math.floor(r * wt + Fraction(1, 2))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('very low aptt bolus',
   {'weight_kg': 71, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5700, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1562}),
  ('aptt exactly 35',
   {'weight_kg': 61, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2400, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1220}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 17},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 19.0, 'rate_u_h': 1520}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 56, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 81, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 96, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 126, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '81.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3300, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1381})],
 [('very low aptt bolus',
   {'weight_kg': 72, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1584}),
  ('aptt exactly 35',
   {'weight_kg': 62, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1240}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 18},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1600}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 57, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 82, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 97, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 127, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '86.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3500, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1466})],
 [('very low aptt bolus',
   {'weight_kg': 73, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1606}),
  ('aptt exactly 35',
   {'weight_kg': 63, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1260}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 19},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 21.0, 'rate_u_h': 1680}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 58, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 83, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 98, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 128, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '91.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3700, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1551})],
 [('very low aptt bolus',
   {'weight_kg': 74, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5900, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1628}),
  ('aptt exactly 35',
   {'weight_kg': 64, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1280}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 20},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1760}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 59, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 84, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 99, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 129, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '76.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3100, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1296})],
 [('very low aptt bolus',
   {'weight_kg': 75, 'aptt': 30, 'rate': 18},
   {'bolus_units': 6000, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1650}),
  ('aptt exactly 35',
   {'weight_kg': 65, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1300}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 21},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 23.0, 'rate_u_h': 1840}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 60, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 85, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 100, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 130, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '71.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 2900, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1211})]]
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 126 kg patient receives a bolus and hourly rate calculated on full body weight.

Input {weight_kg, aptt (integer seconds), rate (units/kg/h)}. Toy nomogram with dosing weight capped at 120 kg: aptt < 35 bolus 80 u/kg and rate +4; 35-45 bolus 40 u/kg and rate +2; 46-70 no change; 71-90 rate -2; > 90 hold 60 minutes and rate -3. Rate is never below 0. Bolus units = bolus u/kg * weight rounded half-up to the nearest 100 units. Return {bolus_units, hold_min, rate_u_kg_h, rate_u_h (half-up integer of rate*weight)}.

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):
    wt = min(Fraction(str(x['weight_kg'])), 120)
    a = x['aptt']
    r = Fraction(str(x['rate']))
    bolus, hold = 0, 0
    if a < 35:
        bolus, r = 80, r + 4
    elif a < 45:
        bolus, r = 40, r + 2
    elif a <= 70:
        pass
    elif a <= 90:
        r = r - 2
    else:
        hold, r = 60, r - 3
    r = max(r, Fraction(0))
    bolus_units = math.floor(bolus * wt / 100 + Fraction(1, 2)) * 100
    return {'bolus_units': bolus_units, 'hold_min': hold, 'rate_u_kg_h': float(r), 'rate_u_h': math.floor(r * wt + Fraction(1, 2))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('very low aptt bolus',
   {'weight_kg': 71, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5700, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1562}),
  ('aptt exactly 35',
   {'weight_kg': 61, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2400, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1220}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 17},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 19.0, 'rate_u_h': 1520}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 56, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 81, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 96, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 126, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '81.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3300, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1381})],
 [('very low aptt bolus',
   {'weight_kg': 72, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1584}),
  ('aptt exactly 35',
   {'weight_kg': 62, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1240}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 18},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1600}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 57, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 82, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 97, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 127, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '86.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3500, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1466})],
 [('very low aptt bolus',
   {'weight_kg': 73, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1606}),
  ('aptt exactly 35',
   {'weight_kg': 63, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1260}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 19},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 21.0, 'rate_u_h': 1680}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 58, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 83, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 98, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 128, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '91.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3700, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1551})],
 [('very low aptt bolus',
   {'weight_kg': 74, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5900, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1628}),
  ('aptt exactly 35',
   {'weight_kg': 64, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1280}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 20},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1760}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 59, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 84, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 99, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 129, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '76.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3100, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1296})],
 [('very low aptt bolus',
   {'weight_kg': 75, 'aptt': 30, 'rate': 18},
   {'bolus_units': 6000, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1650}),
  ('aptt exactly 35',
   {'weight_kg': 65, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1300}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 21},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 23.0, 'rate_u_h': 1840}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 60, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 85, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 100, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 130, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '71.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 2900, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1211})]]
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):
    wt = min(Fraction(str(x['weight_kg'])), 120)
    a = x['aptt']
    r = Fraction(str(x['rate']))
    bolus, hold = 0, 0
    if a < 35:
        bolus, r = 80, r + 4
    elif 35 < a <= 45:
        bolus, r = 40, r + 2
    elif a <= 70:
        pass
    elif a <= 90:
        r = r - 2
    else:
        hold, r = 60, r - 3
    r = max(r, Fraction(0))
    bolus_units = math.floor(bolus * wt / 100 + Fraction(1, 2)) * 100
    return {'bolus_units': bolus_units, 'hold_min': hold, 'rate_u_kg_h': float(r), 'rate_u_h': math.floor(r * wt + Fraction(1, 2))}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('very low aptt bolus',
   {'weight_kg': 71, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5700, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1562}),
  ('aptt exactly 35',
   {'weight_kg': 61, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2400, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1220}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 17},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 19.0, 'rate_u_h': 1520}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 56, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 81, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 96, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 126, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '81.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3300, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1381})],
 [('very low aptt bolus',
   {'weight_kg': 72, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1584}),
  ('aptt exactly 35',
   {'weight_kg': 62, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1240}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 18},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1600}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 57, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 82, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 97, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 127, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '86.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3500, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1466})],
 [('very low aptt bolus',
   {'weight_kg': 73, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5800, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1606}),
  ('aptt exactly 35',
   {'weight_kg': 63, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2500, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1260}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 19},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 21.0, 'rate_u_h': 1680}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 58, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 83, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 98, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 128, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '91.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3700, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1551})],
 [('very low aptt bolus',
   {'weight_kg': 74, 'aptt': 30, 'rate': 18},
   {'bolus_units': 5900, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1628}),
  ('aptt exactly 35',
   {'weight_kg': 64, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1280}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 20},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1760}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 59, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 84, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 99, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 129, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '76.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 3100, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1296})],
 [('very low aptt bolus',
   {'weight_kg': 75, 'aptt': 30, 'rate': 18},
   {'bolus_units': 6000, 'hold_min': 0, 'rate_u_kg_h': 22.0, 'rate_u_h': 1650}),
  ('aptt exactly 35',
   {'weight_kg': 65, 'aptt': 35, 'rate': 18},
   {'bolus_units': 2600, 'hold_min': 0, 'rate_u_kg_h': 20.0, 'rate_u_h': 1300}),
  ('aptt exactly 45',
   {'weight_kg': 80, 'aptt': 45, 'rate': 21},
   {'bolus_units': 3200, 'hold_min': 0, 'rate_u_kg_h': 23.0, 'rate_u_h': 1840}),
  ('therapeutic control',
   {'weight_kg': 75, 'aptt': 60, 'rate': 18},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 18.0, 'rate_u_h': 1350}),
  ('reduction clamps at zero',
   {'weight_kg': 70, 'aptt': 85, 'rate': 1},
   {'bolus_units': 0, 'hold_min': 0, 'rate_u_kg_h': 0.0, 'rate_u_h': 0}),
  ('supratherapeutic hold',
   {'weight_kg': 90, 'aptt': 100, 'rate': 12},
   {'bolus_units': 0, 'hold_min': 60, 'rate_u_kg_h': 9.0, 'rate_u_h': 810}),
  ('weight above cap',
   {'weight_kg': 130, 'aptt': 40, 'rate': 15},
   {'bolus_units': 4800, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 2040}),
  ('half-hundred bolus rounds up',
   {'weight_kg': '71.25', 'aptt': 40, 'rate': 15},
   {'bolus_units': 2900, 'hold_min': 0, 'rate_u_kg_h': 17.0, 'rate_u_h': 1211})]]
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:
An aPTT of exactly 45 s gets no bolus or rate increase.

Input {weight_kg, aptt (integer seconds), rate (units/kg/h)}. Toy nomogram with dosing weight capped at 120 kg: aptt < 35 bolus 80 u/kg and rate +4; 35-45 bolus 40 u/kg and rate +2; 46-70 no change; 71-90 rate -2; > 90 hold 60 minutes and rate -3. Rate is never below 0. Bolus units = bolus u/kg * weight rounded half-up to the nearest 100 units. Return {bolus_units, hold_min, rate_u_kg_h, rate_u_h (half-up integer of rate*weight)}.

split: open-access