aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/steven-wilson/python/ch-02.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-255/steven-wilson/python/ch-02.py')
-rw-r--r--challenge-255/steven-wilson/python/ch-02.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-255/steven-wilson/python/ch-02.py b/challenge-255/steven-wilson/python/ch-02.py
new file mode 100644
index 0000000000..2d5fc3ab9c
--- /dev/null
+++ b/challenge-255/steven-wilson/python/ch-02.py
@@ -0,0 +1,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()