Bridge · From syntax to engineering · 35 MIN
Project · Validate an entire import before applying it
A failed batch should not leave half a result behind.
Validation and application are separate phases. First check every record; only if all records satisfy the contract do you aggregate them. Return the position of the first bad record so the caller can correct it. Python booleans are instances of int, so use type(value) is int when True must not count as the amount 1. This is an in-memory model of atomic behavior. A database import additionally needs an actual transaction and a strategy for concurrent writes.
Write a small contract first, then test how the implementation behaves at its boundaries.
Read the example
print(type(True) is int)
Check the expected output
False
Your challenge
Input is a list of arbitrary JSON values. Each valid row must be an object with a nonempty string category and an integer amount excluding booleans. Return {ok:false,index:first bad position} if any row is invalid; otherwise return {ok:true,totals:category totals}.
Solution cost: O(n). time · O(c) for c categories. space
Common trap
Changing a shared accumulator before validating later rows produces a partially applied batch.
Further reading: Python type checks