EasyEditing SystemsPython 3
File Patch Applier
Apply sorted or unsorted text edits atomically while rejecting invalid and overlapping ranges.
30m3 sample tests6 hidden tests
Implement apply_edits(text, edits), a safe text patch helper for non-overlapping edits.
Requirements
- Each edit is
(start, end, replacement)using half-open indexes from the selected language's string type. Python uses code-point indexes; JavaStringuses UTF-16 code-unit indexes. - Validate all edits before changing the text.
- Raise
ValueErrorfor negative indexes,end < start, indexes past the text length, and overlapping edits. - For overlapping edits, the
ValueErrormessage must include the substring"overlap"(case-sensitive). - Adjacent edits are allowed because half-open ranges touching at an endpoint remain non-overlapping.
- Multiple empty inserts at the same index are allowed. Preserve their input order so earlier inserts appear left-to-right in the result.
- Edits may arrive unsorted.
- Apply all edits atomically and return the new text.
Example
python
1assert apply_edits("hello world", [(6, 11, "Cursor")]) == "hello Cursor"
2assert apply_edits("abcdef", [(0, 1, "A"), (5, 6, "F")]) == "AbcdeF"Constraints
- Use the selected language's native string indexes: Python code points or Java
StringUTF-16 code units. - Don't mutate the input.
- Don't silently skip invalid edits.
Editor