
Code
Side Quest: Names That Look the Same
The name Zoë can be stored in two ways:
- as one character
ë(code point U+00EB), or - as a plain
efollowed by a combining mark (U+0308) that draws the two dots on top of it.
On screen they look identical. To Python they are different strings:
a = "Zoë" # composed: 3 characters
b = "Zoë" # decomposed: 4 characters
print(a == b) # False
print(len(a), len(b)) # 3 4
Normalize first
unicodedata.normalize("NFC", text) rewrites text into its
composed form. Text that looks the same becomes the same string:
import unicodedata
unicodedata.normalize("NFC", b) == a # True
casefold() beats lower()
For case-insensitive matching, use casefold(). It's like lower(),
but stronger: "Straße".casefold() is "strasse", which matches
"STRASSE".casefold(). lower() leaves the ß alone.
Write the policy down
"Same name" is a choice, so write the rules down. The Guild's policy:
- normalize to NFC, then
casefold(); - ignore spaces at the ends, and treat a run of spaces as one;
- accents matter:
"Zoe"and"Zoë"are different people.
Assignment
name_key(name): apply the policy in this order: NFC normalize,casefold(), then tidy the spaces with" ".join(text.split()).same_name(a, b): returnTrueif both names have the same key.unique_names(names): return a list with one entry per key. Keep the first spelling seen, in the original order.
name_key(name): apply the policy in this order: NFC normalize,casefold(), then tidy the spaces with" ".join(text.split()).same_name(a, b): returnTrueif both names have the same key.unique_names(names): return a list with one entry per key. Keep the first spelling seen, in the original order.
main.py Python sleeping
Press Run to try your code, then Check to test it.