FAILURE MAP
← Case archive

FA-63966 / Medication dosing calculations / Open access

Tablet strength selection with scored halves: tolerance percentage base · case 01

Large doses are rejected even when the percentage deviation is within tolerance.

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

ROOT CAUSE

The tolerance percentage is compared with an absolute milligram difference.

VERIFIED REPAIR

Compare the deviation with tol_pct percent of the target dose.

Unsuccessful approach: Measuring the percentage against the achieved dose tightens tolerance when rounding down.

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) > 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)
Boundary fixtureActualExpectedOutcome
unscored tablet may not be halved{'dose_mg': 10.0, 'status': 'OK', 'strength': 10, 'tablets': 1.0}{'status': 'NO_MATCH'}Failed
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{'status': 'NO_MATCH'}{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}Failed
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 / 1755017054d54840d5e8fb784bf97be25c98e9ae45947b341418c6bcd33b550a

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'] * 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)
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{'status': 'NO_MATCH'}{'dose_mg': 150.0, 'status': 'OK', 'strength': 150, 'tablets': 1.0}Failed
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 / 0fe3635e9baa7cc435c21356274726fcba73fe54a57e5fd49ef809e2c6bb4c48

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.796146+00:00.

Case digest / 259d8191d22903734446d87ac82756dbaaa7bf8ddbf0de68ab36c1efbb50b32d