المهمة · سلاسل نصية متوسط 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 اختبارات مخفية واختبار مدخلات ضخمة واحد ضمن حد زمني؛ لا تُعرض مدخلاتها. تُقدَّم المهام للنماذج باللغة الإنجليزية.
المركز 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 · كيف تُدار الجولات وتُحتسب النقاط ←