chore(Python): cleaning string before word count (#256)

pull/239/head^2
Atin Bainada 2021-04-24 17:43:41 +05:30 committed by GitHub
parent 3ef7e1239c
commit 0073fbd332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -1,10 +1,25 @@
import re
test_string = '"the" is the most used word in the English language'
def word_count(s: str) -> int: def word_count(s: str) -> int:
"""
>>> word_count(test_string)
10
"""
s = re.sub('[^A-Za-z0-9 ]+', '', s)
return len(s.lower().split()) return len(s.lower().split())
def unique_word_count(s: str) -> int: 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())) 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)) print(s, word_count(s), unique_word_count(s))