avp26/u4/word_count.py
2026-10-06 11:49:09 +00:00

18 lines
455 B
Python

import string
def count_words(sentence: str) -> dict[str, int]:
sentence = sentence.lower()
for char in sentence:
if not char.isanum() and char not in ("'", " "):
sentence = sentence.replace(char, " ")
words = sentence.split()
result = {}
for word in words:
cleaned_word = word.strip("'")
if cleaned_word:
result[cleaned_word] = result.get(cleaned_word, 0) + 1
return result