diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-10 09:38:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-10 09:38:16 +0000 |
| commit | 54fdc460423fc3743f27c2ba73e65cb2a899c464 (patch) | |
| tree | 0da9d3de4f285dafb23bbcb0fbecf6c80e87f384 /challenge-255/lubos-kolouch/python/ch-2.py | |
| parent | 399287fcd1fa2800c9ab44b9a065116e9a55ec86 (diff) | |
| parent | 20ccf25c6944641c7db4b60fa15d520686cb7036 (diff) | |
| download | perlweeklychallenge-club-54fdc460423fc3743f27c2ba73e65cb2a899c464.tar.gz perlweeklychallenge-club-54fdc460423fc3743f27c2ba73e65cb2a899c464.tar.bz2 perlweeklychallenge-club-54fdc460423fc3743f27c2ba73e65cb2a899c464.zip | |
Merge pull request #9548 from LubosKolouch/master
feat(challenge-255/lubos-kolouch/perl,python,raku/): Challenge 255 LK Perl Python Raku
Diffstat (limited to 'challenge-255/lubos-kolouch/python/ch-2.py')
| -rw-r--r-- | challenge-255/lubos-kolouch/python/ch-2.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-255/lubos-kolouch/python/ch-2.py b/challenge-255/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2c2f1d6807 --- /dev/null +++ b/challenge-255/lubos-kolouch/python/ch-2.py @@ -0,0 +1,35 @@ +def most_frequent_word(p: str, w: str) -> str: + """ + Find the most frequent word in a paragraph excluding a specific word. + + :param p: Paragraph as a string. + :param w: Banned word. + :return: Most frequent word. + """ + import re + from collections import Counter + + # Clean and split the paragraph into words + words = re.findall(r"\w+", p.lower()) + + # Count the frequency of each word, excluding the banned word + word_count = Counter(words) + if w in word_count: + del word_count[w] + + # Find the most frequent word + most_frequent = word_count.most_common(1)[0][0] + + return most_frequent + + +# Test cases +print( + most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit") +) +print( + most_frequent_word( + "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + "the", + ) +) |
