INNER CODE UNIT · Python
hash4
moderndive/ModernDive_book · scripts/assign_exercise_hashes.py:11
def hash4(text):
h = int(hashlib.sha1(text.encode()).hexdigest(), 16)
s = ""
for _ in range(4):
s += ALPH[h % 36]; h //= 36
return s
# 1. collect existing hashes (for global-uniqueness when minting new ones)
files = sorted(glob.glob("exercises/[0-9][0-9].yml"))
existing = set()
for f in files:
for e in yaml.safe_load(open(f)).get("exercises", []):
if e.get("hash"):
existing.add(e["hash"])
def mint(ch, prompt):
base = f"{ch}:{prompt.strip()}"
h = hash4(base); salt = 0