diff --git a/algorithms/Python/strings/word_count.py b/algorithms/Python/strings/word_count.py index b4b32613..6119fd70 100644 --- a/algorithms/Python/strings/word_count.py +++ b/algorithms/Python/strings/word_count.py @@ -1,10 +1,25 @@ +import re + +test_string = '"the" is the most used word in the English language' + + def word_count(s: str) -> int: + """ + >>> word_count(test_string) + 10 + """ + s = re.sub('[^A-Za-z0-9 ]+', '', s) return len(s.lower().split()) def unique_word_count(s: str) -> int: + """ + >>> unique_word_count(test_string) + 8 + """ + s = re.sub('[^A-Za-z0-9 ]+', '', s) return len(set(s.lower().split())) - -for s in ("The Matrix", "To Be or Not to Be", "Kiss Kiss Bang Bang"): + +for s in ("The Matrix", "To Be or Not to Be", "Kiss Kiss Bang Bang", test_string): print(s, word_count(s), unique_word_count(s))