EasyEditing SystemsPython 3

File Patch Applier

Apply sorted or unsorted text edits atomically while rejecting invalid and overlapping ranges.

30m2 sample tests4 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 character offsets.
  • Validate all edits before changing the text.
  • Reject negative indexes, end < start, indexes past the text length, and overlapping edits.
  • Adjacent edits are allowed.
  • 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

  • Work with Python string indexes.
  • Don't mutate the input.
  • Don't silently skip invalid edits.

Editor