From 6b53a4cf661e2743488d6d811232e2bf19379230 Mon Sep 17 00:00:00 2001 From: Gabriel Simas Date: Fri, 31 Jan 2025 10:20:09 -0300 Subject: [PATCH 1/2] Make README.md (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Luís Gabriel Nascimento Simas --- README.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0dfcdde..bdda508 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ -# find-most-frequent-word-in-python-list -Write a program that takes a list of words and finds the most frequently occurring one. This project is designed for intermediate learners who know Python fundamentals and are practicing building complete programs. +# Find the Most Frequent Word in a Python List + +## Project Level 2: Intermediate + +This project is designed for intermediate learners who know Python fundamentals and are practicing building complete programs. + +## Project Description +Write a program that takes a list of words and finds the most frequently occurring one. Start by writing this list in the first line of your program: + +``` +words = ["love", "peace", "joy", "love", "happiness", "love", "joy"] +``` + +## Expected Output +Your program should find the most frequent word and print out a message similar to the following where the most frequent word (i.e., “love“) is is mentioned. + +## Learning Benefits +- **List Processing:** Work with lists containing text data. + +- **Using Collections (Optional):** Learn how the Counter class simplifies frequency counting. + +- **Efficient Lookups:** Extract the most common element in a single step. + +## Prerequisites +**Required Libraries:** No libraries are needed for this project. +**Required Files:** No files are needed for this project. +**IDE:** You can use any IDE on your computer to code the project. \ No newline at end of file From 43777b7aca5ec46cb831660261db3a6609c901cd Mon Sep 17 00:00:00 2001 From: Gabriel Simas Date: Fri, 31 Jan 2025 10:57:59 -0300 Subject: [PATCH 2/2] Release/1.0.0 (#4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make README.md * Feature/challenge solution (#3) * Solution given for challenge * Deleting uneeded code! --------- Co-authored-by: Luís Gabriel Nascimento Simas --------- Co-authored-by: Luís Gabriel Nascimento Simas --- src/main.py | 14 ++++++++++++++ src/models/frequent_word_finder.py | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/main.py create mode 100644 src/models/frequent_word_finder.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..4acf5c2 --- /dev/null +++ b/src/main.py @@ -0,0 +1,14 @@ +from models.frequent_word_finder import FrequentWordFinder + + +words = ["love", "peace", "joy", "love", "happiness", "love", "joy"] + +finder = FrequentWordFinder(words) + +finder.find_the_most_frequent_word() + +result = f""" +The most frequent word is {finder.most_frequent} +with {finder.word_count[finder.most_frequent]} occurrences.""" + +print(result) diff --git a/src/models/frequent_word_finder.py b/src/models/frequent_word_finder.py new file mode 100644 index 0000000..ce16d65 --- /dev/null +++ b/src/models/frequent_word_finder.py @@ -0,0 +1,21 @@ +class FrequentWordFinder: + def __init__(self, wordlist): + self._wordlist = wordlist + self._word_count = {} + self._most_frequent = "" + + @property + def most_frequent(self): + return self._most_frequent + + @property + def word_count(self): + return self._word_count + + def _count_ocurrences(self): + for word in self._wordlist: + self._word_count[word] = self._word_count.get(word, 0) + 1 + + def find_the_most_frequent_word(self): + self._count_ocurrences() + self._most_frequent = max(self._word_count, key=self._word_count.get)