Capstone: Quarry's Hash Kernel
Definition
A hash kernel turns strings into numbers with a polynomial hash taken modulo a prime, then checks the result: how many strings collided, how many buckets were used, and how many collisions chance alone would produce.
In plain words
It is a small machine that gives every word a numbered locker, then counts how many words ended up sharing a locker. It also tells you whether that many shared lockers is normal bad luck or a sign you picked the wrong number of lockers.
In short: Every earlier lesson in this section built one piece of arithmetic. The hash kernel puts them to work together: it folds real strings into numbers, picks a modulus that won't cluster them, and tells you whether the collisions it's seeing are bad luck or a bad choice of modulus.
What you're building
hash_kernel_report(strings, base, mod) -> {
"hashes": [...],
"collisions": int,
"buckets_used": int,
"is_prime_mod": bool,
"expected_collisions": float,
}hash_kernel_report(["cat", "dog", "bird", "cat"], 131, 97)
# {"hashes": [3, 70, 51, 3], "collisions": 1, "buckets_used": 3,
# "is_prime_mod": True, "expected_collisions": 0.0619}hashes:poly_hash(s, base, mod)for every string, in order — lesson 2's running total, modded every step.collisions: how many pairs of strings landed on the same hash."cat"appears twice, so it collides with itself once — that's not the modulus's fault, it's the same string twice, and a real system would need to compare the actual strings, not just their hashes, before trusting a match.buckets_used: how many distinct hash values came out — the same "does capacity resist clustering" question from lesson 1, now measured instead of assumed.is_prime_mod: lesson 1'sis_prime, checking whethermoditself is a sound choice.expected_collisions: the birthday bound — lesson 3's combinatorics, turned into a sanity check: fornstrings landing uniformly at random acrossmodbuckets, about how many colliding pairs should you expect by chance alone?