9.12. 👩‍💻 Accumulator Pattern Strategies¶

9.12.1. When to Use it¶

When children first encounter word problems in their math classes, they find it difficult to translate those words into arithmetic expressions involving addition, subtraction, multiplication, and division. Teachers offer heuristics. If the problem says “how many…altogether”, that’s an addition problem. If it says “how many are left”, that’s going to be a subtraction problem.

Learning to use the accumulator pattern can be similarly confusing. The first step is to recognizing something in the problem statement that suggests an accumulation pattern. Here are a few. You might want to try adding some more of your own.

 Phrase Accumulation Pattern how many count accumulation how frequently total sum accumulation a list of list accumulation concatenate string accumulation join together

For example, if the problem is to compute the total distance traveled in a series of small trips, you would want to accumulate a sum. If the problem is to make a list of the cubes of all the numbers from 1-25, you want a list accumulation, starting with an empty list and appending one more cube each time. If the problem is to make a comma separated list of all the people invited to a party, you should think of concatenating them; you could start with an empty string and concatenate one more person on each iteration through a list of name.

9.12.2. Before Writing it¶

Before writing any code, we recommend that you first answer the following questions:

• What sequence will you iterate through as you accumulate a result? It could be a range of numbers, the letters in a string, or some existing list that you have just as a list of names.

• What type of value will you accumulate? If your final result will be a number, your accumulator will start out with a number and always have a number even as it is updated each time. Similarly, if your final result will be a list, start with a list. If your final result will be a string, you’ll probably want to start with a string; one other option is to accumulate a list of strings and then use the .join() method at the end to concatenate them all together.

We recommend writing your answers to these questions in a comment. As you encounter bugs and have to look things up, it will help remind you of what you were trying to implement. Sometimes, just writing the comment can help you to realize a potential problem and avoid it before you ever write any code.

9.12.3. Choosing Good Accumulator and Iterator Variable Names¶

The final piece of advice regarding accumulation strategies is to be intentional when choosing variable names for the accumulator and iterator variables. A good name can help remind you of what the value is assigned to the variable as well as what you should have by the end of your code. While it might be tempting at first to use a short variable name, such as a or x, if you run into any bugs or look at your code later, you may have trouble understanding what you intended to do and what your code is actually doing.

For the accumulator variable, one thing that can help is to make the variable name end with “so_far”. The prefix can be something that helps remind you of what you’re supposed to end up with. For example: count_so_far, total_so_far, or cubes_so_far.

As mentioned previously in a previous Way of the Programmer segment, 👩‍💻 Naming Variables in For Loops, the iterator variable should be a singular noun. It should describe what one item in the original sequence, not what one item in the final result will be. For example, when accumulating the cubes of the numbers from 1-25, don’t write for cube in range(25):. Instead, write for num in range(25):. If you name the iterator variable cube you run the risk of getting confused that it has already been cubed, when that’s an operation that you still have to write in your code.

Does the following prompt require an accumulation pattern? If so, what words indicate that? For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.

• Yes; "save... to a list"
• Correct!
• Yes; "add 'ed' to the end of the word"
• Not quite - these words don't necessarily mean that we want to accumulate the new strings into a new variable.
• No
• In this case, an accumulation pattern would be good to use!

Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.

• Yes; "to sum up"
• Correct!
• Yes; "numbers in the list"
• Not quite - these words don't necessarily mean that we want to do sum accumulation.
• No
• In this case, an accumulation pattern would be good to use!

Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code to print out each character of the string my_str on a separate line.

• Yes; "print out each"
• Incorrect, this prompt does not need to use the accumulation pattern.
• Yes; "on a separate line"
• Incorrect, this prompt does not need to use the accumulation pattern.
• No
• Correct!

Does the following prompt require an accumulation pattern? If so, what words indicate that? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.

• Yes; "vowels in the sentence"
• Not quite - these words don't necessarily mean that we want to do sum accumulation.
• Yes; "code that will count"
• Correct!
• No
• In this case, an accumulation pattern would be good to use!

What type should be used for the accumulator variable in the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.

