mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 17:20:16 +00:00
20a4fdf384
* Fix mypy error for can_string_be_rearranged_as_pal * Fix mypy error for levenshtein_distance.py * Fix mypy error for word_patterns.py * Fix mypy error for word_occurrence.py
27 lines
849 B
Python
27 lines
849 B
Python
# Created by sarathkaul on 17/11/19
|
|
# Modified by Arkadip Bhattacharya(@darkmatter18) on 20/04/2020
|
|
from collections import defaultdict
|
|
|
|
|
|
def word_occurence(sentence: str) -> dict:
|
|
"""
|
|
>>> from collections import Counter
|
|
>>> SENTENCE = "a b A b c b d b d e f e g e h e i e j e 0"
|
|
>>> occurence_dict = word_occurence(SENTENCE)
|
|
>>> all(occurence_dict[word] == count for word, count
|
|
... in Counter(SENTENCE.split()).items())
|
|
True
|
|
>>> dict(word_occurence("Two spaces"))
|
|
{'Two': 1, 'spaces': 1}
|
|
"""
|
|
occurrence: dict = defaultdict(int)
|
|
# Creating a dictionary containing count of each word
|
|
for word in sentence.split():
|
|
occurrence[word] += 1
|
|
return occurrence
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for word, count in word_occurence("INPUT STRING").items():
|
|
print(f"{word}: {count}")
|