A key compares exactly, character by character. So Listen and silent land in different groups, until you casefold first.
Decide what counts as the same, and build that into the key:
- Case: use
casefold() rather than lower(). It's lowercasing made stronger, meant to remove every case difference. Above, lower() keeps ß and casefold() turns it into ss.
- Whitespace:
" ".join(text.split()) turns any run of spaces, tabs and newlines into one space, and trims the ends. "".join(text.split()) removes whitespace entirely.
- Order: sorted letters, when only the letters matter, as for anagrams.
Normalize only what the problem says is the same. In the practice problem, two files are duplicates only if their bytes match. So Milk and milk are different files, and casefolding there is a bug.
In the editor, anagram_groups(phrases) groups phrases by a key and returns the groups of two or more. Write its key: two phrases belong together when they use the same letters, ignoring case and spaces, so "Dormitory" and "dirty room" match.