blob: 2d5fc3ab9c3d02f751320d9502b8c7e96f2330c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/usr/bin/env python3
from collections import Counter
import string
def most_frequent_word(paragraph, banned):
'''Given a paragraph and a banned word. Return the most frequent word that
is not banned
>>> most_frequent_word(
... "Joe hit a ball, the hit ball flew far after it was hit.", "hit")
'ball'
>>> most_frequent_word(
... "Perl and Raku belong to the same family. "
... "Perl is the most popular language in the weekly challenge.", "the")
'Perl'
'''
if not paragraph or not banned:
raise ValueError("Both strings must be non-empty.")
paragraph_no_punctuation = paragraph.translate(
str.maketrans("", "", string.punctuation))
paragraph_counter = Counter(paragraph_no_punctuation.split())
if banned in paragraph_counter:
del paragraph_counter[banned]
if not paragraph_counter:
raise ValueError("No words found in the paragraph after removing punctuation and banned word.")
return paragraph_counter.most_common()[0][0]
if __name__ == "__main__":
import doctest
doctest.testmod()
|