FA-63961 / Medication dosing calculations / Open access
Tablet strength selection with scored halves: maximum tablet count · case 01
A 3.5-tablet regimen of a scored product is rejected.
ROOT CAUSE
The four-tablet limit is applied to half-tablet units instead of physical tablets.
VERIFIED REPAIR
Compare the tablet count dose/s with the limit of 4.
Unsuccessful approach: Using >= 4 rejects exactly four whole tablets, which the limit allows.
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 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)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| 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 | {'status': 'NO_MATCH'} | {'dose_mg': 35.0, 'status': 'OK', 'strength': 10, 'tablets': 3.5} | Failed |
| 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 / e16a1f452e2659827fbe716b523bd3e2ce7335830d6b61d22a298199a86d4f8d
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 = (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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| 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 | {'status': 'NO_MATCH'} | {'dose_mg': 40.0, 'status': 'OK', 'strength': 10, 'tablets': 4.0} | Failed |
| 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 / 711bbfc365f566fb452e87ac9e85008f84f44dc139fdd12b7058f3bcdf320ace
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 fixture | Actual | Expected | Outcome |
|---|---|---|---|
| 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.797728+00:00.
Case digest / aa3b4bd1419c4f9a2534c9b80cb587b09e7ffc32302597e02a7560c86dfe81c9