1 00:00:07,970 --> 00:00:12,480 We're back with a little more way other programmer advice. 2 00:00:12,480 --> 00:00:17,865 You've seen the accumulator pattern in various guises now, 3 00:00:17,865 --> 00:00:21,450 and this is some advice on how you can know when to use 4 00:00:21,450 --> 00:00:26,490 it and some little tricks for getting it right. 5 00:00:26,490 --> 00:00:32,775 You may remember back in elementary school or maybe you have kids like I do, 6 00:00:32,775 --> 00:00:37,770 who've been in elementary school recently and they start dealing with word problems, 7 00:00:37,770 --> 00:00:41,785 and you look at the problems and you say wow, 8 00:00:41,785 --> 00:00:44,865 you're just going to- that's three plus five. 9 00:00:44,865 --> 00:00:48,660 But somehow converting that word problem into three plus five is challenging. 10 00:00:48,660 --> 00:00:51,200 So you learn little tricks like when it says, 11 00:00:51,200 --> 00:00:53,165 how many do they have altogether, 12 00:00:53,165 --> 00:00:56,375 they should know that it's an addition problem, 13 00:00:56,375 --> 00:00:59,585 or how many are left is going to be a subtraction problem. 14 00:00:59,585 --> 00:01:02,420 So here's something a little similar or how to recognize when 15 00:01:02,420 --> 00:01:05,195 you're going to be dealing with one of our accumulation patterns. 16 00:01:05,195 --> 00:01:10,325 We've got these phrases on the left in this table, 17 00:01:10,325 --> 00:01:16,265 and we've got which accumulation pattern that might suggest you that you're going to use. 18 00:01:16,265 --> 00:01:18,079 So, if the problem says, 19 00:01:18,079 --> 00:01:20,120 how many things are in a list, 20 00:01:20,120 --> 00:01:22,610 or how frequently do something happen, 21 00:01:22,610 --> 00:01:25,085 that's going to be a count accumulation. 22 00:01:25,085 --> 00:01:28,190 If the problem asks for the sum or the total, 23 00:01:28,190 --> 00:01:30,215 that's going to be a sum accumulation. 24 00:01:30,215 --> 00:01:33,695 If the problem asks to generate a list of things, 25 00:01:33,695 --> 00:01:35,660 that's going to be a list accumulation, 26 00:01:35,660 --> 00:01:39,620 and if it says to concatenate or join things together, 27 00:01:39,620 --> 00:01:41,960 that's probably going to be a string accumulation. 28 00:01:41,960 --> 00:01:45,920 Next on this page, we've got a little bit of advice of, 29 00:01:45,920 --> 00:01:50,465 how to think about what you're going to do before you write any code. 30 00:01:50,465 --> 00:01:52,415 One thing to ask is, 31 00:01:52,415 --> 00:01:54,970 which sequence are you going to iterate through, 32 00:01:54,970 --> 00:01:56,545 and second question is, 33 00:01:56,545 --> 00:01:59,300 what kind of value are you going to accumulate? 34 00:01:59,300 --> 00:02:01,730 Are you going to accumulate a number, 35 00:02:01,730 --> 00:02:04,475 a list, or a string? 36 00:02:04,475 --> 00:02:08,120 Then our third set of advice is about choosing 37 00:02:08,120 --> 00:02:12,845 good names for your accumulator variable and your iterator variable, 38 00:02:12,845 --> 00:02:16,280 names that'll help you keep track of things. 39 00:02:16,280 --> 00:02:20,255 As we've said for any iteration, 40 00:02:20,255 --> 00:02:27,365 it's a good idea to use a singular noun for the iterator variable name. 41 00:02:27,365 --> 00:02:31,040 If you are iterating through a list of words, 42 00:02:31,040 --> 00:02:35,015 then it might be a good idea to use an iterator variable name like, 43 00:02:35,015 --> 00:02:39,650 WRD to just remind you that each time on 44 00:02:39,650 --> 00:02:45,695 each iteration WRD your iterator variable is going to be bound to a word. 45 00:02:45,695 --> 00:02:48,304 For the accumulator variable, 46 00:02:48,304 --> 00:02:50,840 I sometimes just use a generic Accum, 47 00:02:50,840 --> 00:02:55,865 but it also can be helpful to sort of remind yourself of what you're accumulating. 48 00:02:55,865 --> 00:03:00,530 So got suggestions for names like, counts so far, 49 00:03:00,530 --> 00:03:02,225 or total so far, 50 00:03:02,225 --> 00:03:07,040 or cubes so far could be good names for your accumulator variable. 51 00:03:07,040 --> 00:03:09,800 Having that appendix so far, 52 00:03:09,800 --> 00:03:11,960 sort of reminds you that, 53 00:03:11,960 --> 00:03:16,400 on each iteration the accumulator is getting more stuff and so, 54 00:03:16,400 --> 00:03:19,910 what you have at a certain point in the execution of the program is, 55 00:03:19,910 --> 00:03:22,655 the total so far not the grand total, 56 00:03:22,655 --> 00:03:27,450 but after you finished all of the iterations you're going to have the grand total. 57 00:03:27,980 --> 00:03:33,380 There's a bunch of questions to help you check your understanding on this, 58 00:03:33,380 --> 00:03:38,150 but I'm just going to go through a few of them with you. 59 00:03:38,150 --> 00:03:43,120 So- and we've got several of these in the book, 60 00:03:43,120 --> 00:03:47,540 I'll just go through a couple and you're supposed to sort of pick out what if 61 00:03:47,540 --> 00:03:52,860 anything in this problem should make you think that you're going to do an accumulation. 62 00:03:53,020 --> 00:03:56,125 I'll tell you now that some of them, 63 00:03:56,125 --> 00:03:58,725 there is something in for some of them it actually isn't 64 00:03:58,725 --> 00:04:01,670 an accumulator pattern at all, so let's start with this one. 65 00:04:01,670 --> 00:04:04,580 For each string in words, 66 00:04:04,580 --> 00:04:08,600 add ED to the end of the word in order to make the word being the past 67 00:04:08,600 --> 00:04:14,220 tense and save those past tense words to a list called passwords. 68 00:04:14,230 --> 00:04:17,930 Does this suggest to you an accumulation pattern, 69 00:04:17,930 --> 00:04:19,355 and if so why? 70 00:04:19,355 --> 00:04:22,340 We've got two possibilities for y here, 71 00:04:22,340 --> 00:04:27,230 either because it says save some things to a list, 72 00:04:27,230 --> 00:04:32,450 or because it says add ED to the end of the word. 73 00:04:32,450 --> 00:04:37,174 The correct answer is saving 74 00:04:37,174 --> 00:04:41,945 a bunch of things to a list should suggest to you accumulating a list. 75 00:04:41,945 --> 00:04:44,750 What goes into each item, 76 00:04:44,750 --> 00:04:49,440 that's not what suggests that we needed an accumulation pattern. 77 00:04:50,890 --> 00:04:53,940 Let's try the next one. 78 00:04:56,320 --> 00:05:00,560 Write code to sum up all the numbers in the list seat counts, 79 00:05:00,560 --> 00:05:03,725 store that number in the variable total seat counts, 80 00:05:03,725 --> 00:05:06,815 and here our correct answer is, 81 00:05:06,815 --> 00:05:09,624 to sum up because, 82 00:05:09,624 --> 00:05:13,790 to sum up suggests that we got a bunch of things and we want to get the grand total, 83 00:05:13,790 --> 00:05:17,820 so that is going to be a sum accumulation. 84 00:05:18,810 --> 00:05:23,850 So there's some more and I encourage you to try those. 85 00:05:23,850 --> 00:05:28,075 Here's a couple of questions about what type of 86 00:05:28,075 --> 00:05:33,215 object you should expect to have stored in your accumulator variable. 87 00:05:33,215 --> 00:05:35,230 So, the question here is, 88 00:05:35,230 --> 00:05:37,450 write code that will count the number of vowels in 89 00:05:37,450 --> 00:05:42,490 the sentence S and assign the result to the variable num_vowels. 90 00:05:42,490 --> 00:05:46,690 Now we haven't quite got all them mechanisms in Python to be 91 00:05:46,690 --> 00:05:49,930 able to count only the vowels and to leave out the consonants, 92 00:05:49,930 --> 00:05:52,315 you're going to get that pretty soon in this course. 93 00:05:52,315 --> 00:05:55,720 But even so the structure of this is enough that you 94 00:05:55,720 --> 00:05:58,810 know it's going to be an accumulation pattern, 95 00:05:58,810 --> 00:06:00,475 and you're going to count the number of vowels, 96 00:06:00,475 --> 00:06:02,265 a count is a number. 97 00:06:02,265 --> 00:06:05,420 So our correct answer here is that it is 98 00:06:05,420 --> 00:06:11,340 an integer because we want to keep track of a number. 99 00:06:11,900 --> 00:06:16,670 So different question about that same problem, 100 00:06:16,670 --> 00:06:21,260 what sequence will you iterate through as you accumulate a result? 101 00:06:21,260 --> 00:06:25,730 We have two possibilities here because we 102 00:06:25,730 --> 00:06:30,260 have a sentence S and we have a variable numb vowels. 103 00:06:30,260 --> 00:06:32,270 Which of those are you going to iterate through? 104 00:06:32,270 --> 00:06:34,640 The answer is you're going to iterate through 105 00:06:34,640 --> 00:06:38,630 S. You're going to accumulate your results in num_vowels, 106 00:06:38,630 --> 00:06:43,070 but the sequence that you're going to iterate through is the characters in 107 00:06:43,070 --> 00:06:49,590 the sentence S. So there's a couple of more of that flavor. 108 00:06:50,230 --> 00:06:56,810 Then we've got some questions about what should you name your accumulator variable, 109 00:06:56,810 --> 00:07:00,905 and what should you name your iterator variable. So let's try this one. 110 00:07:00,905 --> 00:07:03,050 For each string in words, 111 00:07:03,050 --> 00:07:06,145 add ED to the end of the word to make the word past tense, 112 00:07:06,145 --> 00:07:10,040 save this past tense words to a list called past words. 113 00:07:10,040 --> 00:07:13,010 So a couple of possibilities here, 114 00:07:13,010 --> 00:07:17,135 we're either going to name our accumulator variable words so far, 115 00:07:17,135 --> 00:07:18,485 or we're going to name it, 116 00:07:18,485 --> 00:07:25,355 changed words and our iterator variable name can either be WRD short for word, 117 00:07:25,355 --> 00:07:28,520 or x, or it could be ED. 118 00:07:28,520 --> 00:07:31,970 My suggestion is, this is just a matter of 119 00:07:31,970 --> 00:07:35,400 style but these style things help you to avoid confusion, 120 00:07:35,400 --> 00:07:39,020 and can help readers of your programs also not get confused. 121 00:07:39,020 --> 00:07:44,660 Is that word so far is kind of an ideal name for your accumulator variable, 122 00:07:44,660 --> 00:07:46,970 it tells you so far says it's 123 00:07:46,970 --> 00:07:51,560 an accumulator and word says what kind of thing you are accumulating there, 124 00:07:51,560 --> 00:07:53,465 and for your iterator variable, 125 00:07:53,465 --> 00:07:58,970 you're going to be iterating through this string WRDS, 126 00:07:58,970 --> 00:08:02,360 plural, that's a list of strings, 127 00:08:02,360 --> 00:08:05,000 each of those strings is one word. 128 00:08:05,000 --> 00:08:12,240 So having an iterator variable of WRD seems like a really good choice to me. 129 00:08:12,850 --> 00:08:16,505 There are a few more questions like this and I encourage you to 130 00:08:16,505 --> 00:08:19,460 go through all of them just to reinforce the way 131 00:08:19,460 --> 00:08:22,970 of the programmer around recognizing when you need to use 132 00:08:22,970 --> 00:08:27,349 an accumulator pattern on choosing the right variable names, 133 00:08:27,349 --> 00:08:31,160 and on recognizing what kind of thing you're going to accumulate a string or 134 00:08:31,160 --> 00:08:35,765 a count or number and what sequence you're going to iterate through. 135 00:08:35,765 --> 00:08:38,990 So, go through those and you will become 136 00:08:38,990 --> 00:08:43,340 an expert on the accumulator pattern. See you next time.