We're back with a little more way other programmer advice.
You've seen the accumulator pattern in various guises now,
and this is some advice on how you can know when to use
it and some little tricks for getting it right.
You may remember back in elementary school or maybe you have kids like I do,
who've been in elementary school recently and they start dealing with word problems,
and you look at the problems and you say wow,
you're just going to- that's three plus five.
But somehow converting that word problem into three plus five is challenging.
So you learn little tricks like when it says,
how many do they have altogether,
they should know that it's an addition problem,
or how many are left is going to be a subtraction problem.
So here's something a little similar or how to recognize when
you're going to be dealing with one of our accumulation patterns.
We've got these phrases on the left in this table,
and we've got which accumulation pattern that might suggest you that you're going to use.
So, if the problem says,
how many things are in a list,
or how frequently do something happen,
that's going to be a count accumulation.
If the problem asks for the sum or the total,
that's going to be a sum accumulation.
If the problem asks to generate a list of things,
that's going to be a list accumulation,
and if it says to concatenate or join things together,
that's probably going to be a string accumulation.
Next on this page, we've got a little bit of advice of,
how to think about what you're going to do before you write any code.
One thing to ask is,
which sequence are you going to iterate through,
and second question is,
what kind of value are you going to accumulate?
Are you going to accumulate a number,
a list, or a string?
Then our third set of advice is about choosing
good names for your accumulator variable and your iterator variable,
names that'll help you keep track of things.
As we've said for any iteration,
it's a good idea to use a singular noun for the iterator variable name.
If you are iterating through a list of words,
then it might be a good idea to use an iterator variable name like,
WRD to just remind you that each time on
each iteration WRD your iterator variable is going to be bound to a word.
For the accumulator variable,
I sometimes just use a generic Accum,
but it also can be helpful to sort of remind yourself of what you're accumulating.
So got suggestions for names like, counts so far,
or total so far,
or cubes so far could be good names for your accumulator variable.
Having that appendix so far,
sort of reminds you that,
on each iteration the accumulator is getting more stuff and so,
what you have at a certain point in the execution of the program is,
the total so far not the grand total,
but after you finished all of the iterations you're going to have the grand total.
There's a bunch of questions to help you check your understanding on this,
but I'm just going to go through a few of them with you.
So- and we've got several of these in the book,
I'll just go through a couple and you're supposed to sort of pick out what if
anything in this problem should make you think that you're going to do an accumulation.
I'll tell you now that some of them,
there is something in for some of them it actually isn't
an accumulator pattern at all, so let's start with this one.
For each string in words,
add ED to the end of the word in order to make the word being the past
tense and save those past tense words to a list called passwords.
Does this suggest to you an accumulation pattern,
and if so why?
We've got two possibilities for y here,
either because it says save some things to a list,
or because it says add ED to the end of the word.
The correct answer is saving
a bunch of things to a list should suggest to you accumulating a list.
What goes into each item,
that's not what suggests that we needed an accumulation pattern.
Let's try the next one.
Write code to sum up all the numbers in the list seat counts,
store that number in the variable total seat counts,
and here our correct answer is,
to sum up because,
to sum up suggests that we got a bunch of things and we want to get the grand total,
so that is going to be a sum accumulation.
So there's some more and I encourage you to try those.
Here's a couple of questions about what type of
object you should expect to have stored in your accumulator variable.
So, the question here is,
write code that will count the number of vowels in
the sentence S and assign the result to the variable num_vowels.
Now we haven't quite got all them mechanisms in Python to be
able to count only the vowels and to leave out the consonants,
you're going to get that pretty soon in this course.
But even so the structure of this is enough that you
know it's going to be an accumulation pattern,
and you're going to count the number of vowels,
a count is a number.
So our correct answer here is that it is
an integer because we want to keep track of a number.
So different question about that same problem,
what sequence will you iterate through as you accumulate a result?
We have two possibilities here because we
have a sentence S and we have a variable numb vowels.
Which of those are you going to iterate through?
The answer is you're going to iterate through
S. You're going to accumulate your results in num_vowels,
but the sequence that you're going to iterate through is the characters in
the sentence S. So there's a couple of more of that flavor.
Then we've got some questions about what should you name your accumulator variable,
and what should you name your iterator variable. So let's try this one.
For each string in words,
add ED to the end of the word to make the word past tense,
save this past tense words to a list called past words.
So a couple of possibilities here,
we're either going to name our accumulator variable words so far,
or we're going to name it,
changed words and our iterator variable name can either be WRD short for word,
or x, or it could be ED.
My suggestion is, this is just a matter of
style but these style things help you to avoid confusion,
and can help readers of your programs also not get confused.
Is that word so far is kind of an ideal name for your accumulator variable,
it tells you so far says it's
an accumulator and word says what kind of thing you are accumulating there,
and for your iterator variable,
you're going to be iterating through this string WRDS,
plural, that's a list of strings,
each of those strings is one word.
So having an iterator variable of WRD seems like a really good choice to me.
There are a few more questions like this and I encourage you to
go through all of them just to reinforce the way
of the programmer around recognizing when you need to use
an accumulator pattern on choosing the right variable names,
and on recognizing what kind of thing you're going to accumulate a string or
a count or number and what sequence you're going to iterate through.
So, go through those and you will become
an expert on the accumulator pattern. See you next time.