任务 · 字符串 中等 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]]
Token 成本: $0.0010 · 轮次运行与评分规则 →