There’s a Pattern Here!

There’s a pattern in these programs, a pattern that is common when processing data. We call this the Accumulator Pattern. In the first program above, we accumulated the values into the variable sum. In the last few programs, we accumulated a product into the variable product.

Here are the five steps in this pattern.

  1. Set the accumulator variable to its initial value. This is the value we want if there is no data to be processed.
  2. Get all the data to be processed.
  3. Step through all the data using a for loop so that the variable takes on each value in the data.
  4. Combine each piece of the data into the accumulator.
  5. Do something with the result.

Using the Accumulator Pattern

What is the sum of all the numbers between 1 and 100? We can answer that easily using our pattern.

The range function has one more version that we can use here. By providing three input numbers, we can specify the start value, the ending value (which is one more than the last value), and the step – how much to change between numbers.

Now let’s answer a slightly harder question: What is the sum of all the even numbers between 0 and 100? It’s easy with our pattern.

    csp-7-5-1: Why do we stop at 101 in the above program?
  • Because we started at 0
  • We would want to include 100.
  • Because we want to include 100
  • If we stop BEFORE 101, we include 100.
  • Because the computer only understands 1s and 0s
  • Internally, yes, but in Python, all decimal digits are allowed.
  • Because we're using a step of 2
  • That doesn't really matter.
    csp-7-5-2: Why do we START with zero?
  • Because if we started with 1, we would get all odd numbers
  • This gives us [0,2,4,6...98,100].
  • Because all lists start with zero
  • They don't have to start at 0.
  • Because we end with 101
  • That is true, but is not relevant here.

How do we know what’s really going on in this program? How do we know that number is taking on all of the even values from 0 to 100? One way we can tell is by using a CodeLens on a smaller problem from 0 to 20. We can step through the program line-by-line, or race to the end by clicking the Last button and then step backwards.

(Numbers_Sum_Step)

        csp-7-5-3: The following is the correct code for printing the sum of all the odd numbers from 1 to 100 using the accumulator pattern, but it is mixed up. Drag the blocks from the left and put them in the correct order on the right.  Remember that the statements in the body of a loop must be indented!  To indent a block drag it further right. Click the Check Me button to check your solution.

sum = 0 --- numbers = range(1,101,2) --- for number in numbers: --- sum = sum + number --- print(sum)
    csp-7-5-4: Change the program above (in ActiveCode 3: Numbers_Sum_Even) to add up all the ODD numbers including up to 99. You should run it to get 2500. What change did you make to the program?
  • Changed the range step from 2 to 3
  • That would give us [0,3,6,9,12...99].
  • Changed the range end from 101 to 100
  • That would give us the even numbers from 0 to 98.
  • Changed the range end from 101 to 99
  • That would give us the even numbers from 0 to 98.
  • Changed the range start from 0 to 1
  • That would give us [1,3,5,...99].
        csp-7-5-5: The following is the correct code for printing the sum of all the even numbers from 50 to 100 using the accumulator pattern, but it is mixed up and contains one extra block. Drag the required blocks from the left and put them in the correct order on the right.  Don't forget to indent blocks in the body of the loop.  Just drag the block further right to indent.  Click the Check Me button to check your solution.

sum = 0 --- numbers = range(50,101,2) --- for number in numbers: --- sum = sum + number --- print(sum) --- numbers = range(50,100,2) #distractor

Note

Discuss topics in this section with classmates.

Show Comments
Next Section - Adding Print Statements