avp26/u4/word_count.py
2026-10-06 13:47:25 +02:00

11 lines
321 B
Python

def count_words(sentence):
counts = {}
start = 0
for i, ch in enumerate(sentence + " "):
if not (ch.isalnum() or ch == "'"):
word = sentence[start:i].lower().strip("'")
start = i + 1
if word:
counts[word] = counts.get(word, 0) + 1
return counts