def top_words(text, k): """The k most frequent lowercase words; ties broken alphabetically. Words are runs of letters.""" import re from collections import Counter counts = Counter(re.findall(r"[a-z]+", text)) return [w for w, _ in counts.most_common(k)]