โจทย์ · สตริง ปานกลาง 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 · วิธีจัดการแข่งขันและการคิดคะแนน →