1 00:00:07,880 --> 00:00:11,145 Welcome back. In this question, 2 00:00:11,145 --> 00:00:14,580 we're asked to create a dictionary called D that keeps 3 00:00:14,580 --> 00:00:18,060 track of all of the characters in the string placement, 4 00:00:18,060 --> 00:00:21,285 and notes how many times each character was seen. 5 00:00:21,285 --> 00:00:23,040 So, in order to do this, 6 00:00:23,040 --> 00:00:27,550 we're going to kind of use the standard dictionary accumulation pattern, 7 00:00:28,970 --> 00:00:33,015 but this isn't all that this question asks us to do. 8 00:00:33,015 --> 00:00:37,505 It says then, find the key with the lowest value in the dictionary, 9 00:00:37,505 --> 00:00:40,810 and assign that key to be min value. 10 00:00:40,810 --> 00:00:43,540 In order to do this part of the question, 11 00:00:43,540 --> 00:00:47,820 then we're going to use the standard accumulation pattern, 12 00:00:50,440 --> 00:00:55,765 more specifically actually, we're going to use min or max value accumulation. 13 00:00:55,765 --> 00:01:01,070 So, let's do this first part which involves dictionary accumulation first. 14 00:01:01,070 --> 00:01:04,550 So, I'm going to create a dictionary D, 15 00:01:04,550 --> 00:01:07,300 initialize it to an empty dictionary, 16 00:01:07,300 --> 00:01:10,370 and I'll loop through every character in placement, 17 00:01:10,370 --> 00:01:14,710 so I'll say for every character C in placement. 18 00:01:14,710 --> 00:01:18,715 If that character is not in our dictionary, 19 00:01:18,715 --> 00:01:21,440 so if C is not in D, 20 00:01:21,440 --> 00:01:26,140 then initialize D sub C to be zero, 21 00:01:26,140 --> 00:01:34,795 and then regardless we're going to say D sub C equals its previous value, plus one. 22 00:01:34,795 --> 00:01:38,990 Now by the time we get to line nine here, 23 00:01:38,990 --> 00:01:42,169 we've accomplished this first part of the question, 24 00:01:42,169 --> 00:01:44,150 so we have a dictionary D, 25 00:01:44,150 --> 00:01:47,210 where every character is associated with 26 00:01:47,210 --> 00:01:50,735 the number of times that that character appears in placement. 27 00:01:50,735 --> 00:01:53,900 Now let's do the second part where we want to find 28 00:01:53,900 --> 00:01:56,780 the key with the lowest value in this dictionary, 29 00:01:56,780 --> 00:01:59,500 and assign that key to be min value. 30 00:01:59,500 --> 00:02:02,030 So, the first thing that I'm going to do is 31 00:02:02,030 --> 00:02:04,460 I'm actually just going to get a list of keys, 32 00:02:04,460 --> 00:02:10,670 so I'm going to say, keys equals list of D.keys. 33 00:02:10,670 --> 00:02:20,509 So, keys is going to be a list and every item in that list is going to be a character, 34 00:02:20,509 --> 00:02:23,855 so we might have one item is the character A, 35 00:02:23,855 --> 00:02:26,210 one item has the character E, 36 00:02:26,210 --> 00:02:28,050 one item has the character C, 37 00:02:28,050 --> 00:02:31,960 one might be a space, and so on. 38 00:02:31,970 --> 00:02:37,370 So, Keys is a list of the characters in placement, 39 00:02:37,370 --> 00:02:42,065 or a list of all of the keys in our dictionary D, 40 00:02:42,065 --> 00:02:49,115 and now we want to get the key that has the lowest value in our dictionary D. So, 41 00:02:49,115 --> 00:02:52,055 we're going to use max value accumulation, 42 00:02:52,055 --> 00:02:54,965 in this case we actually are searching for the minimum value, 43 00:02:54,965 --> 00:02:56,705 but it's the same concept. 44 00:02:56,705 --> 00:03:03,545 So, I'm going to initialize min value to be keys sub zero, 45 00:03:03,545 --> 00:03:07,340 I'm just going to do this arbitrarily to give us a starting point, 46 00:03:07,340 --> 00:03:12,769 so in other words min value is going to start out as the first key in our list of keys, 47 00:03:12,769 --> 00:03:16,190 so it might start out as something like the character A. 48 00:03:16,190 --> 00:03:22,040 Then we're going to search for any key that has a lower value associated with it, 49 00:03:22,040 --> 00:03:26,135 so I'll say for every key in keys, 50 00:03:26,135 --> 00:03:30,275 now ask is the value associated with this key? 51 00:03:30,275 --> 00:03:38,585 So, if D sub key is less than the value associated with min value, 52 00:03:38,585 --> 00:03:43,620 so if D sub key is less than D sub min value, 53 00:03:43,750 --> 00:03:49,355 then we say our new lowest key is this new key, 54 00:03:49,355 --> 00:03:55,200 so I'll say min value equals key. 55 00:03:56,720 --> 00:04:01,850 So, again we kind of have two parts in this question, 56 00:04:01,850 --> 00:04:05,935 the code from line three, through eight, 57 00:04:05,935 --> 00:04:11,345 addresses the first part where we create a dictionary with character frequencies, 58 00:04:11,345 --> 00:04:15,289 and then lines ten, through 15, 59 00:04:15,289 --> 00:04:19,760 address the second part of finding the key with the lowest value. 60 00:04:19,760 --> 00:04:22,820 We use information from this first part, 61 00:04:22,820 --> 00:04:26,490 in other words we use this dictionary D in the second part, 62 00:04:26,490 --> 00:04:32,215 but we kind of apply the accumulation pattern two different times to solve this problem. 63 00:04:32,215 --> 00:04:34,260 So, when I save and run my code, 64 00:04:34,260 --> 00:04:37,680 you can see that I get the correct values. 65 00:04:39,500 --> 00:04:43,790 So, in this question we're asked to create a dictionary called Let 66 00:04:43,790 --> 00:04:47,540 D that keeps track of all of the characters in the string product, 67 00:04:47,540 --> 00:04:50,695 and notes how many times each character was seen. 68 00:04:50,695 --> 00:04:55,160 And then, we find the key with the highest value in that dictionary, 69 00:04:55,160 --> 00:04:57,305 and assign that to be max value. 70 00:04:57,305 --> 00:05:02,270 So, again here we have kind of two different parts, 71 00:05:02,270 --> 00:05:05,120 the first part uses dictionary accumulation, 72 00:05:05,120 --> 00:05:14,220 the second part uses max value accumulation. 73 00:05:15,710 --> 00:05:20,464 So, to do the first part we're going to create a dictionary, 74 00:05:20,464 --> 00:05:25,355 let_D, initialize it to an empty dictionary, 75 00:05:25,355 --> 00:05:33,930 then we're going to loop through every character in product, and we're going to say, 76 00:05:33,930 --> 00:05:38,265 if that character is not in let D, 77 00:05:38,265 --> 00:05:43,590 then let D sub C is initialized to zero, 78 00:05:43,590 --> 00:05:52,870 then we say let D sub C equals its previous value, plus one. 79 00:05:54,170 --> 00:05:59,840 So, if I print out the value of let D at this point, 80 00:05:59,840 --> 00:06:02,060 then I should get a dictionary where keys are 81 00:06:02,060 --> 00:06:05,525 associated with the number of times that that character appears. 82 00:06:05,525 --> 00:06:09,245 Now, we want to find the key with the highest value in this dictionary. 83 00:06:09,245 --> 00:06:11,710 So, in this case, it's going to be N, 84 00:06:11,710 --> 00:06:19,140 so I'm going to get a list of keys I'll say keys equals list of let D.keys, 85 00:06:19,720 --> 00:06:24,900 and for every key in keys, 86 00:06:26,030 --> 00:06:32,580 actually first I'm going to say max value equals the first key, 87 00:06:33,880 --> 00:06:36,830 and for every key in keys, 88 00:06:36,830 --> 00:06:39,560 if the value associated with that key, 89 00:06:39,560 --> 00:06:49,575 so if let D sub key is greater than let D sub max value, 90 00:06:49,575 --> 00:06:52,255 then I've seen a new largest value, 91 00:06:52,255 --> 00:06:56,190 so I'll say max value equals key. 92 00:06:56,570 --> 00:06:59,605 Now, when I run this code, 93 00:06:59,605 --> 00:07:04,670 and I should see that I pass every test in max value ends up being N, 94 00:07:04,670 --> 00:07:06,455 which was the correct answer. 95 00:07:06,455 --> 00:07:09,750 That's all for now until next time.