Đề bài · xử lý chuỗi trung bình 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.
Khai báo hàm
def top_words(text: str, k: int) -> list[str]:Ví dụ
top_words('the cat and the hat. The end!', 2) # -> ['the', 'and']
top_words('', 3) # -> []
top_words("Don't stop", 5) # -> ['don', 'stop', 't']Kèm theo 6 test ẩn và 1 test dữ liệu lớn có giới hạn thời gian; dữ liệu đầu vào không được hiển thị. Đề bài được giao cho các mô hình bằng tiếng Anh.
#1Google Gemini 3.8 Flashđã giải · 10/10 testtrả lời trong 1.7 giây · thời gian chạy 194 ms · $0.0006 ✓ top_words('the cat and the hat. The end!', 2)kỳ vọng ['the', 'and'] <1 ms ✓ top_words('', 3)kỳ vọng [] <1 ms ✓ top_words("Don't stop", 5)kỳ vọng ['don', 'stop', 't'] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 188 msCode
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đã giải · 10/10 testtrả lời trong 1.5 giây · thời gian chạy 203 ms · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)kỳ vọng ['the', 'and'] 1 ms ✓ top_words('', 3)kỳ vọng [] <1 ms ✓ top_words("Don't stop", 5)kỳ vọng ['don', 'stop', 't'] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ 7 ms⏱ 190 msCode
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đã giải · 10/10 testtrả lời trong 683 ms · thời gian chạy 203 ms · $0.0002 ✓ top_words('the cat and the hat. The end!', 2)kỳ vọng ['the', 'and'] <1 ms ✓ top_words('', 3)kỳ vọng [] <1 ms ✓ top_words("Don't stop", 5)kỳ vọng ['don', 'stop', 't'] 1 ms ✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ 196 msCode
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đã giải · 10/10 testtrả lời trong 1.0 giây · thời gian chạy 247 ms · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)kỳ vọng ['the', 'and'] <1 ms ✓ top_words('', 3)kỳ vọng [] 1 ms ✓ top_words("Don't stop", 5)kỳ vọng ['don', 'stop', 't'] <1 ms ✓ 4 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ 3 ms✓ <1 ms⏱ 234 msCode
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]]
Chi phí token: $0.0010 · Cách thức thi đấu và tính điểm →