From fa0f04c830c6fb5b332d36ea1c6bc31b77c65886 Mon Sep 17 00:00:00 2001 From: Sarath Kaul Date: Sun, 17 Nov 2019 14:08:36 +0530 Subject: [PATCH 1/3] Word Occurence Script Added --- strings/word_occurence.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 strings/word_occurence.py diff --git a/strings/word_occurence.py b/strings/word_occurence.py new file mode 100644 index 000000000000..1c351deb1707 --- /dev/null +++ b/strings/word_occurence.py @@ -0,0 +1,20 @@ +# Created by sarathkaul on 17/11/19 + + +from collections import defaultdict + + +def word_occurence(sentence: str) -> dict: + occurence = defaultdict(int) + + sentence = sentence.split(" ") + # Creating a dictionary containing count of each word + for a_word in sentence: + occurence[a_word] += 1 + return occurence + + +if __name__ == "__main__": + count = word_occurence("INPUT STRING") + for word, count in count.items(): + print(f"{word}: {count}") From c1d41374d1e48eff894b3b428ae355595839370c Mon Sep 17 00:00:00 2001 From: Sarath Kaul Date: Sun, 17 Nov 2019 14:13:15 +0530 Subject: [PATCH 2/3] Word Occurence Script Updated --- strings/word_occurence.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/word_occurence.py b/strings/word_occurence.py index 1c351deb1707..68de54ac9075 100644 --- a/strings/word_occurence.py +++ b/strings/word_occurence.py @@ -15,6 +15,6 @@ def word_occurence(sentence: str) -> dict: if __name__ == "__main__": - count = word_occurence("INPUT STRING") - for word, count in count.items(): + count_dict = word_occurence("INPUT STRING") + for word, count in count_dict.items(): print(f"{word}: {count}") From 95e6937f1711b33b93be58bb16d8cf311ba0941c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 17 Nov 2019 12:52:39 +0100 Subject: [PATCH 3/3] Added doctest using collections.Counter https://docs.python.org/3/library/collections.html#collections.Counter --- strings/word_occurence.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/strings/word_occurence.py b/strings/word_occurence.py index 68de54ac9075..c4eb923d6bc8 100644 --- a/strings/word_occurence.py +++ b/strings/word_occurence.py @@ -1,20 +1,23 @@ # Created by sarathkaul on 17/11/19 - - 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 + """ occurence = defaultdict(int) - - sentence = sentence.split(" ") # Creating a dictionary containing count of each word - for a_word in sentence: - occurence[a_word] += 1 + for word in sentence.split(" "): + occurence[word] += 1 return occurence if __name__ == "__main__": - count_dict = word_occurence("INPUT STRING") - for word, count in count_dict.items(): + for word, count in word_occurence("INPUT STRING").items(): print(f"{word}: {count}")