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

N = 1
observations = []
def solve(parts, html_cids):
    out = []
    have_body = False
    for p in parts:
        disp = (p.get('disp') or '').lower()
        if disp == 'attachment':
            kind = 'attachment'
        elif p.get('cid') and p['cid'] in html_cids:
            kind = 'inline'
        elif p['type'].startswith('text/') and not p.get('filename') and not have_body:
            kind = 'body'
            have_body = True
        elif disp == 'inline':
            kind = 'inline'
        else:
            kind = 'attachment'
        out.append(kind)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo1@x', 'disp': 'attachment'}], ['logo1@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes1.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a1@x'}, {'type': 'image/png', 'cid': 'ba1@x'}], ['a1@x']), ['body', 'inline', 'attachment'])], 2: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo2@x', 'disp': 'attachment'}], ['logo2@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes2.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a2@x'}, {'type': 'image/png', 'cid': 'ba2@x'}], ['a2@x']), ['body', 'inline', 'attachment'])], 3: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo3@x', 'disp': 'attachment'}], ['logo3@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes3.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a3@x'}, {'type': 'image/png', 'cid': 'ba3@x'}], ['a3@x']), ['body', 'inline', 'attachment'])], 4: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo4@x', 'disp': 'attachment'}], ['logo4@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes4.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a4@x'}, {'type': 'image/png', 'cid': 'ba4@x'}], ['a4@x']), ['body', 'inline', 'attachment'])], 5: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo5@x', 'disp': 'attachment'}], ['logo5@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes5.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a5@x'}, {'type': 'image/png', 'cid': 'ba5@x'}], ['a5@x']), ['body', 'inline', 'attachment'])]}
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(parts, html_cids):
    out = []
    have_body = False
    for p in parts:
        disp = (p.get('disp') or '').lower()
        if disp == 'attachment':
            kind = 'attachment'
        elif p.get('cid') and p['cid'] in html_cids:
            kind = 'inline'
        elif p['type'].startswith('text/') and not p.get('filename') and not have_body:
            kind = 'body'
            have_body = True
        elif disp == 'inline' and not p['type'].startswith('application/'):
            kind = 'inline'
        else:
            kind = 'attachment'
        out.append(kind)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo1@x', 'disp': 'attachment'}], ['logo1@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes1.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a1@x'}, {'type': 'image/png', 'cid': 'ba1@x'}], ['a1@x']), ['body', 'inline', 'attachment'])], 2: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo2@x', 'disp': 'attachment'}], ['logo2@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes2.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a2@x'}, {'type': 'image/png', 'cid': 'ba2@x'}], ['a2@x']), ['body', 'inline', 'attachment'])], 3: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo3@x', 'disp': 'attachment'}], ['logo3@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes3.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a3@x'}, {'type': 'image/png', 'cid': 'ba3@x'}], ['a3@x']), ['body', 'inline', 'attachment'])], 4: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo4@x', 'disp': 'attachment'}], ['logo4@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes4.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a4@x'}, {'type': 'image/png', 'cid': 'ba4@x'}], ['a4@x']), ['body', 'inline', 'attachment'])], 5: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo5@x', 'disp': 'attachment'}], ['logo5@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes5.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a5@x'}, {'type': 'image/png', 'cid': 'ba5@x'}], ['a5@x']), ['body', 'inline', 'attachment'])]}
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:
Inline PDFs or audio are placed in the body where the viewer cannot render them.

