avp26/u4/word_count.py
2026-10-27 15:03:30 +01:00

22 lines
586 B
Python

from collections import defaultdict
def count_words(sentance: str) -> dict:
sent_list = list(sentance)
words = defaultdict(int)
for i, ch in enumerate(sent_list):
if(ch == "'"):
if((sent_list[max(i-1, 0)].isalnum()) and (sent_list[min(i+1, len(sent_list)-1)].isalnum())):
pass
else:
sent_list[i] = " "
elif(not ch.isalnum()):
sent_list[i] = " "
for w in ''.join(sent_list).lower().split(" "):
if not len(w):
continue
words[w] += 1
return dict(words)