--Originally published at Programming
The first challenge posted by ken is the next one:
Using a file full of words, create a program that counts how many times each word is repeated in the file. We are going to do first some functions that’ll help us throughout this process. First we’ll create a function read_from_file which will basically open a file and read from it every word, storing them in a list. This function returns a list of words.
def read_from_file(file):
"""
:param file: The file words are going to be read from
:return: a list with the words inside the file
"""
lista = []
try:
f = open(file, 'r')
for line in f:
line = line.strip('\n') # Get rid of the \n at the end of each line
lista += line.split(" ") # get every word of the line and store it inside a list
f.close()
except IOError:
print("Cannot open " + file)
return lista
Now we are going to create a function that counts the frequencies of the words inside a list by storing it in a dictionary. The dictionary keys will be the words and the values will be the number of times each word appears in the list. This function returns a dictionary with frequencies.
def count_frequency(words):
"""
:param words: List of words
:return: A dictionary with frequencies of words
"""
frequency_words = {}
for word in words: # iterate over the list
# If the word is not already inside the dictionary's keys, create it
if word not in frequency_words.keys():
frequency_words[word] = 1
else:
frequency_words[word] += 1
return frequency_words
Now we have the dictionary and it’s time for us to test whether this program works or not. So create a text file with random words, repeat some of them on purpose so that you can check
the program works. For this example I’m creating a text file named words.txt that looks like this:Now let’s create some test cases so that we can check if this program is working properly:
Test Cases
- adult – 5 (The program should map the word adult to 5)
- dog – 2 (The program should map the word dog to 2)
- ship – 4 (The program should map the word ship to 4)
Now let’s run the program. Below the functions we just created we need to write these lines of code:
# Create a list of words
words = read_from_file("words.txt")
# Create a word frequencies based on the words
words_frequency = count_frequency(words)
print(words_frequency)
Now save and run your file. The output should be a dictionary (Remember that dictionaries are data structures and therefore the items inside don’t have any order) that looks like this:
{'sheet': 1, 'ship': 4, 'dog': 2, 'sheep': 1, 'father': 2,
'cat': 5, 'brother': 3, 'adult': 5, 'step': 2, 'mother': 2, 'airplane': 2}
Now look at your test cases and compare them to your output.