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

N = 1
observations = []
def solve(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action == 'delivered':
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    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(report):
    t = report['type'].lower()
    params = {k.lower(): v for k, v in report.get('params', {}).items()}
    rt = params.get('report-type', '').lower()
    parts = report.get('parts', [])
    if t != 'multipart/report' or rt != 'delivery-status' or len(parts) < 2 or parts[1]['type'].lower() not in ('message/delivery-status', 'message/global-delivery-status'):
        return {'valid': False, 'recipients': [], 'has_original': False}
    rows = []
    for r in parts[1]['recipients']:
        action = r['action'].strip().lower()
        klass = r['status'].strip()[:1]
        if action == 'failed':
            verdict = 'hard' if klass == '5' else ('soft' if klass == '4' else 'unknown')
        elif action == 'delayed':
            verdict = 'delayed'
        elif action in ('delivered', 'relayed'):
            verdict = 'ok'
        else:
            verdict = 'unknown'
        rows.append([r['final'].strip(), verdict])
    orig = len(parts) > 2 and parts[2]['type'].lower() in ('message/rfc822', 'text/rfc822-headers')
    return {'valid': True, 'recipients': rows, 'has_original': orig}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a1@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a1@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.1'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd1@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d1@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.1'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.1'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 2: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a2@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a2@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.2'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd2@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d2@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.2'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.2'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 3: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a3@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a3@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.3'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd3@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d3@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.3'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.3'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 4: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a4@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a4@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.4'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd4@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d4@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.4'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.4'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})], 5: [('capitalised parameter name', ({'type': 'multipart/report', 'params': {'Report-Type': 'Delivery-Status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'a5@x', 'action': 'failed', 'status': '5.1.1'}]}, {'type': 'message/rfc822'}]},), {'has_original': True, 'recipients': [['a5@x', 'hard']], 'valid': True}), ('internationalised status part', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global-delivery-status', 'recipients': [{'final': 'b@x', 'action': 'delayed', 'status': '4.4.5'}]}]},), {'has_original': False, 'recipients': [['b@x', 'delayed']], 'valid': True}), ('global message is not a status', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/global', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False}), ('status class decides', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'c@x', 'action': 'failed', 'status': '4.2.2'}, {'final': 'd5@x', 'action': 'failed', 'status': '2.0.0'}]}]},), {'has_original': False, 'recipients': [['c@x', 'soft'], ['d5@x', 'unknown']], 'valid': True}), ('padded capitalised action', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': ' e@x ', 'action': ' Failed ', 'status': ' 5.7.5'}]}, {'type': 'text/rfc822-headers'}]},), {'has_original': True, 'recipients': [['e@x', 'hard']], 'valid': True}), ('relayed and expanded', ({'type': 'multipart/report', 'params': {'report-type': 'delivery-status'}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': [{'final': 'f@x', 'action': 'relayed', 'status': '2.0.0'}, {'final': 'g@x', 'action': 'expanded', 'status': '2.0.5'}]}]},), {'has_original': False, 'recipients': [['f@x', 'ok'], ['g@x', 'ok']], 'valid': True}), ('not a report', ({'type': 'multipart/mixed', 'params': {}, 'parts': [{'type': 'text/plain'}, {'type': 'message/delivery-status', 'recipients': []}]},), {'has_original': False, 'recipients': [], 'valid': False})]}
for _label, _args, _expected in _CASES[N]:
    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:
Relayed or expanded recipients are reported with an unknown outcome.

A report is valid only when its type is multipart/report, its report-type parameter (name and value case-insensitive) is delivery-status, and its second part is message/delivery-status or message/global-delivery-status. Each recipient {final, action, status} gets: failed -> hard for status class 5, soft for 4, unknown otherwise; delayed -> delayed; delivered, relayed or expanded -> ok; anything else unknown (actions are case-insensitive and trimmed). has_original is true when a third part is message/rfc822 or text/rfc822-headers. Invalid reports give {valid False, recipients [], has_original False}.

split: open-access


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

N = 1
observations = []
def solve(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'][f['number']] = f['body']
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'][f['number']] = g['parts'].get(f['number'], '') + f['body']
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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 retransmitted fragment replaces the one already received, corrupting the reassembled message.

fragments {id, number, total (optional), body}. Per id: the first fragment with a given number wins; totals seen on fragments must all agree (otherwise "conflict"); without any total the message is "incomplete"; it is "complete" when every number 1..total is present, and its text joins those numbers in order (other numbers are ignored). Result rows [id, status, text or None] sorted by id.

split: open-access


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

N = 1
observations = []
def solve(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        total = g['totals'][0] if g['totals'] else max(g['parts'])
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        total = g['totals'][0] if g['totals'] else len(g['parts'])
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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 message is declared complete before the fragment carrying the total has arrived.

fragments {id, number, total (optional), body}. Per id: the first fragment with a given number wins; totals seen on fragments must all agree (otherwise "conflict"); without any total the message is "incomplete"; it is "complete" when every number 1..total is present, and its text joins those numbers in order (other numbers are ignored). Result rows [id, status, text or None] sorted by id.

split: open-access


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

N = 1
observations = []
def solve(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(g['totals']) > 1:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(g['totals']) > 1 and g['totals'][0] != g['totals'][-1]:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].get(n, '') for n in range(1, total + 1))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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:
Fragments that repeat the same total are reported as conflicting.

fragments {id, number, total (optional), body}. Per id: the first fragment with a given number wins; totals seen on fragments must all agree (otherwise "conflict"); without any total the message is "incomplete"; it is "complete" when every number 1..total is present, and its text joins those numbers in order (other numbers are ignored). Result rows [id, status, text or None] sorted by id.

split: open-access


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

N = 1
observations = []
def solve(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'].values())])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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(fragments):
    groups = {}
    for f in fragments:
        g = groups.setdefault(f['id'], {'totals': [], 'parts': {}})
        if f.get('total') is not None:
            g['totals'].append(f['total'])
        g['parts'].setdefault(f['number'], f['body'])
    out = []
    for pid in sorted(groups):
        g = groups[pid]
        if len(set(g['totals'])) > 1:
            out.append([pid, 'conflict', None])
            continue
        if not g['totals']:
            out.append([pid, 'incomplete', None])
            continue
        total = g['totals'][0]
        if all(n in g['parts'] for n in range(1, total + 1)):
            out.append([pid, 'complete', ''.join(g['parts'][n] for n in sorted(g['parts']))])
        else:
            out.append([pid, 'incomplete', None])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD1'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD1']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b1'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '11'}],), [['r', 'complete', '1123']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z1'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one1'}],), [['t', 'complete', 'one1two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z1'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w1', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w1', 'complete', 'solo']])], 2: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD2'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD2']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b2'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '12'}],), [['r', 'complete', '1223']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z2'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one2'}],), [['t', 'complete', 'one2two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z2'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w2', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w2', 'complete', 'solo']])], 3: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD3'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD3']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b3'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '13'}],), [['r', 'complete', '1323']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z3'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one3'}],), [['t', 'complete', 'one3two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z3'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w3', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w3', 'complete', 'solo']])], 4: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD4'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD4']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b4'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '14'}],), [['r', 'complete', '1423']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z4'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one4'}],), [['t', 'complete', 'one4two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z4'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w4', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w4', 'complete', 'solo']])], 5: [('retransmitted fragment', ([{'id': 'p', 'number': 1, 'body': 'AB'}, {'id': 'p', 'number': 2, 'total': 2, 'body': 'CD5'}, {'id': 'p', 'number': 1, 'body': 'XX'}],), [['p', 'complete', 'ABCD5']]), ('no total yet', ([{'id': 'q', 'number': 1, 'body': 'a'}, {'id': 'q', 'number': 2, 'body': 'b5'}],), [['q', 'incomplete', None]]), ('repeated equal totals', ([{'id': 'r', 'number': 2, 'total': 3, 'body': '2'}, {'id': 'r', 'number': 3, 'total': 3, 'body': '3'}, {'id': 'r', 'number': 1, 'body': '15'}],), [['r', 'complete', '1523']]), ('disagreeing totals', ([{'id': 's', 'number': 1, 'total': 3, 'body': 'x'}, {'id': 's', 'number': 2, 'total': 4, 'body': 'y'}, {'id': 's', 'number': 3, 'total': 3, 'body': 'z5'}],), [['s', 'conflict', None]]), ('out of order with stray number', ([{'id': 't', 'number': 2, 'total': 2, 'body': 'two'}, {'id': 't', 'number': 3, 'body': 'junk'}, {'id': 't', 'number': 1, 'body': 'one5'}],), [['t', 'complete', 'one5two']]), ('missing middle with stray', ([{'id': 'u', 'number': 1, 'total': 2, 'body': 'a'}, {'id': 'u', 'number': 5, 'body': 'z5'}],), [['u', 'incomplete', None]]), ('two messages', ([{'id': 'w5', 'number': 1, 'total': 1, 'body': 'solo'}, {'id': 'v', 'number': 1, 'total': 2, 'body': 'half'}],), [['v', 'incomplete', None], ['w5', 'complete', 'solo']])]}
for _label, _args, _expected in _CASES[N]:
    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:
Fragments are joined in arrival order instead of by number.

fragments {id, number, total (optional), body}. Per id: the first fragment with a given number wins; totals seen on fragments must all agree (otherwise "conflict"); without any total the message is "incomplete"; it is "complete" when every number 1..total is present, and its text joins those numbers in order (other numbers are ignored). Result rows [id, status, text or None] sorted by id.

split: open-access