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

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix + ['1'])
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix[:-1])
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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:
Parts of an attached multipart message get an extra ".1" level, so fetching them returns the wrong entity.

Returns [section, type] for every non-multipart entity in depth-first order. A single-part message body is section "1". Children of a multipart are numbered 1..n under the parent's number (nested multiparts get their own number). A message/rfc822 part is listed itself; if its encapsulated body is multipart, that body's children are numbered directly under the message part (P.1, P.2...); a single-part encapsulated body is P.1.

split: open-access


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

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix)
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['0'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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 body of an attached single-part message gets the same number as the message part itself.

Returns [section, type] for every non-multipart entity in depth-first order. A single-part message body is section "1". Children of a multipart are numbered 1..n under the parent's number (nested multiparts get their own number). A message/rfc822 part is listed itself; if its encapsulated body is multipart, that body's children are numbered directly under the message part (P.1, P.2...); a single-part encapsulated body is P.1.

split: open-access


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

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix), t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '0', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
for _label, _args, _expected in _CASES[N]:
    check(_label, solve(*_args), _expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A message that is not multipart reports an empty section for its only body part.

Returns [section, type] for every non-multipart entity in depth-first order. A single-part message body is section "1". Children of a multipart are numbered 1..n under the parent's number (nested multiparts get their own number). A message/rfc822 part is listed itself; if its encapsulated body is multipart, that body's children are numbered directly under the message part (P.1, P.2...); a single-part encapsulated body is P.1.

split: open-access


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

N = 1
observations = []
def solve(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix if child['type'].startswith('multipart/') else prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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(tree):
    out = []
    def walk(node, prefix):
        t = node['type']
        if t.startswith('multipart/'):
            for i, child in enumerate(node['parts'], 1):
                walk(child, prefix if child['type'] == 'multipart/alternative' else prefix + [str(i)])
        elif t == 'message/rfc822':
            out.append(['.'.join(prefix), t])
            inner = node['body']
            if inner['type'].startswith('multipart/'):
                walk(inner, prefix)
            else:
                walk(inner, prefix + ['1'])
        else:
            out.append(['.'.join(prefix) or '1', t])
    walk(tree, [])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('single part message', ({'type': 'text/plain'},), [['1', 'text/plain']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 2: [('single part message', ({'type': 'text/html'},), [['1', 'text/html']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 3: [('single part message', ({'type': 'application/pdf'},), [['1', 'application/pdf']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 4: [('single part message', ({'type': 'image/png'},), [['1', 'image/png']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])], 5: [('single part message', ({'type': 'text/calendar'},), [['1', 'text/calendar']]), ('flat mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}, {'type': 'application/octet-stream'}]},), [['1', 'text/plain'], ['2', 'application/octet-stream'], ['3', 'application/octet-stream'], ['4', 'application/octet-stream'], ['5', 'application/octet-stream'], ['6', 'application/octet-stream']]), ('alternative inside mixed', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}, {'type': 'application/pdf'}]},), [['1.1', 'text/plain'], ['1.2', 'text/html'], ['2', 'application/pdf']]), ('attached multipart message', ({'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'message/rfc822', 'body': {'type': 'multipart/mixed', 'parts': [{'type': 'text/plain'}, {'type': 'image/jpeg'}]}}]},), [['1', 'text/plain'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'image/jpeg']]), ('attached single part message', ({'type': 'multipart/mixed', 'parts': [{'type': 'message/rfc822', 'body': {'type': 'text/plain'}}]},), [['1', 'message/rfc822'], ['1.1', 'text/plain']]), ('related and forwarded alternative', ({'type': 'multipart/mixed', 'parts': [{'type': 'multipart/related', 'parts': [{'type': 'text/html'}, {'type': 'image/gif'}]}, {'type': 'message/rfc822', 'body': {'type': 'multipart/alternative', 'parts': [{'type': 'text/plain'}, {'type': 'text/html'}]}}]},), [['1.1', 'text/html'], ['1.2', 'image/gif'], ['2', 'message/rfc822'], ['2.1', 'text/plain'], ['2.2', 'text/html']])]}
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:
Nested multipart containers are flattened, so their children collide with their siblings' numbers.

Returns [section, type] for every non-multipart entity in depth-first order. A single-part message body is section "1". Children of a multipart are numbered 1..n under the parent's number (nested multiparts get their own number). A message/rfc822 part is listed itself; if its encapsulated body is multipart, that body's children are numbered directly under the message part (P.1, P.2...); a single-part encapsulated body is P.1.

split: open-access


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

N = 1
observations = []
def solve(parent_type, header):
    default = 'text/plain'
    mtype = default
    charset = None
    if header is not None:
        head, _, rest = header.partition(';')
        cand = head.strip().lower()
        major, slash, minor = cand.partition('/')
        if slash and major and minor and ' ' not in cand:
            mtype = cand
            for piece in rest.split(';'):
                k, eq, v = piece.partition('=')
                if eq and k.strip().lower() == 'charset':
                    charset = v.strip().strip('"').lower()
    if mtype.startswith('text/'):
        charset = charset or 'us-ascii'
    else:
        charset = None
    return [mtype, charset]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="UTF-8"'), ['text/html', 'utf-8']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-1'), ['text/plain', 'iso-8859-1']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 2: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Windows-1252"'), ['text/html', 'windows-1252']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-2'), ['text/plain', 'iso-8859-2']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 3: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="KOI8-R"'), ['text/html', 'koi8-r']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-3'), ['text/plain', 'iso-8859-3']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 4: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Shift_JIS"'), ['text/html', 'shift_jis']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-4'), ['text/plain', 'iso-8859-4']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 5: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="EUC-KR"'), ['text/html', 'euc-kr']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-5'), ['text/plain', 'iso-8859-5']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])]}
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(parent_type, header):
    default = 'message/rfc822' if parent_type.startswith('message/') else 'text/plain'
    mtype = default
    charset = None
    if header is not None:
        head, _, rest = header.partition(';')
        cand = head.strip().lower()
        major, slash, minor = cand.partition('/')
        if slash and major and minor and ' ' not in cand:
            mtype = cand
            for piece in rest.split(';'):
                k, eq, v = piece.partition('=')
                if eq and k.strip().lower() == 'charset':
                    charset = v.strip().strip('"').lower()
    if mtype.startswith('text/'):
        charset = charset or 'us-ascii'
    else:
        charset = None
    return [mtype, charset]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="UTF-8"'), ['text/html', 'utf-8']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-1'), ['text/plain', 'iso-8859-1']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 2: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Windows-1252"'), ['text/html', 'windows-1252']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-2'), ['text/plain', 'iso-8859-2']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 3: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="KOI8-R"'), ['text/html', 'koi8-r']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-3'), ['text/plain', 'iso-8859-3']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 4: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Shift_JIS"'), ['text/html', 'shift_jis']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-4'), ['text/plain', 'iso-8859-4']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 5: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="EUC-KR"'), ['text/html', 'euc-kr']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-5'), ['text/plain', 'iso-8859-5']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])]}
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:
Entries of a digest without their own Content-Type are shown as plain text instead of as messages.

