INNER CODE UNIT · Python
mint
moderndive/ModernDive_book · scripts/assign_exercise_hashes.py:26
def mint(ch, prompt):
base = f"{ch}:{prompt.strip()}"
h = hash4(base); salt = 0
while h in existing: # resolve rare collisions deterministically
salt += 1; h = hash4(f"{base}#{salt}")
existing.add(h)
return h
# 2. insert ` hash: XXXX` after each `- ex_num: N` that lacks one
total_new = 0
for f in files:
ch = int(re.search(r"(\d\d)\.yml$", f).group(1))
doc = yaml.safe_load(open(f))
by_num = {e["ex_num"]: e for e in doc.get("exercises", [])}
lines = open(f).read().split("\n")
out, i, n_new = [], 0, 0
while i < len(lines):
out.append(lines[i])