• string
• Incorrect, that is not the best type for the accumulator variable.
• list
• Incorrect, that is not the best type for the accumulator variable.
• integer
• Yes, because we want to keep track of a number.
• none, there is no accumulator variable.
• Incorrect, we will need an accumulator variable.

What sequence will you iterate through as you accumulate a result in the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.

• num_vowels
• No, that is the accumulator variable.
• s
• Yes, that is the sequence you will iterate through!
• the prompt does not say
• It is stated in the prompt.

What type should be used for the accumulator variable in the following prompt? For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.

• string
• Incorrect, that is not the best type for the accumulator variable.
• list
• Yes, because we want a new list at the end of the code.
• integer
• Incorrect, that is not the best type for the accumulator variable.
• none, there is no accumulator variable.
• Incorrect, we will need an accumulator variable.

What sequence will you iterate through as you accumulate a result in the following prompt? For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.

• wrds
• Yes, that is the sequence you will iterate through!
• past_wrds
• No, that is the accumulator variable.
• the prompt does not say
• It is stated in the prompt.

What type should be used for the accumulator variable in the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.

• string
• Incorrect, that is not the best type for the accumulator variable.
• list
• Incorrect, that is not the best type for the accumulator variable.
• integer
• Yes, because we want to keep track of a number.
• none, there is no accumulator variable.
• Incorrect, we will need an accumulator variable.

What sequence will you iterate through as you accumulate a result in the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.

• seat_counts
• Yes, that is the sequence you will iterate through!
• total_seat_counts
• No, that is the accumulator variable.
• the prompt does not say
• It is stated in the prompt.

What type should be used for the accumulator variable in the following prompt? Write code to print out each character of the string my_str on a separate line.

• string
• Incorrect, there should not be an accumulator variable.
• list
• Incorrect, there should not be an accumulator variable.
• integer
• Incorrect, there should not be an accumulator variable.
• none, there is no accumulator variable.
• Correct, because this prompt does not require an accumulator pattern

What sequence will you iterate through as you accumulate a result in the following prompt? Write code to print out each character of the string my_str on a separate line.

• my_str
• Yes, that is the sequence you will iterate through!
• my_str.split()
• Close, but read the prompt again - did it say to iterate through words?
• the prompt does not say
• It is stated in the prompt.

Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.

• Accumulator Variable: wrds_so_far ; Iterator Variable: wrd
• Yes, this is the most clear combination of accumulator and iterator variables.
• Accumulator Variable: wrds_so_far ; Iterator Variable: x
• The iterator variable is not the clearest here, something else may be better.
• Accumulator Variable: changed_wrds ; Iterator Variable: ed
• The iterator variable is not the clearest here

Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels.

• Accumulator Variable: count_so_far ; Iterator Variable: l
• Though the accumulator variable is good, the iterator variable is not very clear.
• Accumulator Variable: total_so_far ; Iterator Variable: letter
• Yes! Both the accumulator and iterator variable are clear.
• Accumulator Variable: n_v ; Iterator Variable: letter
• Though the iterator variable is good, the accumulator variable is not very clear.

Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code to sum up all of the numbers in the list seat_counts. Store that number in the variable total_seat_counts.

• Accumulator Variable: total_so_far ; Iterator Variable: seat
• Though the accumulator variable is good, the iterator variable is not clear enough.
• Accumulator Variable: total_seats_so_far ; Iterator Variable: seat_count
• Yes, this is the most clear combination.
• Accumulator Variable: count ; Iterator Variable: n
• Neither the accumulator nor iterator variable are clear enough. The accumulator variable is better, but could be more clear.

Which of these are good alternatives to the accumulator variable and iterator variable names for the following prompt? Write code to print out each character of the string my_str on a separate line.

• Accumulator Variable: character_so_far ; Iterator Variable: char
• Incorrect, there is no accumulator variable neccessary
• Accumulator Variable: no variable needed ; Iterator Variable: c
• Though no accumulator variable is needed, the iterator variable is not clear enough
• Accumulator Variable: no variable needed ; Iterator Variable: char
• Yes, there is no accumulator variable needed and the iterator variable is clear (char is a common short form of character)