The Frequency Count Program

We would like to create a program that counts the frequency of numbers that have been entered by the user.

For example, the program would run as follows.

Please enter a whole number:
3
Please enter a whole number:
2
Please enter a whole number:
2
Please enter a whole number:
6
Please enter a whole number:
63
Please enter a whole number:
q
The numbers seen were [3, 2, 2, 6, 63]
The count for 2 is: 2
The count for 3 is: 1
The count for 6 is: 1
The count for 63 is: 1

1. The Simple Problem

Let's start with a simpler problem.

We will create a program that asks the user for numbers and displays the count seen of each numbers 1 - 5 and the count for everything else.

For example, the program would run as follows.

Please enter a whole number:
3
Please enter a whole number:
2
Please enter a whole number:
2
Please enter a whole number:
6
Please enter a whole number:
63
Please enter a whole number:
q
The numbers seen were [3, 2, 2, 6, 63]
The count for 1 is: 0
The count for 2 is: 2
The count for 3 is: 1
The count for 4 is: 0
The count for 5 is: 0
The count for everything else is: 2

1.1. Solving Using a List

To answer this problem we can use a list.

number_list = []  # define a list to store numbers seen
count_list = [0, 0, 0, 0, 0, 0]  # defint a list to count the frequency of numbers
input_string = input("Please enter a whole number:\n")

while input_string != "q":
    number_list.append(int(input_string))  # add number seen to list
    # check if number between 1 and 5
    if int(input_string) > 0 and int(input_string) < 6:
        count_list[int(input_string) - 1] += 1  # increment number count by 1
    else:
        count_list[5] += 1  # increment everything else by 1
    input_string = input("Please enter a whole number:\n")

# print out the count of the numbers 1 to 5
print(f"The numbers seen were {number_list}")
for i in range(5):
    print(f"The count for {i+1} is: {count_list[i]}")

# print out the count of everything else
print(f"The count of everything else is: {count_list[5]}")

2. The Full Problem.

The issue with using a list to solve the full problem is that we don't know how many different numbers will be entered. In fact even worse, there are an infinite choice of numbers.

If you use a list you need to know how big it should be and therefore how many different types of numbers you expect to see.

This was fine when we said the numbers 1 - 5 and everything else. This is just counting 6 things. So we know the list should be of length 6.

2.1 Using a Dictionary

We can now use a dictionary to solve the full problem.

If you start with an empty dictionary, then you just need to add a new entry whenever you see a new number, then you can increment this by 1 if you see the number again.

number_list = []
count_dict = {}
input_string = input("Please enter a whole number:\n")

while input_string != "q":
    number_list.append(int(input_string))
    # check if the key exists in the dictionary
    if int(input_string) in count_dict.keys():
        count_dict[int(input_string)] += 1  # update entry
    else:
        count_dict[int(input_string)] = 1  # add new entry
    input_string = input("Please enter a whole number:\n")

# print out the count of the numbers
print(f"The numbers seen were {number_list}")
for key, value in sorted(count_dict.items()):
    print(f"The count for {key} is: {value}")