parts are the leaves of a multipart/mixed message in order: {type, disp, cid, filename}. Rules in order: an explicit "attachment" disposition (case-insensitive) is an attachment even when referenced; a part whose cid is exactly one of html_cids is inline; the first text/* part without a (non-empty) filename is the body; an explicit inline image/* is inline; everything else is an attachment.

split: open-access


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

N = 1
observations = []
def solve(parts, html_cids):
    out = []
    have_body = False
    for p in parts:
        disp = (p.get('disp') or '').lower()
        if disp == 'attachment':
            kind = 'attachment'
        elif p.get('cid') and True:
            kind = 'inline'
        elif p['type'].startswith('text/') and not p.get('filename') and not have_body:
            kind = 'body'
            have_body = True
        elif disp == 'inline' and p['type'].startswith('image/'):
            kind = 'inline'
        else:
            kind = 'attachment'
        out.append(kind)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo1@x', 'disp': 'attachment'}], ['logo1@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes1.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a1@x'}, {'type': 'image/png', 'cid': 'ba1@x'}], ['a1@x']), ['body', 'inline', 'attachment'])], 2: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo2@x', 'disp': 'attachment'}], ['logo2@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes2.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a2@x'}, {'type': 'image/png', 'cid': 'ba2@x'}], ['a2@x']), ['body', 'inline', 'attachment'])], 3: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo3@x', 'disp': 'attachment'}], ['logo3@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes3.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a3@x'}, {'type': 'image/png', 'cid': 'ba3@x'}], ['a3@x']), ['body', 'inline', 'attachment'])], 4: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo4@x', 'disp': 'attachment'}], ['logo4@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes4.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a4@x'}, {'type': 'image/png', 'cid': 'ba4@x'}], ['a4@x']), ['body', 'inline', 'attachment'])], 5: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo5@x', 'disp': 'attachment'}], ['logo5@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes5.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a5@x'}, {'type': 'image/png', 'cid': 'ba5@x'}], ['a5@x']), ['body', 'inline', 'attachment'])]}
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(parts, html_cids):
    out = []
    have_body = False
    for p in parts:
        disp = (p.get('disp') or '').lower()
        if disp == 'attachment':
            kind = 'attachment'
        elif p.get('cid') and any(c in p['cid'] for c in html_cids):
            kind = 'inline'
        elif p['type'].startswith('text/') and not p.get('filename') and not have_body:
            kind = 'body'
            have_body = True
        elif disp == 'inline' and p['type'].startswith('image/'):
            kind = 'inline'
        else:
            kind = 'attachment'
        out.append(kind)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo1@x', 'disp': 'attachment'}], ['logo1@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes1.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a1@x'}, {'type': 'image/png', 'cid': 'ba1@x'}], ['a1@x']), ['body', 'inline', 'attachment'])], 2: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo2@x', 'disp': 'attachment'}], ['logo2@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes2.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a2@x'}, {'type': 'image/png', 'cid': 'ba2@x'}], ['a2@x']), ['body', 'inline', 'attachment'])], 3: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo3@x', 'disp': 'attachment'}], ['logo3@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes3.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a3@x'}, {'type': 'image/png', 'cid': 'ba3@x'}], ['a3@x']), ['body', 'inline', 'attachment'])], 4: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo4@x', 'disp': 'attachment'}], ['logo4@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes4.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a4@x'}, {'type': 'image/png', 'cid': 'ba4@x'}], ['a4@x']), ['body', 'inline', 'attachment'])], 5: [('referenced image marked attachment', ([{'type': 'text/plain'}, {'type': 'image/png', 'cid': 'logo5@x', 'disp': 'attachment'}], ['logo5@x']), ['body', 'attachment']), ('upper-case disposition', ([{'type': 'text/plain', 'disp': 'ATTACHMENT'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('second text part', ([{'type': 'text/plain'}, {'type': 'text/plain'}], []), ['body', 'attachment']), ('attachment precedes body', ([{'type': 'application/pdf', 'disp': 'attachment'}, {'type': 'text/plain'}, {'type': 'text/plain'}], []), ['attachment', 'body', 'attachment']), ('named text part', ([{'type': 'text/plain', 'filename': 'notes5.txt'}, {'type': 'text/plain'}], []), ['attachment', 'body']), ('empty filename', ([{'type': 'text/plain', 'filename': ''}], []), ['body']), ('inline non-images', ([{'type': 'text/html'}, {'type': 'application/pdf', 'disp': 'inline'}, {'type': 'audio/ogg', 'disp': 'inline'}, {'type': 'image/gif', 'disp': 'inline'}], []), ['body', 'attachment', 'attachment', 'inline']), ('cid substring', ([{'type': 'text/html'}, {'type': 'image/png', 'cid': 'a5@x'}, {'type': 'image/png', 'cid': 'ba5@x'}], ['a5@x']), ['body', 'inline', 'attachment'])]}
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:
Every part carrying a Content-ID is hidden inline even when the HTML never references it.

parts are the leaves of a multipart/mixed message in order: {type, disp, cid, filename}. Rules in order: an explicit "attachment" disposition (case-insensitive) is an attachment even when referenced; a part whose cid is exactly one of html_cids is inline; the first text/* part without a (non-empty) filename is the body; an explicit inline image/* is inline; everything else is an attachment.

split: open-access


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

N = 1
observations = []
def solve(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fname') or p.get('fstar') or p.get('ctname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('ctname') or p.get('fstar') or p.get('fname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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:
The ASCII fallback filename replaces the properly decoded extended filename.

Each part has optional fstar (already decoded filename*), fname (filename) and ctname (Content-Type name) and a type. Precedence is fstar, fname, ctname. Directory components are removed (both "/" and "\" separate). An empty name, "." or ".." becomes "attachment" plus an extension by type (.pdf, .png, .txt, else .bin). Names are de-duplicated case-insensitively: the k-th copy gets " (k)" before the last extension, or at the end when there is no extension or the name starts with its only dot.

split: open-access


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

N = 1
observations = []
def solve(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.replace('\\\\', '/').split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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 Windows path sent as the filename is kept whole, directory names included.

Each part has optional fstar (already decoded filename*), fname (filename) and ctname (Content-Type name) and a type. Precedence is fstar, fname, ctname. Directory components are removed (both "/" and "\" separate). An empty name, "." or ".." becomes "attachment" plus an extension by type (.pdf, .png, .txt, else .bin). Names are de-duplicated case-insensitively: the k-th copy gets " (k)" before the last extension, or at the end when there is no extension or the name starts with its only dot.

split: open-access


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

N = 1
observations = []
def solve(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('',):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('', '.'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.lower()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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 filename of ".." or "." is accepted and later resolves to a directory.

Each part has optional fstar (already decoded filename*), fname (filename) and ctname (Content-Type name) and a type. Precedence is fstar, fname, ctname. Directory components are removed (both "/" and "\" separate). An empty name, "." or ".." becomes "attachment" plus an extension by type (.pdf, .png, .txt, else .bin). Names are de-duplicated case-insensitively: the k-th copy gets " (k)" before the last extension, or at the end when there is no extension or the name starts with its only dot.

split: open-access


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

N = 1
observations = []
def solve(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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(parts):
    exts = {'application/pdf': '.pdf', 'image/png': '.png', 'text/plain': '.txt'}
    used = {}
    out = []
    for p in parts:
        raw = p.get('fstar') or p.get('fname') or p.get('ctname') or ''
        base = raw.replace('\\', '/').split('/')[-1]
        if base in ('', '.', '..'):
            base = 'attachment' + exts.get(p['type'], '.bin')
        key = base.strip()
        used[key] = used.get(key, 0) + 1
        if used[key] > 1:
            stem, dot, ext = base.rpartition('.')
            base = (stem + ' (%d).' % used[key] + ext) if dot and stem else base + ' (%d)' % used[key]
        out.append(base)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('extended name wins', ([{'fstar': 'résumé1.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé1.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report1.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report1.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report1.PDF', 'type': 'application/pdf'}, {'ctname': 'report1.pdf', 'type': 'application/pdf'}],), ['Report1.PDF', 'report1 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}, {'fname': 'x1.png', 'type': 'image/png'}],), ['x1.png', 'x1 (2).png', 'x1 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 2: [('extended name wins', ([{'fstar': 'résumé2.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé2.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report2.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report2.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report2.PDF', 'type': 'application/pdf'}, {'ctname': 'report2.pdf', 'type': 'application/pdf'}],), ['Report2.PDF', 'report2 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}, {'fname': 'x2.png', 'type': 'image/png'}],), ['x2.png', 'x2 (2).png', 'x2 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 3: [('extended name wins', ([{'fstar': 'résumé3.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé3.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report3.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report3.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report3.PDF', 'type': 'application/pdf'}, {'ctname': 'report3.pdf', 'type': 'application/pdf'}],), ['Report3.PDF', 'report3 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}, {'fname': 'x3.png', 'type': 'image/png'}],), ['x3.png', 'x3 (2).png', 'x3 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 4: [('extended name wins', ([{'fstar': 'résumé4.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé4.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report4.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report4.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report4.PDF', 'type': 'application/pdf'}, {'ctname': 'report4.pdf', 'type': 'application/pdf'}],), ['Report4.PDF', 'report4 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}, {'fname': 'x4.png', 'type': 'image/png'}],), ['x4.png', 'x4 (2).png', 'x4 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])], 5: [('extended name wins', ([{'fstar': 'résumé5.pdf', 'fname': 'resume.pdf', 'ctname': 'r.pdf', 'type': 'application/pdf'}],), ['résumé5.pdf']), ('windows path', ([{'fname': 'C:\\Users\\bo\\report5.docx', 'type': 'application/msword'}, {'fname': '../../etc/passwd', 'type': 'text/plain'}],), ['report5.docx', 'passwd']), ('dot names', ([{'fname': '..', 'type': 'image/png'}, {'fname': '.', 'type': 'text/plain'}],), ['attachment.png', 'attachment.txt']), ('case-insensitive duplicates', ([{'fname': 'Report5.PDF', 'type': 'application/pdf'}, {'ctname': 'report5.pdf', 'type': 'application/pdf'}],), ['Report5.PDF', 'report5 (2).pdf']), ('multi-dot duplicate', ([{'fname': 'archive.tar.gz', 'type': 'application/gzip'}, {'fname': 'archive.tar.gz', 'type': 'application/gzip'}],), ['archive.tar.gz', 'archive.tar (2).gz']), ('dotfile and short stem', ([{'fname': '.profile', 'type': 'text/plain'}, {'fname': '.profile', 'type': 'text/plain'}, {'fname': 'a.pdf', 'type': 'application/pdf'}, {'fname': 'a.pdf', 'type': 'application/pdf'}],), ['.profile', '.profile (2)', 'a.pdf', 'a (2).pdf']), ('third copy', ([{'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}, {'fname': 'x5.png', 'type': 'image/png'}],), ['x5.png', 'x5 (2).png', 'x5 (3).png']), ('no name at all', ([{'type': 'application/zip'}, {'type': 'application/pdf'}],), ['attachment.bin', 'attachment.pdf'])]}
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:
Report.PDF and report.pdf are both saved under names that collide on case-insensitive file systems.

Each part has optional fstar (already decoded filename*), fname (filename) and ctname (Content-Type name) and a type. Precedence is fstar, fname, ctname. Directory components are removed (both "/" and "\" separate). An empty name, "." or ".." becomes "attachment" plus an extension by type (.pdf, .png, .txt, else .bin). Names are de-duplicated case-insensitively: the k-th copy gets " (k)" before the last extension, or at the end when there is no extension or the name starts with its only dot.

split: open-access