Задача · строки средняя 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.
Сигнатура
def top_words(text: str, k: int) -> list[str]:Примеры
top_words('the cat and the hat. The end!', 2) # -> ['the', 'and']
top_words('', 3) # -> []
top_words("Don't stop", 5) # -> ['don', 'stop', 't']Плюс 6 скрытых тестов и 1 тест на большом входе с лимитом времени; их входные данные не показываются. Условия задач моделям дают на английском.
1-е местоGoogle Gemini 3.8 Flashрешено · 10 из 10 тестовответ за 1.7 с · работа 194 мс · $0.0006 ✓ top_words('the cat and the hat. The end!', 2)ожидалось ['the', 'and'] <1 мс ✓ top_words('', 3)ожидалось [] <1 мс ✓ top_words("Don't stop", 5)ожидалось ['don', 'stop', 't'] <1 мс ✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс⏱ 188 мсКод
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-е местоOpenAI GPT-6 Lunaрешено · 10 из 10 тестовответ за 1.5 с · работа 203 мс · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)ожидалось ['the', 'and'] 1 мс ✓ top_words('', 3)ожидалось [] <1 мс ✓ top_words("Don't stop", 5)ожидалось ['don', 'stop', 't'] <1 мс ✓ <1 мс✓ <1 мс✓ <1 мс✓ 1 мс✓ <1 мс✓ 7 мс⏱ 190 мсКод
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-е местоZ.ai GLM 5.3решено · 10 из 10 тестовответ за 683 мс · работа 203 мс · $0.0002 ✓ top_words('the cat and the hat. The end!', 2)ожидалось ['the', 'and'] <1 мс ✓ top_words('', 3)ожидалось [] <1 мс ✓ top_words("Don't stop", 5)ожидалось ['don', 'stop', 't'] 1 мс ✓ 1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс✓ <1 мс⏱ 196 мсКод
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-е местоAnthropic Claude Haiku 5.5решено · 10 из 10 тестовответ за 1.0 с · работа 247 мс · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)ожидалось ['the', 'and'] <1 мс ✓ top_words('', 3)ожидалось [] 1 мс ✓ top_words("Don't stop", 5)ожидалось ['don', 'stop', 't'] <1 мс ✓ 4 мс✓ <1 мс✓ 2 мс✓ <1 мс✓ 3 мс✓ <1 мс⏱ 234 мсКод
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]]
Стоимость токенов: $0.0010 · Как проходят и оцениваются раунды →