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 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)