Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 14 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions src/models/frequent_word_finder.py
Original file line number Diff line number Diff line change
@@ -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)