1 00:00:07,940 --> 00:00:13,005 Welcome back. So, we've learned how to do the dictionary accumulation pattern, 2 00:00:13,005 --> 00:00:15,380 but now we want to learn some things that we 3 00:00:15,380 --> 00:00:17,780 might want to do with our accumulated dictionary. 4 00:00:17,780 --> 00:00:21,350 So, rarely do we just want to do something like 5 00:00:21,350 --> 00:00:25,160 accumulate the number of every character in a dictionary, 6 00:00:25,160 --> 00:00:29,065 we typically want to actually do something with that data. 7 00:00:29,065 --> 00:00:31,670 So, here we have the same code as before. 8 00:00:31,670 --> 00:00:35,120 So, we open up the file scarlet.txt, 9 00:00:35,120 --> 00:00:38,220 load its contents into the variable txt, 10 00:00:38,220 --> 00:00:41,314 and initialize our accumulator variable, 11 00:00:41,314 --> 00:00:43,715 letter counts, to be an empty dictionary. 12 00:00:43,715 --> 00:00:46,250 Then through this for-loop, 13 00:00:46,250 --> 00:00:50,300 we associate every key in letter counts, 14 00:00:50,300 --> 00:00:53,960 which is going to be a character with the number of 15 00:00:53,960 --> 00:00:58,145 times that character appears in scarlet.txt. 16 00:00:58,145 --> 00:01:02,045 So, in other words, by the time we get to line 12, 17 00:01:02,045 --> 00:01:05,060 then letter counts is going to be a dictionary where 18 00:01:05,060 --> 00:01:08,570 every key is going to be a character such as 't', 19 00:01:08,570 --> 00:01:13,175 and every value is going to be the number of times that character appeared. 20 00:01:13,175 --> 00:01:18,995 So in the case of 't', it's 17,584. 21 00:01:18,995 --> 00:01:21,020 So, in our code before, 22 00:01:21,020 --> 00:01:23,810 we just printed out the number of 't's' 23 00:01:23,810 --> 00:01:26,600 by printing out "There are" and then something like, 24 00:01:26,600 --> 00:01:32,065 letter count sub 't', 't's' in scarlet.txt. 25 00:01:32,065 --> 00:01:35,970 We can do the same thing with other characters of course, 26 00:01:36,760 --> 00:01:43,120 so we can say that there are this many 'a's' and 'b's' and so on. 27 00:01:43,120 --> 00:01:46,010 But suppose that we wanted to print out the number of 28 00:01:46,010 --> 00:01:48,815 every single character in letter counts. 29 00:01:48,815 --> 00:01:51,965 Well, we could do that by using a for-loop. 30 00:01:51,965 --> 00:01:57,870 So, we could say for every character, 31 00:01:58,250 --> 00:02:05,080 I'll just call my iterator variable 'y' in letter counts. 32 00:02:09,380 --> 00:02:15,870 Then inside of our for-loop we can just say "There are" and then letter counts 33 00:02:15,870 --> 00:02:24,870 sub y and we can say that there are that many of the character y. 34 00:02:32,060 --> 00:02:35,110 So, what this code is going to do, 35 00:02:35,110 --> 00:02:40,570 is it's going to loop through all of the keys in letter counts and for every key, 36 00:02:40,570 --> 00:02:44,300 y is going to be the value of that key and 37 00:02:44,300 --> 00:02:47,900 then we're going to get the number of times that character y 38 00:02:47,900 --> 00:02:51,860 appears by printing out letter count sub y and then we say 39 00:02:51,860 --> 00:02:56,360 "There are" that many and then whatever that character y is. 40 00:02:56,360 --> 00:02:59,165 So, when we run our code, 41 00:02:59,165 --> 00:03:03,200 we can see that there are this many t's, 42 00:03:03,200 --> 00:03:07,105 this many s's, this many spaces, 43 00:03:07,105 --> 00:03:10,245 this many capital 't's' and so on. 44 00:03:10,245 --> 00:03:15,590 You can see that there are quite a few characters in scarlet.txt. 45 00:03:15,590 --> 00:03:20,255 So, let's suppose that we had called our dictionary x instead of letter counts. 46 00:03:20,255 --> 00:03:21,755 In this question we're asked, 47 00:03:21,755 --> 00:03:25,490 which of the following will print out true if there are more occurrences 48 00:03:25,490 --> 00:03:29,870 of the character e than the character t in the text, 49 00:03:29,870 --> 00:03:31,115 A Study in Scarlet, 50 00:03:31,115 --> 00:03:33,955 and false if t occurred more frequently? 51 00:03:33,955 --> 00:03:37,055 So, this is assuming that our previous code has run, 52 00:03:37,055 --> 00:03:40,520 except our dictionary is called x instead of letter counts. 53 00:03:40,520 --> 00:03:43,520 So, we want to write an expression that's going to be 54 00:03:43,520 --> 00:03:47,545 true if there are more e's than t's. 55 00:03:47,545 --> 00:03:49,180 So the way that we do that, 56 00:03:49,180 --> 00:03:54,875 we get the number of the character e by saying, x sub 'e', 57 00:03:54,875 --> 00:04:01,060 this is going to get the value associated with the character e in the dictionary x, 58 00:04:01,060 --> 00:04:07,690 and then we get the number of t's by saying, x sub 't'. 59 00:04:08,050 --> 00:04:12,290 If we want to know if there are more e's than t's, 60 00:04:12,290 --> 00:04:14,000 then we can write the expression, 61 00:04:14,000 --> 00:04:17,700 x sub 'e' is greater than x sub 't'. 62 00:04:17,700 --> 00:04:22,240 So in other words, the answer here is going to be B. 63 00:04:24,890 --> 00:04:28,320 So, if you've ever played the game Scrabble, 64 00:04:28,320 --> 00:04:32,285 then you know that different letters in Scrabble have different scores. 65 00:04:32,285 --> 00:04:37,610 So, letters that are more rare have a higher score than letters that are more common. 66 00:04:37,610 --> 00:04:40,360 So, let's suppose that we want to open up 67 00:04:40,360 --> 00:04:46,900 scarlet2.txt and we want to figure out what's the "Scrabble score" for this. 68 00:04:46,900 --> 00:04:49,119 So in other words, for every character, 69 00:04:49,119 --> 00:04:53,280 suppose that we want to know not just how many times that character appeared, 70 00:04:53,280 --> 00:04:56,435 but what the Scrabble score for that character with be. 71 00:04:56,435 --> 00:05:00,125 So, from lines one through 11, 72 00:05:00,125 --> 00:05:04,870 we have the dictionary accumulation pattern and it accumulates the frequency of 73 00:05:04,870 --> 00:05:10,510 every character in scarlet2.txt into this dictionary x. 74 00:05:10,510 --> 00:05:12,900 Then on line 13, 75 00:05:12,900 --> 00:05:15,005 we have a different dictionary, 76 00:05:15,005 --> 00:05:19,250 which represents the Scrabble letter value of every character. 77 00:05:19,250 --> 00:05:22,520 So in 'a', which is a really common letter, 78 00:05:22,520 --> 00:05:24,395 only has a score of one. 79 00:05:24,395 --> 00:05:27,375 Whereas a 'z', which is a lot less common, 80 00:05:27,375 --> 00:05:29,985 has a Scrabble value of 10. 81 00:05:29,985 --> 00:05:35,540 So, if we wanted to get the Scrabble score in scarlet2.txt, 82 00:05:35,540 --> 00:05:38,375 then what we could do is we could loop through 83 00:05:38,375 --> 00:05:42,135 every character inside of the dictionary letter values. 84 00:05:42,135 --> 00:05:46,680 So I'll say, for every character, 85 00:05:46,680 --> 00:05:50,115 I'll call it y in x. 86 00:05:50,115 --> 00:05:54,920 Then because not every character has a Scrabble score, so for instance, 87 00:05:54,920 --> 00:05:59,840 numbers or exclamation points don't have Scrabble scores but they're in our dictionary, 88 00:05:59,840 --> 00:06:04,160 we want to first check to see is that character y in letter values. 89 00:06:04,160 --> 00:06:10,000 So, I'll say if y is in letter values. 90 00:06:12,440 --> 00:06:16,790 We want to keep track of what's our letter values so far, 91 00:06:16,790 --> 00:06:20,600 so the way that we do that is with the standard accumulation pattern. 92 00:06:20,600 --> 00:06:23,645 So, I'm going to initialize an accumulator variable, 93 00:06:23,645 --> 00:06:27,190 I'll call it scrabble score, 94 00:06:27,190 --> 00:06:29,835 we initialize it to zero, 95 00:06:29,835 --> 00:06:35,420 and then we say, if this character y has a letter value, 96 00:06:35,420 --> 00:06:43,020 then scrabble score equals its previous score. 97 00:06:43,020 --> 00:06:48,950 So, scrabble score equals scrabble score plus, 98 00:06:48,950 --> 00:06:52,075 and then we want to add the score for that letter. 99 00:06:52,075 --> 00:06:53,220 So in other words, 100 00:06:53,220 --> 00:06:55,035 'a' has value one, 101 00:06:55,035 --> 00:06:56,685 'b' has value three, 102 00:06:56,685 --> 00:06:58,835 'c' has value three, and so on. 103 00:06:58,835 --> 00:07:05,230 So we get that score by saying letter values sub y, 104 00:07:05,230 --> 00:07:07,530 again y is going to be a, b, 105 00:07:07,530 --> 00:07:08,760 c, d, e, f, 106 00:07:08,760 --> 00:07:10,780 g, and so on. 107 00:07:10,970 --> 00:07:16,190 Then we want to multiply that by the number of times that that character appears. 108 00:07:16,190 --> 00:07:18,695 So, I'll say letter values sub y, 109 00:07:18,695 --> 00:07:22,515 times x sub y, 110 00:07:22,515 --> 00:07:28,115 x is again our dictionary accumulator variable from the previous problem. 111 00:07:28,115 --> 00:07:30,980 So in other words, we're saying the score is 112 00:07:30,980 --> 00:07:36,300 the previous score plus the value of that letter, 113 00:07:36,300 --> 00:07:40,100 times the number of times that that letter appeared. 114 00:07:40,100 --> 00:07:43,250 So, this is using the standard accumulation pattern. 115 00:07:43,250 --> 00:07:50,250 Here, our accumulator variable is scrabble score and we update it by saying, 116 00:07:50,250 --> 00:07:56,680 scrabble score equals scrabble score plus letter value sub y times x sub y. 117 00:07:57,560 --> 00:08:03,580 So, if I print out the scrabble score at the end of this for-loop, 118 00:08:05,680 --> 00:08:12,060 then I should expect it to be the actual scrabble score of scarlet2.txt. 119 00:08:12,850 --> 00:08:18,590 You can see that this has a pretty high scrabble score overall. 120 00:08:19,650 --> 00:08:24,025 Let's do some more questions that involve dictionary accumulation 121 00:08:24,025 --> 00:08:28,670 and doing something after we accumulate the results from the dictionary. 122 00:08:29,370 --> 00:08:31,705 So, in this question, 123 00:08:31,705 --> 00:08:34,720 we're told the dictionary travel contains the number 124 00:08:34,720 --> 00:08:38,380 of countries within each continent that Jackie has traveled to, 125 00:08:38,380 --> 00:08:42,415 and we're asked to find the total number of countries that Jackie has been to. 126 00:08:42,415 --> 00:08:48,190 In other words, our result is going to be two plus eight plus three plus four and so on. 127 00:08:48,190 --> 00:08:52,045 We're asked to save that into the variable named "total". 128 00:08:52,045 --> 00:08:54,130 So, in this question, 129 00:08:54,130 --> 00:08:58,730 we're given a dictionary and it doesn't matter how we arrived at this dictionary. 130 00:08:58,730 --> 00:09:02,600 So, we might have 20 lines before this that compute 131 00:09:02,600 --> 00:09:05,660 this dictionary's value or in this case we're 132 00:09:05,660 --> 00:09:09,190 just given the dictionary as a literal object. 133 00:09:09,190 --> 00:09:12,105 But regardless of how we get that dictionary, 134 00:09:12,105 --> 00:09:15,665 what we want to do is we want to accumulate the sum 135 00:09:15,665 --> 00:09:19,880 of every value associated with every key in that dictionary. 136 00:09:19,880 --> 00:09:23,470 So, we can do that with the standard accumulation pattern. 137 00:09:23,470 --> 00:09:29,430 So, I'm going to first initialize an accumulator variable total to be zero, 138 00:09:30,990 --> 00:09:38,535 so this is initializing our accumulator variable. 139 00:09:38,535 --> 00:09:42,410 Then we want to loop through every single continent, so I'll say, 140 00:09:42,410 --> 00:09:46,670 for continent in travel. 141 00:09:46,670 --> 00:09:51,140 So, continent is going to be North America, 142 00:09:51,140 --> 00:09:55,580 Europe, South America, Asia, Africa et cetera. 143 00:09:55,580 --> 00:09:58,730 So, for every continent in travel, 144 00:09:58,730 --> 00:10:03,110 we get the number of countries within that continent that Jackie has 145 00:10:03,110 --> 00:10:09,060 been to by saying, travel sub continent. 146 00:10:09,740 --> 00:10:17,880 We want to say total equals total plus travel sub continent, 147 00:10:17,880 --> 00:10:24,790 so this is us updating our accumulator variable. 148 00:10:27,190 --> 00:10:32,050 So again, what we're doing is we're looping through every key in 149 00:10:32,050 --> 00:10:37,615 our dictionary and we are getting the value associated with that key. 150 00:10:37,615 --> 00:10:41,010 So, two, eight, three and so on, 151 00:10:41,010 --> 00:10:45,730 and adding that value to our previous total. 152 00:10:45,730 --> 00:10:48,670 By the time we're done with that for-loop, 153 00:10:48,670 --> 00:10:53,120 we should have the total number of countries that Jackie has been to. 154 00:10:54,950 --> 00:10:59,460 In this question, we're told that schedule is a dictionary where 155 00:10:59,460 --> 00:11:03,890 a class name is the key and its value is how many credits it's worth. 156 00:11:03,890 --> 00:11:07,880 We're asked to go through and accumulate the total number of credits that have 157 00:11:07,880 --> 00:11:12,635 been earned so far and assign that to the variable total credits. 158 00:11:12,635 --> 00:11:16,610 So, this is the same idea with just a slightly different dictionary. 159 00:11:16,610 --> 00:11:22,350 So, our accumulator variable is going to be named total credits. 160 00:11:22,600 --> 00:11:25,595 We're going to initialize it to zero, 161 00:11:25,595 --> 00:11:28,790 then we want to loop through every course in our schedule. 162 00:11:28,790 --> 00:11:32,405 So, I'll say for course in schedule. 163 00:11:32,405 --> 00:11:36,990 So again here, course is going to be "UARTS 150", 164 00:11:36,990 --> 00:11:40,695 "SPANISH 103", "English 125" and so on. 165 00:11:40,695 --> 00:11:44,195 We get the number of credits for that course by saying, 166 00:11:44,195 --> 00:11:51,680 schedule sub course and we add that to the total number of credits by saying, 167 00:11:51,680 --> 00:11:57,179 total credits equals total credits 168 00:11:58,840 --> 00:12:02,970 plus schedule sub course. 169 00:12:04,640 --> 00:12:08,410 That's all for now, until next time.