Counting and summing¶
It is a very common case that we are only interested in some of the data from a collection. Here we will practice how to count and, if necessary, sum numbers that are of interest to us, or that fulfill some condition.
The general form of a program (algorithm) by which we count the elements of a collection that meet a given condition looks like this:
num = 0 for x in collection: if (x meets the condition): num += 1 print(num)
x += a increases the value of the variable x by a. This is actually an abbreviated form of the statement
x = x + a, which assigns the value x + a to the variable x.
x -= a decreases the value of the variable x by a. This is an abbreviated form of the statement
x = x - a, which assigns the value x - a to the variable x.
In our example, the statement num += 1 increases the value of the variable br by 1.
Examples and tasks¶
Example - meeting:
The team leader has offered two options for the time of the meeting to be held tomorrow. Each team member wrote in a table which term would be more appropriate for him/her (1 for the first term, 2 for the second). This information was transferred to the first line of the following program.
Complete the program - script, so that given the data on voting of team members, it prints how many voted for the first and how many for the second term.
For example, we can count the number of team members who voted for the first term, and calculate the rest at the end.
Another way is to count the votes for both the first term and the second term.
or, assuming the data is “clean”, that is, there are no values other than 1 and 2:
In case the information is not known in advance but should be entered, we could write a program like this:
At the beginning of this program, we load the number of votes n, then use the for loop to repeat loading and counting one vote n times.
Task - written test:
Several people passed the traffic proficiency test, which is a prerequisite for taking the practical part of the exam. A test is considered passed if the number of incorrect answers is less than or equal to 3.
At the beginning of the script are given the test results of one group of candidates (number of incorrect answers for each person who took the test). Complete the script by listing how many candidates have passed the test.
Task - swimming pool
A visit to the pool is being prepared for a group of children. Anyone lower than 160 centimeters can only go into the smaller pool. The organizer is interested in how many children are below 160 centimeters in order to plan the groups.
Children’s heights are given at the beginning of the program. Complete the program to print the number of children lower than 160 centimeters.
Task - humidity
In a botanical garden, soil moisture is measured once a day for rare and sensitive species. Humidity is expressed in numbers from 0 to 1, and conditions for the development of plants are considered to be good when the humidity is between 0.3 and 0.7 (including boundaries).
Values of humidity (measured over a period of time) are given at the beginning of the script. Complete the script by printing the number of days when the humidity was not good.
In one big group of practical problems, we come to the result by gradually building (accumulating) it as we go through the data. For example, if we need the sum of some numbers, we can get to it in this general way:
total = 0 for num in collection: total += num print(total)
When we are computing the sum of all the elements of a collection, we get the same result by calling the sum function:
We will use gradual formation of results when we need only some elements from the collection, that is, those that fulfill the given condition. In this case, the algorithm for calculating the sum would generally look like this:
total = 0 for num in collection: if (num meets the condition): total += num print(total)
In order to obtain the mean of the data that fulfills a condition, it is necessary to count and add up such data, and then divide their sum by their number. In the general case it looks like this:
total = 0 counter = 0 for num in collection: if (num meets the condition): total += num counter += 1 print(total / counter)
Note that in Python, the sum and mean of the selected elements of the collection can be obtained in a shorter and more efficient way. We chose the above method because it looks almost the same as in other programming languages.
Examples and tasks¶
Example - Average IQ test result:
The results of an IQ test for a group of people are given. A score of -1 means that the person did not take the test. Complete the program by printing the mean obtained on the test.
We can write the program like this:
Task - on duty:
In Company X, all employees occasionally remain on-call. The norm for the previous period is 20 hours on-call. Every extra hour (over 20 hours) on-call is additionally paid. The number of on-call hours for each employee is given, and the director wants to know the total number of on-call hours over the norm.
Complete the program by computing and printing the total number of overtime hours on-call.
If you solve the task correctly, you should get a score of 25 for the data given, since \((21-20)+(23-20)+(34-20)+(25-20)+(22-20)=25\).
Task - average yield:
In one orchard after the third year, plum yield per tree is monitored. Trees with yields below 3 kilograms are considered damaged or diseased and will be taken out.
The yield of all the trees in the orchard is given. Complete the program by calculating and printing the average yield of healthy trees (with yields of 3 kilograms or more).
You should get a result of approximately 14.757 for the given data.