Prototype safely reads one index, returning nullopt outside [0,size). Final applies Change{index,delta} entries in order to a candidate vector and calls after_change(index) after each candidate write. Invalid index or Int overflow throws invalid_argument; callback exceptions propagate unchanged. All failures leave values exactly unchanged. Successful calls commit all changes together. Callback is supplied and callable, must not mutate values/changes or retain candidate references; arithmetic is checked before addition. Empty batch succeeds with no callbacks.
C++ contract conventions: numeric values use signed Int (long long); all inputs and intermediate arithmetic are within its representable range. Opt<T> means std::optional<T> and absence is std::nullopt; Vec<T> means std::vector<T>. Invalid documented arguments throw std::invalid_argument. Inputs passed by const reference remain caller owned. Returned containers and records own their contents. No runtime type coercion is required; C++ static types exclude many dynamic-language type errors. Unless a lesson states otherwise, string ordering is lexicographic over Unicode code points (Text) or UTF-8 identifiers (std::string).
struct Change {Int index,delta;};
inline Opt<Int> prototype(const Vec<Int>& values,Int index);
inline void solve(Vec<Int>& values,const Vec<Change>& changes,const std::function<void(Int)>& after_change);Compile and run from this directory:
c++ -std=c++20 -O2 -Wall -Wextra -pedantic -pthread main.cpp -o practice
./practice --stage prototype
./practice --stage finalEdit exercise.hpp. Both stages start unfinished and must report failures until implemented. Keep main.cpp as the executable contract. The runner returns nonzero for a failed check, an unexpected exception, an invalid stage, or an empty suite. C++20 and only the standard library are required.
Complete the starter code, then run the checks.
Results and errors will appear here.
C++ runtime powered by Wasmer.