prize18-6 Exercise 1: Code Points Against Clusters =================================================== Predictions: every input is one thing to a reader, so every cluster count is 1: the thumbs-up (1 code point), the thumbs-up with a tone modifier (2), the person-with-tone joined to a laptop (4: person, tone, ZWJ, laptop), the flag (2 regional indicators), the plain heart (1) and the heart with variation selector 16 (2). ex1.py (needs the third-party regex module: pip install regex): import regex def count(s): return len(regex.findall(r"\X", s)) def report(s): cps = " ".join(f"U+{ord(c):04X}" for c in s) print(f"{len(s)} code points, {count(s)} clusters: {cps}") report("\U0001F44D") report("\U0001F44D\U0001F3FD") report("\U0001F469\U0001F3FB\u200D\U0001F4BB") report("\U0001F1E9\U0001F1EA") report("\u2764") report("\u2764\uFE0F") Real output (Python 3.14): 1 code points, 1 clusters: U+1F44D 2 code points, 1 clusters: U+1F44D U+1F3FD 4 code points, 1 clusters: U+1F469 U+1F3FB U+200D U+1F4BB 2 code points, 1 clusters: U+1F1E9 U+1F1EA 1 code points, 1 clusters: U+2764 2 code points, 1 clusters: U+2764 U+FE0F All six clusters equal 1, as predicted. The code point count ranges from 1 to 4, so len() would give a wrong answer to "how many characters does the user see?".