mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
fix: space count in strings/word_occurrence.py (#1896)
* fix: space count in strings/word_occurrence.py * Update strings/word_occurrence.py Co-Authored-By: Christian Clauss <cclauss@me.com> * Update strings/word_occurrence.py Co-Authored-By: Christian Clauss <cclauss@me.com> * Update strings/word_occurrence.py Co-Authored-By: Christian Clauss <cclauss@me.com> * Update word_occurrence.py Seems like, there is no need o `occurrence.pop('', None)` Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
24b2aecef3
commit
098be3594b
@ -1,4 +1,5 @@
|
||||
# Created by sarathkaul on 17/11/19
|
||||
# Modified by Arkadip Bhattacharya(@darkmatter18) on 20/04/2020
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
@ -10,10 +11,12 @@ def word_occurence(sentence: str) -> dict:
|
||||
>>> all(occurence_dict[word] == count for word, count
|
||||
... in Counter(SENTENCE.split()).items())
|
||||
True
|
||||
>>> dict(word_occurence("Two spaces"))
|
||||
{'Two': 1, 'spaces': 1}
|
||||
"""
|
||||
occurrence = defaultdict(int)
|
||||
# Creating a dictionary containing count of each word
|
||||
for word in sentence.split(" "):
|
||||
for word in sentence.split():
|
||||
occurrence[word] += 1
|
||||
return occurrence
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user