Workspace/Coding labs
Loading progress

Apply a constrained atomic state patch

Advanced50 min

Implement apply_updates(state, operations) for plain top-level dictionary paths using add, replace, remove, and test. Return a new state or raise ValueError without mutating the input.

Your task

  1. Accept paths of the form /key where key is nonempty and contains neither / nor ~. This intentionally excludes full JSON Pointer syntax.
  2. add sets a key, including an existing key; replace and remove require that key to exist.
  3. test requires an existing key equal to value; any failure aborts the whole update.
  4. Reject unsupported operations and copy nested values so result mutations cannot alter input state.

Examples

EXAMPLE 1

Input{'status':'draft'} with replace /status='ready'

Output{'status':'ready'}

The original dictionary remains unchanged.
solution.pyPython 3.12