7.5. The Accumulator Pattern¶
Recall that an algorithm is a set of specific steps that, when followed, solve a problem.
A pattern is similar to an algorithm, but it can be used to solve many different types of problems. Think of a pattern like tying a knot—you can use the same steps to tie your shoelaces, a drawstring, the handles of a plastic bag, the ribbon on a present, etc. The pattern is the act of tying, and it is part of a larger solution (e.g. cleaning up after your dog). Often, a pattern makes up one piece of an algorithm.
Even though two algorithms can solve very different problems, the same pattern might be found in each one.
Let’s take a look at your first coding pattern.
7.5.1. Keeping a Running Total¶
Assume you are working at a theater, and your job is to count how many people walk through the front door. To help you keep track of the total, your boss gives you a counting tool. Every time a person walks in, you push a button and CLICK, the total displayed on the tool increases by 1.
This is an example of the accumulator pattern. Every time a particular event occurs, the value of a running total gets updated.
In programming, the accumulator pattern occurs VERY often. The key pieces to this pattern include:
- A variable that starts at some basic value. For numbers, this value is
1. For the
strdata type, begin with the empty string,
- A loop.
- Inside the loop, the value of the variable steadily increases (or decreases) with each iteration.
Let’s look at some code to see how the accumulator pattern works.
Let’s write a program that adds up the numbers from 1 to
num, where the
num stores some integer.
If we did this with pencil and paper, finding the sum might look like this
num = 6:
(1 + 2) + 3 + 4 + 5 + 6 # Calculate 1 + 2 (3 + 3) + 4 + 5 + 6 # Calculate 3 + 3 (6 + 4) + 5 + 6 # Calculate 6 + 4 (10 + 5) + 6 # Calculate 10 + 5 (15 + 6) # Calculate 15 + 6 21 # Final result!
In each step, the running total is the first number, and it gets added to the next value in the series. As we move down the steps, we see the total increase from 1 to 3 to 6 etc.
For larger values of
num, solving by hand gets tedious really fast. Loops
to the rescue!
1 2 3 4 5 6 7
num = 6 total = 0 for integer in range(1, num+1): total += integer print(total)
- In line 2, we assign a value of
0to the variable
- Each time the loop repeats, the loop variable
integeris assigned a new, higher value (1 to 6).
- Each time line 5 runs, the current value of
integeris added to
- Once the loop ends,
totalcontains the sum of all the individual values.
Recall that with
range(start, stop), the loop variable takes each value
start up to but NOT including
stop. This is why line 4 uses
range(1, num+1). We want to include the value of
num as part of the
The loop carries out the same basic algorithm that we used to solve
1 + 2 + 3 + 4 + 5 + 6 by hand. When we do this on paper, we usually do not
write down a running total for simple steps like 1 + 2. With programming,
however, we must store this total in a variable.
total is called the accumulator, which is a fancy way of
saying that it gathers up all the individual integers one by one.
The key to using the accumulator pattern successfully is to define the accumulator variable before you start the loop. Once inside the loop, update the variable.
7.5.2. Decreasing Total¶
The accumulator pattern can also be used to reduce the size of a running total.
Run the program below several times using different values for
1 2 3 4 5 6 7 8
# The accumulator pattern can also decrease a running total! total = 1000 decrease_by = 25 for step in range(10): total -= decrease_by print(total)
Any of the operators
+=, -=, *=, /= can be used in the accumulator
pattern to update the variable.
Which operator you choose depends on the problem you need to solve.
7.5.3. Check Your Understanding¶
Use this code sample to answer the following questions:
1 2 3 4 5 6
total = 0 for step in range(5): total += 2 print(total)
What does the program print?
What will print if you put
total = 0 inside the for loop but before
total += 2?