broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step)
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.ceil(target / step)
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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 14 mg order with 5 mg tablets finds no match although 3 tablets are within tolerance.

Input {target_mg, strengths, scored (strengths that may be halved), tol_pct}. For each strength s: step = s/2 if s is scored else s; units = target/step rounded half-up (skip if 0); dose = units*step; tablets = dose/s (skip if more than 4 tablets); keep only |dose-target|*100 <= tol_pct*target. Prefer smallest absolute error, then fewest physical tablets (ceil of tablets), then the largest strength. Return {status OK, strength, tablets, dose_mg} or {status NO_MATCH}.

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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if units > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs >= 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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 3.5-tablet regimen of a scored product is rejected.

Input {target_mg, strengths, scored (strengths that may be halved), tol_pct}. For each strength s: step = s/2 if s is scored else s; units = target/step rounded half-up (skip if 0); dose = units*step; tablets = dose/s (skip if more than 4 tablets); keep only |dose-target|*100 <= tol_pct*target. Prefer smallest absolute error, then fewest physical tablets (ceil of tablets), then the largest strength. Return {status OK, strength, tablets, dose_mg} or {status NO_MATCH}.

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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) > x['tol_pct']:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * dose:
            continue
        key = (abs(dose - target), math.ceil(tabs), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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:
Large doses are rejected even when the percentage deviation is within tolerance.

Input {target_mg, strengths, scored (strengths that may be halved), tol_pct}. For each strength s: step = s/2 if s is scored else s; units = target/step rounded half-up (skip if 0); dose = units*step; tablets = dose/s (skip if more than 4 tablets); keep only |dose-target|*100 <= tol_pct*target. Prefer smallest absolute error, then fewest physical tablets (ceil of tablets), then the largest strength. Return {status OK, strength, tablets, dose_mg} or {status NO_MATCH}.

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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (abs(dose - target), math.ceil(tabs), s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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):
    target = Fraction(str(x['target_mg']))
    best = None
    for s in sorted(x['strengths']):
        step = Fraction(s, 2) if s in x['scored'] else Fraction(s)
        units = math.floor(target / step + Fraction(1, 2))
        if units == 0:
            continue
        dose = units * step
        tabs = dose / s
        if tabs > 4:
            continue
        if abs(dose - target) * 100 > x['tol_pct'] * target:
            continue
        key = (math.ceil(tabs), abs(dose - target), -s)
        if best is None or key < best[0]:
            best = (key, s, tabs, dose)
    if best is None:
        return {'status': 'NO_MATCH'}
    return {'status': 'OK', 'strength': best[1], 'tablets': float(best[2]), 'dose_mg': float(best[3])}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('unscored tablet may not be halved',
   {'target_mg': 5, 'strengths': [10, 25], 'scored': [25], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 6},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 11},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 1},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 27},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 1},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 6},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 15, 'strengths': [30, 75], 'scored': [75], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 7},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 12},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 2},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 28},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 2},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 7},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 25, 'strengths': [50, 125], 'scored': [125], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 8},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 13},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 3},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 29},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 3},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 8},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 35, 'strengths': [70, 175], 'scored': [175], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 9},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 14},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 4},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 30},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 4},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 9},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.0})],
 [('unscored tablet may not be halved',
   {'target_mg': 45, 'strengths': [90, 225], 'scored': [225], 'tol_pct': 10},
   {'status': 'NO_MATCH'}),
  ('scored tablet half used',
   {'target_mg': '7.5', 'strengths': [5, 10], 'scored': [5], 'tol_pct': 10},
   {'status': 'OK', 'strength': 5, 'tablets': 1.5, 'dose_mg': 7.5}),
  ('near dose rounds up to whole tablet',
   {'target_mg': 14, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 3.0, 'dose_mg': 15.0}),
  ('near dose rounds down to whole tablet',
   {'target_mg': 11, 'strengths': [5], 'scored': [], 'tol_pct': 15},
   {'status': 'OK', 'strength': 5, 'tablets': 2.0, 'dose_mg': 10.0}),
  ('three and a half tablets allowed',
   {'target_mg': 35, 'strengths': [10], 'scored': [10], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 3.5, 'dose_mg': 35.0}),
  ('exactly four tablets allowed',
   {'target_mg': 40, 'strengths': [10], 'scored': [], 'tol_pct': 5},
   {'status': 'OK', 'strength': 10, 'tablets': 4.0, 'dose_mg': 40.0}),
  ('tolerance measured against target',
   {'target_mg': 200, 'strengths': [150], 'scored': [], 'tol_pct': 31},
   {'status': 'OK', 'strength': 150, 'tablets': 1.0, 'dose_mg': 150.0}),
  ('equal error prefers larger strength',
   {'target_mg': 30, 'strengths': [15, 20], 'scored': [20], 'tol_pct': 5},
   {'status': 'OK', 'strength': 20, 'tablets': 1.5, 'dose_mg': 30.0}),
  ('accuracy outranks tablet count',
   {'target_mg': 21, 'strengths': [10, 7], 'scored': [], 'tol_pct': 10},
   {'status': 'OK', 'strength': 7, 'tablets': 3.0, 'dose_mg': 21.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:
Two tablets of a smaller strength are chosen over one and a half of a larger strength giving the same dose.

Input {target_mg, strengths, scored (strengths that may be halved), tol_pct}. For each strength s: step = s/2 if s is scored else s; units = target/step rounded half-up (skip if 0); dose = units*step; tablets = dose/s (skip if more than 4 tablets); keep only |dose-target|*100 <= tol_pct*target. Prefer smallest absolute error, then fewest physical tablets (ceil of tablets), then the largest strength. Return {status OK, strength, tablets, dose_mg} or {status NO_MATCH}.

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)

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 and x['carbs_g'] == 0:
        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)

prompt:
A glucose of exactly 70 mg/dL blocks a meal bolus.

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