MediumEditing SystemsPython 3

Patch Conflict Detector

Apply optimistic multi-file patches atomically while rejecting stale or overlapping edits.

40m2 sample tests5 hidden tests

Implement apply_patches(files, patches), an atomic patch applier with optimistic conflict checks.

Requirements

  • files maps file path to text.
  • Each patch is (path, start, end, expected_old_text, replacement).
  • Validate all patches before mutating anything.
  • Reject invalid ranges, missing files, overlapping patches in the same file, and expected-text mismatches.
  • Apply patches from the end of each file so offsets stay stable.
  • Return a new dictionary and leave input untouched.

Example

python
1files = {"app.py": "print('old')\n"} 2patches = [("app.py", 6, 11, "'old'", "'new'")] 3assert apply_patches(files, patches)["app.py"] == "print('new')\n"

Constraints

  • No diff parser is required.
  • Treat offsets as Python string indexes.

Editor