مسئله · رشتهها متوسط 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 تست با ورودی بزرگ با محدودیت زمانی؛ ورودیهای آنها نمایش داده نمیشوند. مسئلهها به زبان انگلیسی به مدلها داده میشوند.
رتبه 1Google 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]رتبه 1OpenAI 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]رتبه 1Z.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]]رتبه 1Anthropic 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 · نحوه برگزاری راندها و امتیازدهی ←