What happens when two keys hash to the same slot, and why can't a list ever be a dictionary key?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The given letter_hash(key) is the video's hash: add up the letter positions and take the sum mod 16. A table is a list of 16 buckets, each bucket a list of (key, value) tuples. Define store(table, key, value) that puts the pair at the end of the bucket its key hashes to, so colliding keys simply share that bucket (assume the key is not stored yet).
def letter_hash(key, size=16):
"""Sum of letter positions (a=1, ..., z=26), mod size."""
total = 0
for ch in key:
total += ord(ch.lower()) - ord('a') + 1
return total % size