1 00:00:07,880 --> 00:00:12,300 So previously, we've discussed the accumulator pattern, 2 00:00:12,300 --> 00:00:14,640 but the accumulator pattern becomes even more 3 00:00:14,640 --> 00:00:17,160 powerful when recombine it with conditionals. 4 00:00:17,160 --> 00:00:21,395 So, recall that the accumulator pattern involves three broad steps. 5 00:00:21,395 --> 00:00:24,120 So first, we initialize 6 00:00:25,040 --> 00:00:32,520 an accumulator variable and then, 7 00:00:32,520 --> 00:00:42,010 we iterate through the relevant sequence, 8 00:00:42,050 --> 00:00:45,680 and then as we iterate through that sequence, 9 00:00:45,680 --> 00:00:51,570 we update our accumulator variable. 10 00:00:52,700 --> 00:00:54,840 Then in the end, 11 00:00:54,840 --> 00:01:02,070 the accumulator variable, if we did these steps right has the answer that we want. 12 00:01:04,210 --> 00:01:11,060 Okay. So, let's apply this template and use it in combination with conditionals. 13 00:01:11,060 --> 00:01:14,840 So, suppose that we have a phrase here and 14 00:01:14,840 --> 00:01:18,604 we want to count the number of characters that are not spaces. 15 00:01:18,604 --> 00:01:22,875 So in other words, we want to count one, two, three, four, 16 00:01:22,875 --> 00:01:25,155 you want to skip the space, five, 17 00:01:25,155 --> 00:01:26,745 skip the space, six, 18 00:01:26,745 --> 00:01:28,485 seven, and so on. 19 00:01:28,485 --> 00:01:32,135 Then, we can use the accumulator pattern with conditionals. 20 00:01:32,135 --> 00:01:34,540 The way that we can do that is here, 21 00:01:34,540 --> 00:01:38,300 we initialize our accumulator variable total to be zero, 22 00:01:38,300 --> 00:01:43,140 in other words, the number of non-space characters that we've seen so far. 23 00:01:44,260 --> 00:01:53,810 Then, we iterate through our sequence in this case it's phrase and then, 24 00:01:53,810 --> 00:02:00,130 we update our accumulator variable appropriately. 25 00:02:00,130 --> 00:02:02,560 So in this case, we want to say, 26 00:02:02,560 --> 00:02:09,950 we've seen one more non-character space if the character in our phrase is not a space. 27 00:02:09,950 --> 00:02:17,160 So we write, if character and then exclamation point equals that's to say not equal to. 28 00:02:17,160 --> 00:02:21,830 So, if character is not a space then we say, 29 00:02:21,830 --> 00:02:24,050 total equals total plus one. 30 00:02:24,050 --> 00:02:25,925 So, when we run our code, 31 00:02:25,925 --> 00:02:29,135 it's going to count the number of non-space characters. 32 00:02:29,135 --> 00:02:31,725 In this case, it's 26. 33 00:02:31,725 --> 00:02:34,575 If we had only spaces here, 34 00:02:34,575 --> 00:02:37,005 then we would get zero. 35 00:02:37,005 --> 00:02:41,190 If we had three non-space characters, 36 00:02:41,190 --> 00:02:44,050 then we would get three and so on. 37 00:02:44,450 --> 00:02:48,679 If we wanted to solve a slightly different problem, 38 00:02:48,679 --> 00:02:53,660 so let's suppose that we want to count the number of vowels inside of this string s, 39 00:02:53,660 --> 00:02:55,940 then we take the same broad steps. 40 00:02:55,940 --> 00:03:01,530 So, we initialize our accumulator variable x to be zero, 41 00:03:01,530 --> 00:03:04,660 then we loop through our sequence, 42 00:03:06,080 --> 00:03:10,245 and then we say, if this character i, 43 00:03:10,245 --> 00:03:12,330 so i is the individual character, 44 00:03:12,330 --> 00:03:15,090 so if it's in this list a, 45 00:03:15,090 --> 00:03:16,860 e, i, o, u, 46 00:03:16,860 --> 00:03:18,570 so in other words this is saying, 47 00:03:18,570 --> 00:03:20,775 if i is a vowel, 48 00:03:20,775 --> 00:03:22,365 it's one of a, e, 49 00:03:22,365 --> 00:03:23,925 i, o or u. 50 00:03:23,925 --> 00:03:30,980 If it's a vowel, then we update our accumulator variable x to add one onto it, 51 00:03:30,980 --> 00:03:34,835 and so the result is that we're going to count the number of vowels. 52 00:03:34,835 --> 00:03:36,590 So, when we run our code, 53 00:03:36,590 --> 00:03:42,725 we can see that there are eight vowels in the string s. So, 54 00:03:42,725 --> 00:03:45,155 we can visualize this with an example. 55 00:03:45,155 --> 00:03:48,515 So here, we want to cut the number of o's in onomatopoeia. 56 00:03:48,515 --> 00:03:52,250 In our code, we loop through every character and we 57 00:03:52,250 --> 00:03:55,970 say if the character c is the character o, 58 00:03:55,970 --> 00:03:58,040 then add one to o count. 59 00:03:58,040 --> 00:04:02,275 The result is that we're going to loop through every character in our word, 60 00:04:02,275 --> 00:04:05,640 and print out the number of o's in our word. 61 00:04:05,640 --> 00:04:07,965 So, when we see the first o, 62 00:04:07,965 --> 00:04:09,405 we add one onto it. 63 00:04:09,405 --> 00:04:11,010 When see the second o, 64 00:04:11,010 --> 00:04:12,660 we add another onto it, 65 00:04:12,660 --> 00:04:14,505 and then we keep looping through. 66 00:04:14,505 --> 00:04:15,945 Every time we see an o, 67 00:04:15,945 --> 00:04:18,195 we increment o count by one, 68 00:04:18,195 --> 00:04:21,660 and when our code is done running, then, 69 00:04:21,660 --> 00:04:23,230 we o count would be four, 70 00:04:23,230 --> 00:04:26,710 and we print out there are four o's in onomatopoeia. 71 00:04:26,710 --> 00:04:30,020 Another common pattern that mixes conditionals in 72 00:04:30,020 --> 00:04:33,955 the accumulator pattern is what's called max value accumulation. 73 00:04:33,955 --> 00:04:36,330 So, in max value accumulation, 74 00:04:36,330 --> 00:04:40,225 we're trying to find the largest thing in a list of things. 75 00:04:40,225 --> 00:04:41,890 So, in this example, 76 00:04:41,890 --> 00:04:45,890 suppose that we want to find the largest number in this list of numbers. 77 00:04:45,890 --> 00:04:49,790 So, nums is just a random list of numbers. 78 00:04:49,790 --> 00:04:52,910 What we're going to do is we're going to use 79 00:04:52,910 --> 00:04:55,370 the accumulator pattern to keep track of the 80 00:04:55,370 --> 00:04:58,480 best or highest number that we've seen so far. 81 00:04:58,480 --> 00:05:02,290 So, in other words, we assign best_num. 82 00:05:05,060 --> 00:05:10,070 So at first, we just assign it to be the first number that we see. 83 00:05:10,070 --> 00:05:15,710 So, we assign best_num to be nine and then 84 00:05:15,710 --> 00:05:21,235 what we're going to do is we're going to loop through all of the numbers in nums and say, 85 00:05:21,235 --> 00:05:24,700 if we find a higher number than our best numbered so far, 86 00:05:24,700 --> 00:05:28,925 then we're going to update best num to be that number. 87 00:05:28,925 --> 00:05:32,165 So, we say for every number n, 88 00:05:32,165 --> 00:05:36,155 if n is greater than the best number that we've seen so far, 89 00:05:36,155 --> 00:05:40,865 then reassign best_num to be n. So, 90 00:05:40,865 --> 00:05:45,480 I'm going to simulate execution on this code before actually running it. 91 00:05:46,270 --> 00:05:52,745 Okay. So first, we assign best_num to be the first number, 92 00:05:52,745 --> 00:05:55,295 then we loop through every number. 93 00:05:55,295 --> 00:05:58,340 So, after we assign best_num to be the first number, 94 00:05:58,340 --> 00:06:00,470 then we loop through every number. 95 00:06:00,470 --> 00:06:04,100 So first, n is going to be nine and we 96 00:06:04,100 --> 00:06:08,270 say is nine greater than best num which is nine? It's not. 97 00:06:08,270 --> 00:06:11,630 So, we then loop through you to the next number three. 98 00:06:11,630 --> 00:06:14,035 Is three greater than nine? 99 00:06:14,035 --> 00:06:17,615 It's not. So, we loop through to the next number eight. 100 00:06:17,615 --> 00:06:20,375 Is eight greater than nine? It's not. 101 00:06:20,375 --> 00:06:24,485 So, we loop through to the next number 11 and then now, 102 00:06:24,485 --> 00:06:26,510 when n is 11, we say, 103 00:06:26,510 --> 00:06:30,150 is 11 greater than nine? It is. 104 00:06:30,150 --> 00:06:38,545 So, we reassign best_num from nine to instead be 11 and then, 105 00:06:38,545 --> 00:06:41,750 we loop through to the next number five 106 00:06:41,750 --> 00:06:46,580 and rather than comparing it to nine because best_num is now 11, 107 00:06:46,580 --> 00:06:50,795 we compare, is five greater than 11? 108 00:06:50,795 --> 00:06:55,310 It is not. So, we loop through to the next number 29 and 109 00:06:55,310 --> 00:07:00,165 we ask is 29 greater than 11? It is. 110 00:07:00,165 --> 00:07:07,650 So, we reassign best_num to be 29 and then, 111 00:07:07,650 --> 00:07:08,860 after we've done that, 112 00:07:08,860 --> 00:07:13,220 n is now two and we're asked is two greater than 29? 113 00:07:13,220 --> 00:07:16,025 It's not. So, at that point, 114 00:07:16,025 --> 00:07:19,504 because we've gone through all the numbers in our sequence, 115 00:07:19,504 --> 00:07:22,140 then we're done with this for-loop. 116 00:07:22,180 --> 00:07:24,720 When we're done with this for-loop, 117 00:07:24,720 --> 00:07:28,230 best_num has been assigned to 29 and so, 118 00:07:28,230 --> 00:07:30,465 we print out 29. 119 00:07:30,465 --> 00:07:32,660 So in other words, our strategy for 120 00:07:32,660 --> 00:07:38,510 max value accumulation is to keep track of the highest value that we've seen so far, 121 00:07:38,510 --> 00:07:42,005 and then to loop through every item in the sequence, 122 00:07:42,005 --> 00:07:46,055 and if we see a number higher than the highest one that we've seen so far, 123 00:07:46,055 --> 00:07:51,665 then reassign our highest that we've seen so far to that higher number. 124 00:07:51,665 --> 00:07:55,120 We know that by the time this for-loop is done running, 125 00:07:55,120 --> 00:08:00,080 then best_num, it's going to be the largest number that's in our sequence. 126 00:08:00,080 --> 00:08:01,805 So, when you run this code, 127 00:08:01,805 --> 00:08:06,570 we should expect 29 to print out and we see that it does. 128 00:08:06,640 --> 00:08:11,990 If I change this sequence to instead have 50 instead of five, 129 00:08:11,990 --> 00:08:15,980 then we should now expect 50 to print out and so on. 130 00:08:15,980 --> 00:08:18,395 If I change this to be 200, 131 00:08:18,395 --> 00:08:20,155 200 would print out. 132 00:08:20,155 --> 00:08:24,720 So, it always prints out the largest number in this sequence. 133 00:08:24,910 --> 00:08:29,840 So, in this question, we're asked what is printed by the following statements? 134 00:08:29,840 --> 00:08:32,990 So, we assign s to the string we are learning, 135 00:08:32,990 --> 00:08:35,690 then we assign x to be zero. 136 00:08:35,690 --> 00:08:39,170 So, x here is our accumulator variable, 137 00:08:39,170 --> 00:08:42,695 s is the sequence that we're iterating over and we say, 138 00:08:42,695 --> 00:08:44,750 if i is in a, b, 139 00:08:44,750 --> 00:08:45,920 c, d, e. So, 140 00:08:45,920 --> 00:08:48,025 in other words, if it's one of these characters, 141 00:08:48,025 --> 00:08:51,165 then assign x to be x plus one, 142 00:08:51,165 --> 00:08:54,555 then at the end, we print out what's the value of x. 143 00:08:54,555 --> 00:08:57,200 So, this is the accumulation pattern and we're 144 00:08:57,200 --> 00:08:59,690 accumulating the number of characters that are a, 145 00:08:59,690 --> 00:09:01,995 b, c, d and e. So, 146 00:09:01,995 --> 00:09:04,300 in order to answer this question, 147 00:09:04,300 --> 00:09:06,400 we have to count the number of a, b, c, 148 00:09:06,400 --> 00:09:10,855 d's and e's in the string s. So I see, 1, 2, 149 00:09:10,855 --> 00:09:16,670 3, 4, 5 and so, 150 00:09:16,670 --> 00:09:19,230 I expect the answer to be five. 151 00:09:19,750 --> 00:09:22,170 So, this question asks, 152 00:09:22,170 --> 00:09:24,375 what's printed by the following statements? 153 00:09:24,375 --> 00:09:27,670 So, we have a variable called min value that gets 154 00:09:27,670 --> 00:09:31,825 assigned to zero and we have a list that we're iterating over in this for-loop, 155 00:09:31,825 --> 00:09:36,400 and we say, if an item in this list is less than min value, 156 00:09:36,400 --> 00:09:38,825 then min value equals item. 157 00:09:38,825 --> 00:09:41,895 So here, we assign min value to zero, 158 00:09:41,895 --> 00:09:44,650 and one thing I noticed right away is that 159 00:09:44,650 --> 00:09:48,240 no item in this list is actually less than zero. 160 00:09:48,240 --> 00:09:51,820 So, what that means is that this min expression is never 161 00:09:51,820 --> 00:09:55,495 going to be true because we don't have an item that's less than zero. 162 00:09:55,495 --> 00:09:59,980 So, I expect this to never run and by the end of our loop, 163 00:09:59,980 --> 00:10:03,560 min value is going to be zero. 164 00:10:03,740 --> 00:10:09,980 So, if we actually wanted to collect the minimum value in this list, 165 00:10:09,980 --> 00:10:12,530 rather than assigning min value to be zero, 166 00:10:12,530 --> 00:10:16,535 we should assign min value equals the first item in the list. 167 00:10:16,535 --> 00:10:20,320 So another words, list sub zero, 168 00:10:20,320 --> 00:10:23,300 and then min value would start out as five, 169 00:10:23,300 --> 00:10:26,785 and we would do a form of max value accumulation. 170 00:10:26,785 --> 00:10:28,165 So, as we've seen, 171 00:10:28,165 --> 00:10:33,410 the accumulator pattern becomes even more powerful when we combine it with conditionals. 172 00:10:33,410 --> 00:10:36,450 That's all for now until next time.