broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + q) // (2 * q)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + max(onhand, 1)) // (2 * max(onhand, 1))
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
After a receipt the new average is far too high because the pooled cost is divided by the receipt quantity only.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = total // units
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 2, 'events': [['recv', 1, 3], ['issue', 2]]}, {'cogs': [6], 'onhand': 0, 'avg': 3}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 4, 'events': [['recv', 1, 5], ['issue', 2]]}, {'cogs': [10], 'onhand': 0, 'avg': 5}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 6, 'events': [['recv', 1, 7], ['issue', 2]]}, {'cogs': [14], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 8, 'events': [['recv', 1, 9], ['issue', 2]]}, {'cogs': [18], 'onhand': 0, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 10, 'events': [['recv', 1, 11], ['issue', 2]]}, {'cogs': [22], 'onhand': 0, 'avg': 11}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = round(total / units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 2, 'events': [['recv', 1, 3], ['issue', 2]]}, {'cogs': [6], 'onhand': 0, 'avg': 3}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 4, 'events': [['recv', 1, 5], ['issue', 2]]}, {'cogs': [10], 'onhand': 0, 'avg': 5}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 6, 'events': [['recv', 1, 7], ['issue', 2]]}, {'cogs': [14], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 8, 'events': [['recv', 1, 9], ['issue', 2]]}, {'cogs': [18], 'onhand': 0, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['exact half', {'onhand': 1, 'avg': 10, 'events': [['recv', 1, 11], ['issue', 2]]}, {'cogs': [22], 'onhand': 0, 'avg': 11}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Averages that land exactly on half a cent are truncated, drifting unit cost down over many receipts.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = x['onhand'] * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 21], ['issue', 1]]}, {'cogs': [30, 16], 'onhand': 5, 'avg': 16}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 51], ['issue', 1]]}, {'cogs': [26], 'onhand': 4, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 22], ['issue', 1]]}, {'cogs': [30, 16], 'onhand': 5, 'avg': 16}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 52], ['issue', 1]]}, {'cogs': [26], 'onhand': 4, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 23], ['issue', 1]]}, {'cogs': [30, 17], 'onhand': 5, 'avg': 17}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 53], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 24], ['issue', 1]]}, {'cogs': [30, 17], 'onhand': 5, 'avg': 17}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 54], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 25], ['issue', 1]]}, {'cogs': [30, 18], 'onhand': 5, 'avg': 18}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 55], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * x['avg'] + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 21], ['issue', 1]]}, {'cogs': [30, 16], 'onhand': 5, 'avg': 16}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 51], ['issue', 1]]}, {'cogs': [26], 'onhand': 4, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 22], ['issue', 1]]}, {'cogs': [30, 16], 'onhand': 5, 'avg': 16}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 52], ['issue', 1]]}, {'cogs': [26], 'onhand': 4, 'avg': 26}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 23], ['issue', 1]]}, {'cogs': [30, 17], 'onhand': 5, 'avg': 17}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 53], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 24], ['issue', 1]]}, {'cogs': [30, 17], 'onhand': 5, 'avg': 17}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 54], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['issue before receipt', {'onhand': 6, 'avg': 10, 'events': [['issue', 3], ['recv', 3, 25], ['issue', 1]]}, {'cogs': [30, 18], 'onhand': 5, 'avg': 18}], ['two receipts', {'onhand': 2, 'avg': 10, 'events': [['recv', 2, 30], ['recv', 1, 55], ['issue', 1]]}, {'cogs': [27], 'onhand': 4, 'avg': 27}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The first receipt after an issue is averaged as if the issued units were still on hand.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                onhand -= q
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 5], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 6], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 7], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 8], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 9], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                onhand = max(0, onhand - q)
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 5], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 6], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 7], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 8], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['recover after short', {'onhand': 3, 'avg': 7, 'events': [['issue', 9], ['issue', 3]]}, {'cogs': ['short', 21], 'onhand': 0, 'avg': 7}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A rejected over-issue still drains on-hand units, making later issues short too.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
            if onhand == 0:
                avg = 0
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 41, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [41, 82], 'onhand': 0, 'avg': 41}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 42, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [42, 84], 'onhand': 0, 'avg': 42}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 43, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [43, 86], 'onhand': 0, 'avg': 43}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 44, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [44, 88], 'onhand': 0, 'avg': 44}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 45, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [45, 90], 'onhand': 0, 'avg': 45}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
            if onhand == 0 and ev is x['events'][-1]:
                avg = 0
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 41, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [41, 82], 'onhand': 0, 'avg': 41}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 42, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [42, 84], 'onhand': 0, 'avg': 42}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 43, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [43, 86], 'onhand': 0, 'avg': 43}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 44, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [44, 88], 'onhand': 0, 'avg': 44}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['ends empty', {'onhand': 3, 'avg': 45, 'events': [['issue', 1], ['issue', 2]]}, {'cogs': [45, 90], 'onhand': 0, 'avg': 45}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An issue that empties stock zeroes the average, so the next receipt is costed with a zero-weight but reported avg is wrong in between.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q >= onhand:
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 1, 9]]}, {'cogs': [9], 'onhand': 1, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 2, 9]]}, {'cogs': [9], 'onhand': 2, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 3, 9]]}, {'cogs': [9], 'onhand': 3, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 4, 9]]}, {'cogs': [9], 'onhand': 4, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 5, 9]]}, {'cogs': [9], 'onhand': 5, 'avg': 9}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    onhand = x['onhand']
    avg = x['avg']
    cogs = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            total = onhand * avg + q * c
            units = onhand + q
            avg = (2 * total + units) // (2 * units)
            onhand = units
        else:
            q = ev[1]
            if q > onhand or (q == onhand and onhand == 1):
                cogs.append('short')
                continue
            cogs.append(q * avg)
            onhand -= q
    return {'cogs': cogs, 'onhand': onhand, 'avg': avg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 131], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 12], ['issue', 2], ['issue', 1]]}, {'cogs': [22, 11], 'onhand': 4, 'avg': 11}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 71], ['issue', 1]]}, {'cogs': [100, 71], 'onhand': 2, 'avg': 71}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 4]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 1, 20], ['issue', 3]]}, {'cogs': [42], 'onhand': 1, 'avg': 14}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 4], ['issue', 1]]}, {'cogs': [3], 'onhand': 1, 'avg': 3}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 1, 9]]}, {'cogs': [9], 'onhand': 1, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 132], ['issue', 5]]}, {'cogs': [580], 'onhand': 15, 'avg': 116}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 13], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 72], ['issue', 1]]}, {'cogs': [100, 72], 'onhand': 2, 'avg': 72}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 5]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 2, 20], ['issue', 3]]}, {'cogs': [45], 'onhand': 2, 'avg': 15}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 6], ['issue', 1]]}, {'cogs': [4], 'onhand': 1, 'avg': 4}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 2, 9]]}, {'cogs': [9], 'onhand': 2, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 133], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 14], ['issue', 2], ['issue', 1]]}, {'cogs': [24, 12], 'onhand': 4, 'avg': 12}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 73], ['issue', 1]]}, {'cogs': [100, 73], 'onhand': 2, 'avg': 73}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 6]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 3, 20], ['issue', 3]]}, {'cogs': [48], 'onhand': 3, 'avg': 16}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 8], ['issue', 1]]}, {'cogs': [5], 'onhand': 1, 'avg': 5}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 3, 9]]}, {'cogs': [9], 'onhand': 3, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 134], ['issue', 5]]}, {'cogs': [585], 'onhand': 15, 'avg': 117}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 15], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 74], ['issue', 1]]}, {'cogs': [100, 74], 'onhand': 2, 'avg': 74}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 7]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 4, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 4, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 10], ['issue', 1]]}, {'cogs': [6], 'onhand': 1, 'avg': 6}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 4, 9]]}, {'cogs': [9], 'onhand': 4, 'avg': 9}]], [['receipt reprices', {'onhand': 10, 'avg': 100, 'events': [['recv', 10, 135], ['issue', 5]]}, {'cogs': [590], 'onhand': 15, 'avg': 118}], ['uneven rounding', {'onhand': 3, 'avg': 10, 'events': [['recv', 4, 16], ['issue', 2], ['issue', 1]]}, {'cogs': [26, 13], 'onhand': 4, 'avg': 13}], ['zero then receipt', {'onhand': 2, 'avg': 50, 'events': [['issue', 2], ['recv', 3, 75], ['issue', 1]]}, {'cogs': [100, 75], 'onhand': 2, 'avg': 75}], ['short issue', {'onhand': 2, 'avg': 50, 'events': [['issue', 8]]}, {'cogs': ['short'], 'onhand': 2, 'avg': 50}], ['many receipts', {'onhand': 0, 'avg': 0, 'events': [['recv', 1, 10], ['recv', 2, 13], ['recv', 5, 20], ['issue', 3]]}, {'cogs': [51], 'onhand': 5, 'avg': 17}], ['half cent', {'onhand': 1, 'avg': 1, 'events': [['recv', 1, 12], ['issue', 1]]}, {'cogs': [7], 'onhand': 1, 'avg': 7}], ['issue all', {'onhand': 4, 'avg': 25, 'events': [['recv', 4, 26], ['issue', 8]]}, {'cogs': [208], 'onhand': 0, 'avg': 26}], ['single unit', {'onhand': 1, 'avg': 9, 'events': [['issue', 1], ['recv', 5, 9]]}, {'cogs': [9], 'onhand': 5, 'avg': 9}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Issuing exactly the on-hand quantity is rejected as short.

Input {onhand, avg (cents, integer), events}. On receipt the average becomes round-half-up((onhand*avg + qty*cost)/(onhand+qty)) to whole cents; an issue is costed at qty*avg with the current average and never changes it; an issue exceeding on-hand returns "short". When on-hand reaches zero the average is kept as the last value (not reset). Return {cogs, onhand, avg}.

split: open-access