Aktualizovat u4/word_count.py

final
This commit is contained in:
Oleksandr Mateshchuk 2026-10-06 20:55:27 +00:00
parent a388452cc1
commit 1510578629

View File

@ -1,15 +1,14 @@
import re import re
text = input()
words = re.findall(r"[^\W_]+(?:'[^\W_]+)?", text.lower(), re.UNICODE)
counts = {}
for word in words: def count_words(text):
if word in counts: words = re.findall(r"[^\W_]+(?:'[^\W_]+)?", text.lower(), re.UNICODE)
counts[word] += 1 result = {}
for word in words:
if word in result:
result[word] += 1
else: else:
counts[word] = 1 result[word] = 1
for word, count in sorted(counts.items()):
print(f"{word}: {count}")
return result