1 00:00:00,790 --> 00:00:03,190 The following content is provided under a Creative 2 00:00:03,190 --> 00:00:04,730 Commons license. 3 00:00:04,730 --> 00:00:07,030 Your support will help MIT OpenCourseWare 4 00:00:07,030 --> 00:00:11,390 continue to offer high-quality educational resources for free. 5 00:00:11,390 --> 00:00:13,990 To make a donation or view additional materials 6 00:00:13,990 --> 00:00:17,860 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,860 --> 00:00:18,820 at owc.mit.edu. 8 00:00:23,430 --> 00:00:27,620 ANA BELL: So we have a list L, originally, 9 00:00:27,620 --> 00:00:29,050 [life, answer, 42, 0]. 10 00:00:34,160 --> 00:00:37,680 In this example, if you don't know 11 00:00:37,680 --> 00:00:39,900 Hitchhiker's Guide to the Galaxy, 12 00:00:39,900 --> 00:00:43,010 that's why 42 is important. 13 00:00:43,010 --> 00:00:46,380 As a scientist, if I didn't at least one example 14 00:00:46,380 --> 00:00:49,330 with the number 42, I'd probably hear about it. 15 00:00:49,330 --> 00:00:51,570 So here it is. 16 00:00:51,570 --> 00:00:54,120 OK, so we have this original list L, 17 00:00:54,120 --> 00:00:56,040 which contains the string "life", the string 18 00:00:56,040 --> 00:00:59,460 "answer", the number 42, and the number 0. 19 00:00:59,460 --> 00:01:03,060 Here, I'm writing a loop that iterates over every element 20 00:01:03,060 --> 00:01:05,980 directly in L, right? 21 00:01:05,980 --> 00:01:13,780 So originally, thing is going to be string "life", then, thing 22 00:01:13,780 --> 00:01:15,830 is going to be the string "answer", then, 23 00:01:15,830 --> 00:01:18,540 thing is going to be the number 42. 24 00:01:18,540 --> 00:01:22,090 And so what am I doing inside the loop as I'm iterating? 25 00:01:22,090 --> 00:01:27,170 Well, if thing is equal to 0, then I'm going to do this. 26 00:01:27,170 --> 00:01:29,200 And otherwise, I'm going to do this. 27 00:01:29,200 --> 00:01:31,109 So for the first two iterations when 28 00:01:31,109 --> 00:01:32,650 thing is equal to "life" or "answer", 29 00:01:32,650 --> 00:01:34,960 I'm not doing anything. 30 00:01:34,960 --> 00:01:43,300 As soon as I hit 42 though, I'm saying, L, at position 1, 31 00:01:43,300 --> 00:01:46,660 is going to get this value here, OK? 32 00:01:46,660 --> 00:01:50,140 So when I've iterated over this element here, I'm saying L, 33 00:01:50,140 --> 00:01:53,574 at position 1, I'm going to change its value, which says, 34 00:01:53,574 --> 00:01:55,240 well, I'm going to keep "life" the same, 35 00:01:55,240 --> 00:01:58,810 but this is going to be "everything". 36 00:01:58,810 --> 00:02:01,280 And then we're keeping the rest the same, 42 and 0, 37 00:02:01,280 --> 00:02:04,839 because we're only modifying L at position 1. 38 00:02:04,839 --> 00:02:06,380 As soon as we do that, we've finished 39 00:02:06,380 --> 00:02:09,259 with the loop when L was 42. 40 00:02:09,259 --> 00:02:14,790 And then we go through 0. 41 00:02:14,790 --> 00:02:17,880 So now, thing takes on the value 0. 42 00:02:17,880 --> 00:02:21,520 And we say, if thing is equal to 0, well, we're good on that. 43 00:02:21,520 --> 00:02:24,460 L, at position thing, is equal to "universe". 44 00:02:24,460 --> 00:02:26,430 So L, at position 0, is equal to "universe". 45 00:02:26,430 --> 00:02:30,780 So that says, OK, I'm going to replace "life" with "universe". 46 00:02:30,780 --> 00:02:37,070 And then everything else stays the same-- 42 and 0, OK? 47 00:02:37,070 --> 00:02:42,720 So that would be my answer, [universe, everything, 42, 0]. 48 00:02:42,720 --> 00:02:46,060 Great, that's the majority. 49 00:02:46,060 --> 00:02:47,730 Good job.