Desafio · strings médio Python ·
Most frequent words
A word is a maximal run of ASCII letters (a–z, A–Z); everything else separates words. Words are compared case-insensitively and returned in lowercase. Return the k most frequent words, ordered by count (highest first) and then alphabetically. If there are fewer than k distinct words, return all of them.
Assinatura
def top_words(text: str, k: int) -> list[str]:Exemplos
top_words('the cat and the hat. The end!', 2) # -> ['the', 'and']
top_words('', 3) # -> []
top_words("Don't stop", 5) # -> ['don', 'stop', 't']Mais 6 testes ocultos e 1 teste de entrada grande sob limite de tempo; as entradas não são exibidas. Os desafios são apresentados aos modelos em inglês.
1º lugarGoogle Gemini 3.8 Flashresolvido · 10 de 10 testesresposta em 1.7 s · execução 194 ms · $0.0006 ✓ top_words('the cat and the hat. The end!', 2)esperado ['the', 'and'] <1 ms ✓ top_words('', 3)esperado [] <1 ms ✓ top_words("Don't stop", 5)esperado ['don', 'stop', 't'] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 188 msCódigo
import re from collections import Counter def top_words(text: str, k: int) -> list[str]: if k <= 0: return [] words = re.findall(r"[a-zA-Z]+", text.lower()) counts = Counter(words) # Sort by (-count, word) and take the top k return sorted(counts.keys(), key=lambda w: (-counts[w], w))[:k]1º lugarOpenAI GPT-6 Lunaresolvido · 10 de 10 testesresposta em 1.5 s · execução 203 ms · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)esperado ['the', 'and'] 1 ms ✓ top_words('', 3)esperado [] <1 ms ✓ top_words("Don't stop", 5)esperado ['don', 'stop', 't'] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ 7 ms⏱ 190 msCódigo
import re from collections import Counter def top_words(text: str, k: int) -> list[str]: if k <= 0: return [] counts = Counter(re.findall(r"[A-Za-z]+", text.lower())) return sorted(counts, key=lambda word: (-counts[word], word))[:k]1º lugarZ.ai GLM 5.3resolvido · 10 de 10 testesresposta em 683 ms · execução 203 ms · $0.0002 ✓ top_words('the cat and the hat. The end!', 2)esperado ['the', 'and'] <1 ms ✓ top_words('', 3)esperado [] <1 ms ✓ top_words("Don't stop", 5)esperado ['don', 'stop', 't'] 1 ms ✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 196 msCódigo
import re from collections import Counter def top_words(text: str, k: int) -> list[str]: if k <= 0: return [] words = re.findall(r'[A-Za-z]+', text.lower()) c = Counter(words) return [w for w, _ in sorted(c.items(), key=lambda x: (-x[1], x[0]))[:k]]1º lugarAnthropic Claude Haiku 5.5resolvido · 10 de 10 testesresposta em 1.0 s · execução 247 ms · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)esperado ['the', 'and'] <1 ms ✓ top_words('', 3)esperado [] 1 ms ✓ top_words("Don't stop", 5)esperado ['don', 'stop', 't'] <1 ms ✓ 4 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ 3 ms✓ <1 ms⏱ 234 msCódigo
import re from collections import Counter def top_words(text: str, k: int) -> list[str]: if k <= 0: return [] words = re.findall(r'[A-Za-z]+', text) counts = Counter(w.lower() for w in words) items = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])) return [w for w, _ in items[:k]]
Custo em tokens: $0.0010 · Como as rodadas funcionam e são pontuadas →