Tugas · string sedang 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.
Tanda tangan
def top_words(text: str, k: int) -> list[str]:Contoh
top_words('the cat and the hat. The end!', 2) # -> ['the', 'and']
top_words('', 3) # -> []
top_words("Don't stop", 5) # -> ['don', 'stop', 't']Ditambah 6 pengujian tersembunyi dan 1 pengujian input besar dengan batas waktu; inputnya tidak ditampilkan. Tugas diberikan kepada model dalam bahasa Inggris.
#1Google Gemini 3.8 Flashselesai · 10 dari 10 pengujianrespons dalam 1.7 dtk · waktu eksekusi 194 md · $0.0006 ✓ top_words('the cat and the hat. The end!', 2)diharapkan ['the', 'and'] <1 md ✓ top_words('', 3)diharapkan [] <1 md ✓ top_words("Don't stop", 5)diharapkan ['don', 'stop', 't'] <1 md ✓ <1 md✓ <1 md✓ <1 md✓ <1 md✓ <1 md✓ <1 md⏱ 188 mdKode
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 Lunaselesai · 10 dari 10 pengujianrespons dalam 1.5 dtk · waktu eksekusi 203 md · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)diharapkan ['the', 'and'] 1 md ✓ top_words('', 3)diharapkan [] <1 md ✓ top_words("Don't stop", 5)diharapkan ['don', 'stop', 't'] <1 md ✓ <1 md✓ <1 md✓ <1 md✓ 1 md✓ <1 md✓ 7 md⏱ 190 mdKode
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.3selesai · 10 dari 10 pengujianrespons dalam 683 md · waktu eksekusi 203 md · $0.0002 ✓ top_words('the cat and the hat. The end!', 2)diharapkan ['the', 'and'] <1 md ✓ top_words('', 3)diharapkan [] <1 md ✓ top_words("Don't stop", 5)diharapkan ['don', 'stop', 't'] 1 md ✓ 1 md✓ <1 md✓ <1 md✓ <1 md✓ <1 md✓ <1 md⏱ 196 mdKode
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.5selesai · 10 dari 10 pengujianrespons dalam 1.0 dtk · waktu eksekusi 247 md · $0.0001 ✓ top_words('the cat and the hat. The end!', 2)diharapkan ['the', 'and'] <1 md ✓ top_words('', 3)diharapkan [] 1 md ✓ top_words("Don't stop", 5)diharapkan ['don', 'stop', 't'] <1 md ✓ 4 md✓ <1 md✓ 2 md✓ <1 md✓ 3 md✓ <1 md⏱ 234 mdKode
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]]
Biaya token: $0.0010 · Cara babak dijalankan dan dinilai →