From c2bab639f415313beeedd77632a9241ef550d8b3 Mon Sep 17 00:00:00 2001 From: Arina Dohadailo Date: Tue, 6 Oct 2026 12:19:50 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20u4/word=5Fcount.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- u4/word_count.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/u4/word_count.py b/u4/word_count.py index b97615c..1125029 100644 --- a/u4/word_count.py +++ b/u4/word_count.py @@ -1,8 +1,13 @@ +import string +from collections import Counter def count_words(sentence): - cleaned = "".join(c if c.isalnum() or c == "'" else " " for c in sentence.lower()) - counts = {} - for word in cleaned.split(): + text = sentence.lower() +for mark in string.punctuation.replace("'", ""): + text = text.replace(mark, " ") +words = [] + for word in text.split(): word = word.strip("'") if word: - counts[word] = counts.get(word, 0) + 1 - return counts \ No newline at end of file + words.append(word) + + return dict(Counter(words)) \ No newline at end of file