INNER CODE UNIT · Python
ALPH
moderndive/ModernDive_book · scripts/assign_exercise_hashes.py:9
ALPH = string.digits + string.ascii_lowercase # base36
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):