MediumParsingPython 3
Prompt Section Extractor
Parse explicit prompt boundary markers while detecting outside text, duplicates, and unterminated sections.
35m3 sample tests5 hidden tests
Parse named prompt sections from explicit boundary markers.
Requirements
- Define
extract_sections(text). - Sections start with a line like
<<<name>>>. - A section ends with a line exactly
<<<END>>>. - Return
{"sections": dict, "errors": list}. - Store section content without the boundary lines.
- Join interior content lines with
"\\n". The line that holds<<<END>>>is not part of the body (so a one-line body has no trailing newline). Blank lines beforeENDare preserved as empty lines in the joined body. - Duplicate section names should produce
duplicate:name. On duplicates, keep the first completed section body and appendduplicate:<name>while keeping the first stored value unchanged. - Text outside sections that's not blank should produce
outside_text(emit at most once for the whole parse). - Unterminated sections should produce
unterminated:nameand must not store a partial body insections.
Example
python
1text = "<<<system>>>\\nBe brief.\\n<<<END>>>\\n"
2assert extract_sections(text)["sections"] == {"system": "Be brief."}Constraints
- Use explicit state tracking.
- Preserve internal newlines inside sections.
- Keep errors deterministic.
Editor