Workspace/Coding labs
Loading progress

Apply a bounded patch atomically in memory

Intermediate65 min

Apply edits to a virtual repository mapping. Each edit supplies a relative POSIX path, nonempty old text, and replacement new text. Only existing files are supported, with at most one edit per path. The old text must occur exactly once in the original file. Reject absolute paths, empty/dot/traversal segments, backslashes, stale context, and ambiguity. Validate all edits before returning a new mapping. Never mutate inputs or execute the edited code. This is an educational patch format, not a Git patch parser or a security sandbox.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputold now <= expiry; new now < expiry

OutputA copied mapping with exactly that replacement

Explicit context limits accidental matches.
EXAMPLE 2

InputOne valid edit then one stale edit

OutputValueError; original mapping unchanged

The patch has all-or-nothing in-memory semantics.
solution.pyPython 3.12