header is the raw Content-Type value or None. The type is the lower-cased text before ";" with surrounding spaces removed; it is valid only with a non-empty major and minor separated by "/" and no inner space. Missing or invalid types default to message/rfc822 inside multipart/digest and text/plain everywhere else (parameters of an invalid header are ignored). text/* types get the charset parameter (quotes removed, lower-cased) or us-ascii; other types have charset None. Result [type, charset].

split: open-access


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

N = 1
observations = []
def solve(parent_type, header):
    default = 'message/rfc822' if parent_type == 'multipart/digest' else 'text/plain'
    mtype = default
    charset = None
    if header is not None:
        head, _, rest = header.partition(';')
        cand = head.strip().lower()
        major, slash, minor = cand.partition('/')
        if slash:
            mtype = cand
            for piece in rest.split(';'):
                k, eq, v = piece.partition('=')
                if eq and k.strip().lower() == 'charset':
                    charset = v.strip().strip('"').lower()
    if mtype.startswith('text/'):
        charset = charset or 'us-ascii'
    else:
        charset = None
    return [mtype, charset]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="UTF-8"'), ['text/html', 'utf-8']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-1'), ['text/plain', 'iso-8859-1']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 2: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Windows-1252"'), ['text/html', 'windows-1252']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-2'), ['text/plain', 'iso-8859-2']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 3: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="KOI8-R"'), ['text/html', 'koi8-r']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-3'), ['text/plain', 'iso-8859-3']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 4: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Shift_JIS"'), ['text/html', 'shift_jis']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-4'), ['text/plain', 'iso-8859-4']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 5: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="EUC-KR"'), ['text/html', 'euc-kr']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-5'), ['text/plain', 'iso-8859-5']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])]}
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(parent_type, header):
    default = 'message/rfc822' if parent_type == 'multipart/digest' else 'text/plain'
    mtype = default
    charset = None
    if header is not None:
        head, _, rest = header.partition(';')
        cand = head.strip().lower()
        major, slash, minor = cand.partition('/')
        if slash and minor:
            mtype = cand
            for piece in rest.split(';'):
                k, eq, v = piece.partition('=')
                if eq and k.strip().lower() == 'charset':
                    charset = v.strip().strip('"').lower()
    if mtype.startswith('text/'):
        charset = charset or 'us-ascii'
    else:
        charset = None
    return [mtype, charset]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
_CASES = {1: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="UTF-8"'), ['text/html', 'utf-8']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-1'), ['text/plain', 'iso-8859-1']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 2: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Windows-1252"'), ['text/html', 'windows-1252']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-2'), ['text/plain', 'iso-8859-2']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 3: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="KOI8-R"'), ['text/html', 'koi8-r']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-3'), ['text/plain', 'iso-8859-3']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 4: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="Shift_JIS"'), ['text/html', 'shift_jis']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-4'), ['text/plain', 'iso-8859-4']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])], 5: [('digest child without header', ('multipart/digest', None), ['message/rfc822', None]), ('mixed child without header', ('multipart/mixed', None), ['text/plain', 'us-ascii']), ('truncated type in digest', ('multipart/digest', 'text/'), ['message/rfc822', None]), ('missing major type', ('multipart/mixed', '/html; charset=utf-8'), ['text/plain', 'us-ascii']), ('quoted upper-case charset', ('multipart/mixed', 'Text/HTML; charset="EUC-KR"'), ['text/html', 'euc-kr']), ('charset on a binary type', ('multipart/mixed', 'image/png; charset=binary'), ['image/png', None]), ('padded header', ('multipart/alternative', ' text/plain ; charset=ISO-8859-5'), ['text/plain', 'iso-8859-5']), ('nested message parent', ('message/rfc822', None), ['text/plain', 'us-ascii'])]}
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 malformed type such as "text/" or "/html" is used verbatim instead of falling back to the default.

header is the raw Content-Type value or None. The type is the lower-cased text before ";" with surrounding spaces removed; it is valid only with a non-empty major and minor separated by "/" and no inner space. Missing or invalid types default to message/rfc822 inside multipart/digest and text/plain everywhere else (parameters of an invalid header are ignored). text/* types get the charset parameter (quotes removed, lower-cased) or us-ascii; other types have charset None. Result [type, charset].

split: open-access