Exercises
Practice checking bounded variation, replaying ordered calls, preserving sampled and shuffled records, saving run evidence, and comparing later runs.
These exercises combine generation, replay, sampling, aligned splitting, saved records, and comparison. State each rule before checking one result.
Q1. Check a bounded noise contract
Complete the property checks for noise that must have shape (3, 2) and stay
in the half-open interval [-0.2, 0.2).
Editable Python
Ready to run.
HintTranslate the contract directly
>= -0.2 for every lower comparison, and < 0.2 for every upper comparison.SolutionCheck properties rather than exact values
shape_valid = noise.shape == (3, 2) and range_valid = np.all(noise >= -0.2) and np.all(noise < 0.2).Q2. Predict whether two sequences replay
Two generators start from seed 21. The first draws one (3, 2) uniform
array. The second draws one scalar before drawing that array. Will the arrays
match exactly?
Select one choice, then check.
HintTrace generator state
SolutionCall order is part of replay
Q3. Avoid accidental reseeding
Which function design lets repeated calls continue one controlled sequence?
Select one choice, then check.
HintKeep ownership visible
SolutionPass the generator
rng, then call rng.uniform(...) inside the function.Q4. Apply one sampled index array
Complete the program so the selected IDs and readings stay aligned.
Editable Python
Ready to run.
HintReuse the selection
indexes value.SolutionPreserve record identity
selected_ids = record_ids[indexes] and selected_readings = readings[indexes].Q5. Read replacement from selected indexes
The sampled indexes are [1, 3, 1]. What does the repeated 1 prove about
this particular selection?
Select one choice, then check.
HintCompare with no replacement
1 twice.SolutionIdentify the rule from the result
Q6. Verify a paired split
Complete the disjointness and coverage checks for two groups of shuffled indexes.
Editable Python
Ready to run.
HintCheck overlap and the complete set
np.arange(6).SolutionVerify both split properties
disjoint = np.intersect1d(working, checking).size == 0, combined = np.concatenate([working, checking]), and coverage = np.array_equal(np.sort(combined), np.arange(6)).Q7. Make a run record JSON-compatible
Which conversion is needed before json.dumps can store a NumPy index array
and a NumPy floating-point mean?
Select one choice, then check.
HintUse ordinary Python forms
SolutionConvert at the serialization boundary
.tolist() for the index array and float(...) for the scalar mean.Q8. Compare replayed outputs correctly
Which comparison pair fits a replayed integer index array and a floating-point measurement array?
Select one choice, then check.
HintSeparate discrete and numerical evidence
SolutionUse each comparison for its job
np.array_equal(replayed_indexes, saved_indexes) and np.allclose(replayed_values, saved_values).Q9. Find the first failed replay position
The saved indexes are [4, 1, 5] and the replayed indexes are [4, 2, 5].
What is the first mismatching array index?
Compute it first, then check your number.
HintCheck in order
SolutionLocate the first divergence
1. Check configuration and call order before that selection.Q10. Distinguish replay from controlled comparison
Run B uses the same data, seed, and call order as Run A but deliberately changes
the noise upper bound from 0.2 to 0.3. What kind of run is B?
Select one choice, then check.
HintAsk whether agreement is expected
SolutionName the comparison honestly
noise_high as the changed setting.You can now add bounded variation, replay its ordered generator calls, sample and split aligned records, save the settings and evidence, and decide whether a later run is a reproduction or a controlled comparison. This completes the Python Core path and leaves a repeatable numerical experiment ready for Mathematics.