DEV Community

Cover image for Mastering Word Frequencies: A Python Solution to the Coding Challenge
Raymundo Alva
Raymundo Alva

Posted on

Mastering Word Frequencies: A Python Solution to the Coding Challenge

Problem-solving skills are the key to mastering complex challenges. Let's dig into a fascinating coding problem that involves reading a file, counting word frequencies, and generating a unique word frequency list. I'm going to explain this using the Python programming language. We will guide go over a comprehensive solution to this simple yet challenging problem. Mastering this problem will sharpen some extremely important programming skills.

The objective of this challenge is to develop a program that reads an input file containing a list of words separated by commas, determine the frequency of each word in the file and present the result, excluding any duplicates.


Example

To better understand the problem, let's consider the following example:

Suppose we have an input file named input1.csv with the following content:

hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy

Enter fullscreen mode Exit fullscreen mode

The expected output for this program would be:

hello 1
cat 2
man 2
hey 2
dog 2
boy 2
Hello 1
woman 1
Cat 1
Enter fullscreen mode Exit fullscreen mode

Solution

To reach a solution to this coding problem, we will make use of the powerful built-in csv module. The following code presents a straightforward solution that will lead us to the desired output:

import csv

user_input = input("Enter the name of the input file: ")
word_freq = {}

with open(user_input, 'r') as csvfile:
    input_reader = csv.reader(csvfile, delimiter=',')

    for line in input_reader:
        for word in line:
            if word not in word_freq:
                word_freq[word] = 1
            else:
                word_freq[word] += 1

for word, frequency in word_freq.items():
    print(f"{word} {frequency}")
Enter fullscreen mode Exit fullscreen mode

At the beginning of our solution we prompt the user to enter the name of the input file. Next, we create an empty dictionary called word_freq to store the word frequencies. We open the file using the csv.reader() method and read it line by line. Each line is then split into individual words using the comma delimiter.

On each iteration, we check if each word already exists in the word_freq dictionary. If the word is not present, we add it to the dictionary with an initial frequency of 1. If the word already exists, we increment its frequency by 1.

Finally, we loop through the word_freq dictionary using the items() method to access both the word and its frequency. We print each word and its frequency using string formatting.


Problem-solving is a programmer's main skill, finding a comprehensive solution to a common coding challenge like this one will sharpen this necessary skill. By leveraging Python's powerful csv module, we read an input file, counted word frequencies, and created a frequency dictionary. The step-by-step approach displayed in this solution arms programmers of all levels with valuable insights, upgrades their problem-solving skills and expands their programming knowledge. I hope you had fun solving this problem and experimenting with it as much as I did. Happy coding! 😎

Top comments (0)