総まとめ:小さなプログラム

単語を数える小さなプログラムを、Python と JavaScript で書きます。ここまでの記号をすべて使います。

レッスン 16 / 16
ホームポジション(F と J)に指を置いて、スペース を押すとスタートします
from collections import Counter def top_words(text, n=3): words = text.lower().split() counts = Counter(w.strip(".,!?") for w in words) return counts.most_common(n) const countWords = (text) => { const counts = {}; for (const w of text.toLowerCase().split(" ")) { counts[w] = (counts[w] ?? 0) + 1; } return Object.entries(counts).sort((a, b) => b[1] - a[1]); };