py_anagram

Código comentado línea a línea y paso a paso:

Entregas: py_anagram.py con anagram(s1, s2). Rank 03 · nivel 4.

Qué pide (en palabras normales)

Subject (enunciado original)
Write a function that checks if two strings are anagrams.
They must contain exactly the same letters with the same quantity,
ignoring case and spaces.

Expected file: py_anagram.py
Function signature:
def anagram(s1: str, s2: str) -> bool:

Examples:
anagram("listen", "silent")
=> True
anagram("Triangle", "Integral")
=> True
anagram("Dormitory", "Dirty Room")
=> True
anagram("hello", "world")
=> False
anagram("", "")
=> True
anagram("abc", "abcc")
=> False

Solución

def anagram(s1, s2):
    return clean(s1) == clean(s2)

def clean(s):
    return sorted(s.replace(" ", "").lower())

Normalizo igual los dos lados y comparo las letras ordenadas. Ordenar sustituye a contar: si una letra sale tres veces en uno y dos en otro, las listas difieren.

Trampas