FAILURE MAP
← Case archive

FA-63971 / Medication dosing calculations / Open access

Tablet strength selection with scored halves: candidate ranking · case 01

Two tablets of a smaller strength are chosen over one and a half of a larger strength giving the same dose.

Verified by executionVariant 1 · 9 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The final tie-break prefers the smallest strength instead of the largest.

VERIFIED REPAIR

Rank by absolute error, then physical tablet count, then larger strength.

Unsuccessful approach: Putting tablet count first lets a less accurate dose win whenever it needs fewer tablets.

Case contract

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}.

Why this case matters

Dispensing systems translate an ordered dose into real tablets; halving rules, tolerance, and ranking are separate decisions.

1 / The failure

Exit 1
"""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)
Boundary fixtureActualExpectedOutcome
unscored tablet may not be halved{'status': 'NO_MATCH'}{'status': 'NO_MATCH'}Passed
scored tablet half used{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}Passed
near dose rounds up to whole tablet{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}Passed
near dose rounds down to whole tablet{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}Passed
three and a half tablets allowed{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}Passed
exactly four tablets allowed{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}Passed
tolerance measured against target{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}Passed
equal error prefers larger strength{'dose_mg': 30.0, 'status': 'OK', 'strength': 15, 'tablets': 2.0}{'dose_mg': 30.0, 'status': 'OK', 'strength': 20, 'tablets': 1.5}Failed
accuracy outranks tablet count{'dose_mg': 21.0, 'status': 'OK', 'strength': 7, 'tablets': 3.0}{'dose_mg': 21.0, 'status': 'OK', 'strength': 7, 'tablets': 3.0}Passed

SHA-256 / 8894ad7574679eef10bc774d185f8bd83bd9faccbeefe8eb204f4ff0d90361e1

2 / The unsuccessful fix

Exit 1
"""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)
Boundary fixtureActualExpectedOutcome
unscored tablet may not be halved{'status': 'NO_MATCH'}{'status': 'NO_MATCH'}Passed
scored tablet half used{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}Passed
near dose rounds up to whole tablet{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}Passed
near dose rounds down to whole tablet{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}Passed
three and a half tablets allowed{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}Passed
exactly four tablets allowed{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}Passed
tolerance measured against target{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}Passed
equal error prefers larger strength{'dose_mg': 30.0, 'status': 'OK', 'strength': 20, 'tablets': 1.5}{'dose_mg': 30.0, 'status': 'OK', 'strength': 20, 'tablets': 1.5}Passed
accuracy outranks tablet count{'dose_mg': 20.0, 'status': 'OK', 'strength': 10, 'tablets': 2.0}{'dose_mg': 21.0, 'status': 'OK', 'strength': 7, 'tablets': 3.0}Failed

SHA-256 / db36e11eb62784371b690180415d23c84e2f33b6ad0b12d8822396e19fd25dfd

3 / The verified repair

Exit 0
"""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)
Boundary fixtureActualExpectedOutcome
unscored tablet may not be halved{'status': 'NO_MATCH'}{'status': 'NO_MATCH'}Passed
scored tablet half used{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}{'dose_mg': 7.5, 'status': 'OK', 'strength': 5, 'tablets': 1.5}Passed
near dose rounds up to whole tablet{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}{'dose_mg': 15.0, 'status': 'OK', 'strength': 5, 'tablets': 3.0}Passed
near dose rounds down to whole tablet{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}{'dose_mg': 10.0, 'status': 'OK', 'strength': 5, 'tablets': 2.0}Passed
three and a half tablets allowed{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}{'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5}Passed
exactly four tablets allowed{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}{'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0}Passed
tolerance measured against target{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}Passed
equal error prefers larger strength{'dose_mg': 30.0, 'status': 'OK', 'strength': 20, 'tablets': 1.5}{'dose_mg': 30.0, 'status': 'OK', 'strength': 20, 'tablets': 1.5}Passed
accuracy outranks tablet count{'dose_mg': 21.0, 'status': 'OK', 'strength': 7, 'tablets': 3.0}{'dose_mg': 21.0, 'status': 'OK', 'strength': 7, 'tablets': 3.0}Passed

SHA-256 / 4a35ff02faa3cbdbb3323f7349bfcd6bcd62d917328ee74ebee268a6e3408893

Verification & scope

A deterministic toy software model with explicitly stipulated thresholds and formulas; it is not clinical guidance, not a validated medical calculator, and makes no claim of conformance to any published protocol. Numbered variants vary patient and order inputs. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.

Observations recorded using Python 3.12.14 at 2026-09-29T14:47:19.836475+00:00.

Case digest / 74565fc93bfb32988807fbad895b16648bbfce4e48ac7d7cb8e3c1fa3ca0e277