Exercise 1: Counting and totalling: one traversal, two accumulators
The program below traverses an array once. Each pass adds the current element to total and, when the element is at least 10, adds 1 to count. Trace it with one row per pass, then answer.
a = [14, 9, 23, 6, 18, 8, 13] # line 1
count = 0 # line 2
total = 0 # line 3
for i in range(len(a)): # line 4
total = total + a[i] # line 5
if a[i] >= 10: # line 6
count = count + 1 # line 7
print(count, total) # line 8- a) What do total and count hold after the third pass, i = 2?
- b) What does line 8 print?
- c) A line 9 must print the average of the seven values. Which line is right? What does it print?
- d) Line 1 becomes a = [], an array with no element. What does line 8 print? What does the line 9 chosen in c) do?
- e) Write the lines, placed after line 9, that count the values strictly greater than the average. What is that count for the array of line 1?
Show the solution
Answers
- a) total = 46 and count = 2
- b) 4 91
- c) print(total / len(a)), which prints 13.0
- d) 0 0, then line 9 stops the program with a ZeroDivisionError
- e) 3 values (14, 23 and 18)
A count and a total follow one pattern: a variable set before the loop, changed by each pass, read after the loop. The trace table below follows lines 4 to 7; the program above is the complete answer, with the guard of part d) and the count of part e).
Pass 1, i = 0: total = 0 + 14 = 14, and 14 >= 10 is True, so count = 1.
Pass 2, i = 1: total = 14 + 9 = 23; 9 >= 10 is False, count stays 1.
Pass 3, i = 2: total = 23 + 23 = 46; 23 >= 10 is True, count = 2.
After the third pass, total = 46 and count = 2.
The last four passes add 6, 18, 8 and 13: total = 46 + 45 = 91.
Of these four values, 18 and 13 pass the test, so count = 2 + 2 = 4.
Line 8 prints 4 91. The value 13 is counted: >= includes the bound 10 itself, and so would 10 be.
Check: sum(a) also gives 91. The expected answer is the loop, which is what a unit test asks you to write.
The average is the total divided by the number of values: print(total / len(a)) prints 91 / 7 = 13.0. In Python, / always gives a float, hence 13.0 and not 13.
Pitfall: total / count divides the total of all seven values by the number of values at least 10: 91 / 4 = 22.75 is neither the average of the array nor the average of its four large values, (14 + 23 + 18 + 13) / 4 = 17.0.
With a = [], range(len(a)) is range(0): the loop makes no pass, count and total keep their starting values, and line 8 prints 0 0.
Line 9 then computes 0 / 0, and Python stops the program with ZeroDivisionError: division by zero.
Pitfall: an empty array has no average. The guard goes before the division: if len(a) > 0:, with a message such as No values otherwise.
The average must be known before the test, so the count is a second traversal, after the first one.
average = 91 / 7 = 13.0; the values strictly greater are 14, 23 and 18: 3 values.
13 equals the average and is not counted. With >= the count would be 4, so the comparison follows the words of the question: strictly greater means >.
a = [14, 9, 23, 6, 18, 8, 13]
count = 0
total = 0
for i in range(len(a)):
total = total + a[i]
if a[i] >= 10:
count = count + 1
print(count, total)
if len(a) > 0:
average = total / len(a)
print(average)
above = 0
for i in range(len(a)):
if a[i] > average:
above = above + 1
print(above)
else:
print('No values')