WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:04.491 [MUSIC PLAYING] 00:00:49.398 --> 00:00:50.440 DAVID J MALAN: All right. 00:00:50.440 --> 00:00:53.560 This is CS50, Harvard University's introduction 00:00:53.560 --> 00:00:55.930 to the intellectual enterprises of computer science 00:00:55.930 --> 00:00:57.610 and the art of programming. 00:00:57.610 --> 00:01:01.840 And ordinarily we would all be here on campus in this beautiful Sanders 00:01:01.840 --> 00:01:02.800 Theater together. 00:01:02.800 --> 00:01:05.560 This, of course, is a little bit different this year, 00:01:05.560 --> 00:01:07.375 for more than one reason. 00:01:07.375 --> 00:01:11.110 But we're here instead in the Loeb Drama Center at Harvard University. 00:01:11.110 --> 00:01:14.350 Thanks to our friends in collaboration with the American Repertory Theater, 00:01:14.350 --> 00:01:19.607 we have this new space, including even such amenities as a prop shop in back, 00:01:19.607 --> 00:01:21.940 where we've been working with an amazingly talented team 00:01:21.940 --> 00:01:25.300 over the course of the summer to prepare for this semester and CS50. 00:01:25.300 --> 00:01:29.540 And so I daresay we'll have some new and improved demonstrations along the way. 00:01:29.540 --> 00:01:32.920 So our thanks to our hosts, the American Repertory Theater. 00:01:32.920 --> 00:01:35.440 Now we wanted to evoke memories, at least, 00:01:35.440 --> 00:01:38.290 or some imagery of the campus itself, particularly 00:01:38.290 --> 00:01:41.300 for the many of you who could not be here in person this semester. 00:01:41.300 --> 00:01:45.040 And so we went into the Harvard Archives, where among their collections 00:01:45.040 --> 00:01:49.330 was this watercolor painting, painted by a Harvard graduate student 00:01:49.330 --> 00:01:52.870 over 200 years ago in the year 1794. 00:01:52.870 --> 00:01:56.230 Jonathan Fisher, who sat in what is now Harvard Square, 00:01:56.230 --> 00:01:59.950 looking in on some of the earliest buildings of Harvard's campus. 00:01:59.950 --> 00:02:03.700 And thanks to technology, we took what is a relatively small watercolor 00:02:03.700 --> 00:02:07.420 that this graduate student painted some 200 years ago, 00:02:07.420 --> 00:02:10.720 and now adorns the stage here in the Loeb Drama Center. 00:02:10.720 --> 00:02:14.320 So if unfamiliar, we have Holden Chapel here at left, 00:02:14.320 --> 00:02:17.920 Hollis Hall to its right, which is one of the undergraduate dormitories 00:02:17.920 --> 00:02:20.920 in Harvard Yard, Harvard Hall, which is one of the classroom 00:02:20.920 --> 00:02:24.070 buildings on campus, and then Massachusetts Hall, 00:02:24.070 --> 00:02:29.720 where both first years and Harvard's president live and work respectively. 00:02:29.720 --> 00:02:31.650 So welcome, then, to CS50. 00:02:31.650 --> 00:02:35.800 And I can say that not quite as long ago, 00:02:35.800 --> 00:02:38.620 but, nonetheless, feels rather long ago, some 20 years ago, 00:02:38.620 --> 00:02:39.910 did I take the same class. 00:02:39.910 --> 00:02:44.530 But as you know, I myself had some trepidation 00:02:44.530 --> 00:02:47.710 when it came to studying CS50, when it came to studying computer science. 00:02:47.710 --> 00:02:49.720 Because it was a very unfamiliar field. 00:02:49.720 --> 00:02:53.650 I had followed a path when I got to college of sticking within my comfort 00:02:53.650 --> 00:02:56.080 zone, studying government early on, thinking I would major 00:02:56.080 --> 00:02:57.430 or concentrate in government. 00:02:57.430 --> 00:03:02.080 And it wasn't until I got up the nerve to shop, that is, sit in 00:03:02.080 --> 00:03:06.730 on this class CS50, that I realized that homework can actually be fun. 00:03:06.730 --> 00:03:10.840 And I found that computer science and CS50 is not about programming per se, 00:03:10.840 --> 00:03:14.660 even though that's how many of us perceive it in high school, 00:03:14.660 --> 00:03:17.075 whether it's us or our classmates taking the class. 00:03:17.075 --> 00:03:18.700 But it really is about problem solving. 00:03:18.700 --> 00:03:21.160 And as such, it's so very applicable not only 00:03:21.160 --> 00:03:23.290 to computer science and the engineering fields, 00:03:23.290 --> 00:03:27.890 but really to the arts, humanities, social sciences, sciences, and beyond. 00:03:27.890 --> 00:03:30.160 And so if you're feeling a little uncomfortable 00:03:30.160 --> 00:03:32.500 with the idea of taking a class like CS50, 00:03:32.500 --> 00:03:36.910 know that almost every year, nearly two thirds of the students who take CS50 00:03:36.910 --> 00:03:40.010 have never taken a computer science course before. 00:03:40.010 --> 00:03:45.220 So if you look up, down, left, right, right now, odds are more than many 00:03:45.220 --> 00:03:49.720 of the classmates joining you here today are in a very similar position. 00:03:49.720 --> 00:03:52.270 You're indeed in very good company. 00:03:52.270 --> 00:03:56.683 And what's ultimately important in CS50 too, we emphasize, as in the syllabus, 00:03:56.683 --> 00:03:59.350 that what ultimately matters in this course is not so much where 00:03:59.350 --> 00:04:01.750 you end up relative to your classmates, but where you end 00:04:01.750 --> 00:04:04.360 up relative to yourself when you began. 00:04:04.360 --> 00:04:07.242 Indeed, taking into account where you currently are, perhaps 00:04:07.242 --> 00:04:08.950 with no prior background, and considering 00:04:08.950 --> 00:04:11.590 where you will be in just three or so months 00:04:11.590 --> 00:04:14.510 is ultimately meant to be the measure of your own success. 00:04:14.510 --> 00:04:19.029 And so toward that end, we'll start off this class programming 00:04:19.029 --> 00:04:20.890 a little something from yesteryear. 00:04:20.890 --> 00:04:23.350 An image here of Super Mario Brothers and the pyramid 00:04:23.350 --> 00:04:24.910 that the character has to ascend. 00:04:24.910 --> 00:04:28.780 We'll recreate a portion of this game, albeit using text-- 00:04:28.780 --> 00:04:32.920 otherwise known as ASCII art-- but we'll do that in just the course's second 00:04:32.920 --> 00:04:33.795 or so week. 00:04:33.795 --> 00:04:35.920 So this will be among the first programs you write. 00:04:35.920 --> 00:04:39.070 And then fast forward just several problem sets or programming 00:04:39.070 --> 00:04:41.950 assignments later, or several weeks later, too, 00:04:41.950 --> 00:04:44.950 and you'll be building what we call CS50 Finance, a web 00:04:44.950 --> 00:04:48.250 application of your very own that runs on the internet, 00:04:48.250 --> 00:04:52.250 pulling down nearly real-time stock quotes from a third party service, 00:04:52.250 --> 00:04:57.220 allowing your own users to log in and register to buy and sell stocks, 00:04:57.220 --> 00:04:59.260 so to speak, using virtual currency. 00:04:59.260 --> 00:05:02.440 So over the course of the class's several months, 00:05:02.440 --> 00:05:05.335 will you go, truly, from building a pyramid in Mario 00:05:05.335 --> 00:05:08.050 to building your very own web application and more, 00:05:08.050 --> 00:05:10.660 followed by the course's capstone experience, which 00:05:10.660 --> 00:05:13.450 will be your very own final project. 00:05:13.450 --> 00:05:15.700 But what exactly is computer science? 00:05:15.700 --> 00:05:19.600 What we thought we would do in this week zero, the very first week of the class, 00:05:19.600 --> 00:05:23.200 is consider exactly what it means to solve problems. 00:05:23.200 --> 00:05:26.080 And let me propose that this is computer science. 00:05:26.080 --> 00:05:28.000 This is problem solving. 00:05:28.000 --> 00:05:30.370 You have some input, which is the problem you care about 00:05:30.370 --> 00:05:33.020 that you want to solve, and you care about the solution to that problem, 00:05:33.020 --> 00:05:34.270 which is the so-called output. 00:05:34.270 --> 00:05:37.660 And in between that input and output is this black box 00:05:37.660 --> 00:05:41.230 of sorts, inside of which is the magic that happens, 00:05:41.230 --> 00:05:45.310 the magic that you'll eventually be able to harness and compel computers 00:05:45.310 --> 00:05:46.600 to solve problems for you. 00:05:46.600 --> 00:05:50.800 Inside of that black box, ultimately, is going to be the code that you write. 00:05:50.800 --> 00:05:53.380 But for us to begin doing that, we all kind of 00:05:53.380 --> 00:05:56.930 need to agree on how we're going to represent these inputs and outputs. 00:05:56.930 --> 00:05:59.755 We all kind of have to speak a common language, so to speak. 00:05:59.755 --> 00:06:02.630 And so we need to agree how these inputs are going to be represented. 00:06:02.630 --> 00:06:05.050 So how might we typically represent information? 00:06:05.050 --> 00:06:08.230 Well, maybe the simplest thing to do at the very first class, whether we're 00:06:08.230 --> 00:06:10.450 online or in person, is to take attendance 00:06:10.450 --> 00:06:13.520 or to count the number of people in the room. 00:06:13.520 --> 00:06:16.340 And so you might do this old school style on your hands 00:06:16.340 --> 00:06:18.730 so as to represent every person in a room 00:06:18.730 --> 00:06:21.100 with just a finger raised on your hands. 00:06:21.100 --> 00:06:23.260 So how we might represent information boils down 00:06:23.260 --> 00:06:25.880 to very simple digits on your hand. 00:06:25.880 --> 00:06:28.768 Of course, you can't count very high with just this hand. 00:06:28.768 --> 00:06:31.060 And there's actually a fancy word for what we're doing, 00:06:31.060 --> 00:06:35.020 old school here, and that's unary notation-- uno, implying one, 00:06:35.020 --> 00:06:36.920 or one finger being up or down. 00:06:36.920 --> 00:06:39.128 And so you can count, it would seem, as high as five. 00:06:39.128 --> 00:06:40.920 And of course, if I bring in a second hand, 00:06:40.920 --> 00:06:43.960 I can go as high as 10, and then things get a little more difficult. 00:06:43.960 --> 00:06:45.940 But it's a system for representing information 00:06:45.940 --> 00:06:49.330 and it's fairly universal, certainly when we're all quite young. 00:06:49.330 --> 00:06:52.960 But you and I tend to use a more useful system. 00:06:52.960 --> 00:06:55.870 Not just digits on the hand, but other sorts of digits. 00:06:55.870 --> 00:06:58.780 Namely, the decimal digits that you and I know. 00:06:58.780 --> 00:07:02.037 So the numbers that are otherwise more technically called base 10-- 00:07:02.037 --> 00:07:04.120 and that's just a fancy way of describing the fact 00:07:04.120 --> 00:07:08.490 that there's 10 digits that you and I as humans really tend to use typically. 00:07:08.490 --> 00:07:10.990 Those digits, of course, are zero through nine, 00:07:10.990 --> 00:07:14.710 and using these several digits, we can pose numbers like zero 00:07:14.710 --> 00:07:18.220 through nine, but also 10 and 11 and 12, and as high up 00:07:18.220 --> 00:07:21.220 as we want to go by using multiple digits still. 00:07:21.220 --> 00:07:26.140 But computers don't really speak the same language as us. 00:07:26.140 --> 00:07:29.570 They're, in some sense, much simpler than we humans, 00:07:29.570 --> 00:07:33.760 even though they seem so complicated or so sophisticated and certainly so fast. 00:07:33.760 --> 00:07:36.430 At the end of the day, these are all human-made devices, 00:07:36.430 --> 00:07:39.230 and they're relatively simple at their core. 00:07:39.230 --> 00:07:41.770 In fact, even if you don't quite know what you're saying, 00:07:41.770 --> 00:07:43.870 but you've at least heard this to be the case, 00:07:43.870 --> 00:07:47.920 what language do you understand computers to speak? 00:07:47.920 --> 00:07:52.120 What language do computers speak, if not the system that you and I use 00:07:52.120 --> 00:07:54.520 of zeros through nines or decimal? 00:07:54.520 --> 00:07:57.280 Brian, could we see who might answer this? 00:07:57.280 --> 00:08:00.340 What system do computers use, so far as you've heard, 00:08:00.340 --> 00:08:02.800 whether or not you've taken a CS class before? 00:08:02.800 --> 00:08:04.305 Keith, can we go to you first? 00:08:04.305 --> 00:08:04.930 AUDIENCE: Yeah. 00:08:04.930 --> 00:08:06.263 The computers use binary. 00:08:06.263 --> 00:08:07.180 DAVID J MALAN: Binary. 00:08:07.180 --> 00:08:08.638 And can you elaborate a little bit? 00:08:08.638 --> 00:08:10.090 What do you mean by binary? 00:08:10.090 --> 00:08:11.380 AUDIENCE: It's zeros and ones. 00:08:11.380 --> 00:08:13.798 So, like, while we use zero through nine for base 10, 00:08:13.798 --> 00:08:15.340 they use zero through one for base 2. 00:08:15.340 --> 00:08:16.548 DAVID J MALAN: Yeah, exactly. 00:08:16.548 --> 00:08:19.630 So computers use the so-called binary system, bi implying two. 00:08:19.630 --> 00:08:23.140 And they, indeed, only use, as Keith notes, zero and one, two digits. 00:08:23.140 --> 00:08:25.480 So on the one hand, this is actually pretty encouraging, 00:08:25.480 --> 00:08:28.420 because, wow, this is actually a pretty simple system if we're only 00:08:28.420 --> 00:08:29.830 using two of these digits. 00:08:29.830 --> 00:08:33.370 But, of course, if you only have two digits, 00:08:33.370 --> 00:08:37.120 how are we going to represent the number two or three or four 00:08:37.120 --> 00:08:38.830 or any much larger number? 00:08:38.830 --> 00:08:40.970 It would almost seem like a step backwards. 00:08:40.970 --> 00:08:41.980 But it isn't actually. 00:08:41.980 --> 00:08:44.710 And it turns out that this so-called system, or base 2-- 00:08:44.710 --> 00:08:47.560 two because there's two digits in the vocabulary, otherwise known, 00:08:47.560 --> 00:08:49.000 as Keith says, as binary-- 00:08:49.000 --> 00:08:52.600 uses just zeros and ones, and it turns out there's other nomenclature here 00:08:52.600 --> 00:08:53.500 we can toss out. 00:08:53.500 --> 00:08:56.080 These zeros and ones are otherwise known as bits. 00:08:56.080 --> 00:09:00.070 And bits actually derive from just two words, binary digits. 00:09:00.070 --> 00:09:02.595 Binary, implying two possibilities, digits, 00:09:02.595 --> 00:09:03.970 just being symbols on the screen. 00:09:03.970 --> 00:09:05.845 So binary digits, or otherwise known as bits. 00:09:05.845 --> 00:09:09.220 And computers speak binary using these things called bits. 00:09:09.220 --> 00:09:11.350 But what does that mean and why is it the case? 00:09:11.350 --> 00:09:13.480 Like, why didn't they invent computers decades ago 00:09:13.480 --> 00:09:15.580 that just use zero through nine, rather than 00:09:15.580 --> 00:09:19.330 come up with a whole new system for us to think about, let alone talk about? 00:09:19.330 --> 00:09:22.480 Well, at the end of the day, computers are using what as their input? 00:09:22.480 --> 00:09:24.100 Really just electricity. 00:09:24.100 --> 00:09:28.030 Probably the only thing all of us do every day or every couple of days 00:09:28.030 --> 00:09:32.560 with our laptop or desktop or phone is either make sure it's still plugged in, 00:09:32.560 --> 00:09:34.780 or to plug it in so as to charge it. 00:09:34.780 --> 00:09:36.760 So the only physical input to our devices 00:09:36.760 --> 00:09:38.642 these days is electricity in some form. 00:09:38.642 --> 00:09:41.350 And we don't have to get into the nuances of what electricity is, 00:09:41.350 --> 00:09:45.610 but I think it's about electrons flowing into the device so as to charge it. 00:09:45.610 --> 00:09:48.310 So it suffices for our purposes to know that there's 00:09:48.310 --> 00:09:51.790 some physical input to the device, these computers and phones that we use. 00:09:51.790 --> 00:09:52.820 But that's it. 00:09:52.820 --> 00:09:56.410 And so if we harness this electricity, maybe we 00:09:56.410 --> 00:09:58.510 can start to represent information with it. 00:09:58.510 --> 00:10:01.900 For instance, here is a light bulb, this old ghost light in the theater here 00:10:01.900 --> 00:10:03.280 that's currently off. 00:10:03.280 --> 00:10:05.320 But it has the ability to turn on. 00:10:05.320 --> 00:10:08.750 We just need to plug it in or throw on a switch. 00:10:08.750 --> 00:10:11.470 And if that's the case, what's really quite 00:10:11.470 --> 00:10:14.013 compelling about the metaphor of using lights 00:10:14.013 --> 00:10:16.180 is that right now, this light bulb is currently off, 00:10:16.180 --> 00:10:18.100 but as soon as I allow electricity to flow 00:10:18.100 --> 00:10:22.510 as by plugging it in or maybe throwing a switch, now it's, of course, on. 00:10:22.510 --> 00:10:25.570 And if I unplug it or throw the switch again, it's off. 00:10:25.570 --> 00:10:28.150 Or if I plug it back in, it's on. 00:10:28.150 --> 00:10:31.000 And the implication of this very simple idea 00:10:31.000 --> 00:10:34.570 is that we can take a physical device, like a single light bulb, 00:10:34.570 --> 00:10:38.110 and by plugging it in or unplugging it, we can represent information. 00:10:38.110 --> 00:10:39.170 What did I just do? 00:10:39.170 --> 00:10:41.650 I represented the light bulb being off or on, 00:10:41.650 --> 00:10:43.610 but we can just call off and on something else. 00:10:43.610 --> 00:10:46.250 We can call them zeros and ones. 00:10:46.250 --> 00:10:48.290 And so this really is the germ of an idea 00:10:48.290 --> 00:10:52.550 that gave us computers, and with it, their use of the binary system. 00:10:52.550 --> 00:10:55.850 If, at the end of the day, all they have is physical input as electricity, 00:10:55.850 --> 00:10:59.800 well, let's just use that to harness and keep track of information. 00:10:59.800 --> 00:11:03.240 Let's store a little bit of electricity when we want to represent a one, 00:11:03.240 --> 00:11:05.870 and let's let go of that electricity in some sense 00:11:05.870 --> 00:11:09.060 when we want to represent a zero instead. 00:11:09.060 --> 00:11:12.020 And so because the input to computers is so simple, 00:11:12.020 --> 00:11:15.620 thus gives us the zeros and ones that we now use. 00:11:15.620 --> 00:11:18.050 But we seem to have created a problem for ourselves. 00:11:18.050 --> 00:11:22.400 If we only have one light bulb or one switch, if it's off, 00:11:22.400 --> 00:11:25.760 it might be zero, if it's on, it might be a one, 00:11:25.760 --> 00:11:28.220 but how do I count higher than one? 00:11:28.220 --> 00:11:30.380 That problem still fundamentally remains. 00:11:30.380 --> 00:11:32.850 Well, I could, of course, use more light bulbs. 00:11:32.850 --> 00:11:33.920 So let me ask this. 00:11:33.920 --> 00:11:38.180 If we were to use three light bulbs, how high could we count? 00:11:38.180 --> 00:11:42.740 So with one light bulb, we can count from zero to one, two possibilities. 00:11:42.740 --> 00:11:45.170 But with three light bulbs, how high could we count? 00:11:45.170 --> 00:11:47.780 Let me go ahead and ask this question here on the screen. 00:11:47.780 --> 00:11:52.730 In just a moment you'll see on your side this particular question via which 00:11:52.730 --> 00:11:56.150 you can respond on your device. 00:11:56.150 --> 00:11:59.820 How high can you count with three light bulbs? 00:11:59.820 --> 00:12:05.130 So instead of one, I give you three, each of which can be on or off. 00:12:05.130 --> 00:12:07.700 How high can we, perhaps, count? 00:12:07.700 --> 00:12:11.010 So you'll see on the screen here the answers coming in. 00:12:11.010 --> 00:12:14.360 We have a lot of folks thinking, 60-plus percent that it's 00:12:14.360 --> 00:12:16.523 eight is the highest you can count. 00:12:16.523 --> 00:12:18.440 A lot of you think it's seven, and some of you 00:12:18.440 --> 00:12:20.960 also think it might be three or two. 00:12:20.960 --> 00:12:23.790 So that's actually an interesting range of answers. 00:12:23.790 --> 00:12:25.980 And let's see what might actually be the case. 00:12:25.980 --> 00:12:29.990 Well, let me cut back over to three actual light bulbs here, all of which 00:12:29.990 --> 00:12:30.680 are off. 00:12:30.680 --> 00:12:33.620 And most naively, I think, if we were to turn these light bulbs on, 00:12:33.620 --> 00:12:36.680 if they currently represent zero, obviously, I could turn one on 00:12:36.680 --> 00:12:37.760 and we could call it one. 00:12:37.760 --> 00:12:41.990 Then I could turn the second one on and call it two, turn on the third one, 00:12:41.990 --> 00:12:44.963 and now with all three on, we could say now we're representing three. 00:12:44.963 --> 00:12:47.630 But we're not really being clever enough just yet, if we're only 00:12:47.630 --> 00:12:49.430 counting as high as three. 00:12:49.430 --> 00:12:52.340 Because I'm just turning them on, in this story, left to right. 00:12:52.340 --> 00:12:54.230 But what if we were a little more clever? 00:12:54.230 --> 00:12:57.740 Maybe we turn them on right to left, or maybe we kind of permuted them 00:12:57.740 --> 00:12:58.940 in different directions? 00:12:58.940 --> 00:13:01.790 That is, we took into account not just how many bulbs are on 00:13:01.790 --> 00:13:06.350 or how many fingers are in the air, but rather the pattern of on 00:13:06.350 --> 00:13:08.523 and off light bulbs that we've created. 00:13:08.523 --> 00:13:09.690 So let's just count this up. 00:13:09.690 --> 00:13:12.680 So let me somewhat systematically turn some of these bulbs on here, 00:13:12.680 --> 00:13:14.000 albeit virtually. 00:13:14.000 --> 00:13:18.147 Here might be one, here might be two, here might be three. 00:13:18.147 --> 00:13:19.980 But then we're kind of done with that story. 00:13:19.980 --> 00:13:21.570 So how might we do it a little better? 00:13:21.570 --> 00:13:23.060 Well, let's start again at zero. 00:13:23.060 --> 00:13:24.530 Here might be one. 00:13:24.530 --> 00:13:26.480 Why don't we call this two? 00:13:26.480 --> 00:13:28.670 Why don't we call this three? 00:13:28.670 --> 00:13:30.770 Why don't we call this four? 00:13:30.770 --> 00:13:35.180 Call this five, this six, and this seven. 00:13:35.180 --> 00:13:38.030 Now, it's fine if you didn't quite see what pattern I was following, 00:13:38.030 --> 00:13:42.800 but take my word for it that that was a unique pattern of light bulbs, 00:13:42.800 --> 00:13:44.300 eight total times. 00:13:44.300 --> 00:13:47.495 I started at off, off, off, and I ended at on, on, on. 00:13:47.495 --> 00:13:49.370 But along the way, there were, indeed, eight. 00:13:49.370 --> 00:13:50.573 But how high can I count? 00:13:50.573 --> 00:13:53.240 Well, it kind of depends on what number you start counting from, 00:13:53.240 --> 00:13:57.560 and just as we thus far have been doing, computer scientists do all the time. 00:13:57.560 --> 00:13:59.900 Computer scientists and, in turn, computer programs, 00:13:59.900 --> 00:14:03.020 typically start counting from zero, just because it makes sense 00:14:03.020 --> 00:14:05.900 because when everything is off, you might as well call that zero. 00:14:05.900 --> 00:14:09.800 So if we start counting at zero, and we have eight possible patterns 00:14:09.800 --> 00:14:12.320 that we just saw pictorially, well, that would 00:14:12.320 --> 00:14:14.330 allow us to count as high as seven. 00:14:14.330 --> 00:14:17.450 So from zero to seven, so seven is the highest 00:14:17.450 --> 00:14:19.320 we can count with three light bulbs. 00:14:19.320 --> 00:14:22.760 So those of you who propose that seven was the answer, 36% of you 00:14:22.760 --> 00:14:24.410 were indeed correct. 00:14:24.410 --> 00:14:29.510 57% of you who said eight are correct if you assume we start counting at one, 00:14:29.510 --> 00:14:30.360 and that's fine. 00:14:30.360 --> 00:14:33.388 But at least in the computing world now, we'll generally, by convention, 00:14:33.388 --> 00:14:34.430 start counting from zero. 00:14:34.430 --> 00:14:37.660 But you are correct to say that there's eight such possibilities. 00:14:37.660 --> 00:14:39.410 All right, well, this is all fine and good 00:14:39.410 --> 00:14:41.460 to represent things with patterns of light bulbs. 00:14:41.460 --> 00:14:44.270 But how do we actually now get to the zeros and ones 00:14:44.270 --> 00:14:45.920 that a computer is actually using? 00:14:45.920 --> 00:14:48.650 Because what's inside of a computer, at the end of the day, 00:14:48.650 --> 00:14:52.070 are not light bulbs but tiny, tiny little switches, 00:14:52.070 --> 00:14:57.320 millions of little switches that can either be on, or one, or off, or zero. 00:14:57.320 --> 00:14:59.780 Those switches happen to be called transistors. 00:14:59.780 --> 00:15:02.348 And these days computers do have millions 00:15:02.348 --> 00:15:04.890 of these things that can be on and off in different patterns. 00:15:04.890 --> 00:15:08.030 So if you have the ability to turn on and off all of these switches, well, 00:15:08.030 --> 00:15:14.000 what can we all agree on representation when it comes to using those switches? 00:15:14.000 --> 00:15:16.250 How will we represent information with them? 00:15:16.250 --> 00:15:19.700 Well, wonderfully, we don't really need to think very hard 00:15:19.700 --> 00:15:22.700 or go past our very comfortable roots as kids. 00:15:22.700 --> 00:15:25.320 If we consider for a moment not just zero and one, 00:15:25.320 --> 00:15:27.320 but the whole decimal system, zero through nine, 00:15:27.320 --> 00:15:30.000 that you and I all started our day with today. 00:15:30.000 --> 00:15:31.310 How does that system work? 00:15:31.310 --> 00:15:34.340 Well, here on the screen is 123. 00:15:34.340 --> 00:15:37.130 So yes, you're probably thinking that's one hundred twenty three, 00:15:37.130 --> 00:15:38.180 but not quite. 00:15:38.180 --> 00:15:40.280 All I've shown on the screen is a pattern 00:15:40.280 --> 00:15:43.010 of symbols, 123, or three digits. 00:15:43.010 --> 00:15:45.817 And all of us probably are instinctively just saying, obviously, 00:15:45.817 --> 00:15:47.150 it's a hundred and twenty three. 00:15:47.150 --> 00:15:49.192 But it's probably been years since you considered 00:15:49.192 --> 00:15:51.340 why it is one hundred twenty three. 00:15:51.340 --> 00:15:55.180 Well, let's consider what each of these digits or symbols represents. 00:15:55.180 --> 00:15:58.600 If you're like me, you grew up learning that the rightmost digit is the ones 00:15:58.600 --> 00:16:03.230 place, the middle is the tens place, the left one is the hundreds place. 00:16:03.230 --> 00:16:06.250 And so how do we get from these three symbols or digits, 00:16:06.250 --> 00:16:09.970 123, to the mathematical idea we know as one hundred twenty three? 00:16:09.970 --> 00:16:14.860 Well, all of us instantaneously, these days, did 100 times 1 plus 10 times 00:16:14.860 --> 00:16:19.690 2 plus 1 times 3, which, of course, is just 100 plus 20 plus 3, 00:16:19.690 --> 00:16:23.290 or the mathematical value we all know as one hundred twenty three. 00:16:23.290 --> 00:16:26.380 So a bit of a circular argument, but just to remind us 00:16:26.380 --> 00:16:30.280 how we got from 123 to one hundred twenty three. 00:16:30.280 --> 00:16:34.300 Well, it turns out that in the world of computers, the system 00:16:34.300 --> 00:16:38.240 they use is exactly, fundamentally the same. 00:16:38.240 --> 00:16:41.680 The only difference is that computers only have access to zeros and ones, 00:16:41.680 --> 00:16:43.150 not zeros through nines. 00:16:43.150 --> 00:16:46.990 So if we consider now in the abstract, just three possible digits 00:16:46.990 --> 00:16:51.610 represented here, let's consider for a moment why those columns are places 00:16:51.610 --> 00:16:54.923 where one, 10, and 100, and so forth. 00:16:54.923 --> 00:16:56.090 Well, why was that the case? 00:16:56.090 --> 00:16:58.048 Well, there was a pattern, in fact, and it just 00:16:58.048 --> 00:16:59.900 has to do with exponents or powers. 00:16:59.900 --> 00:17:03.490 So the rightmost column, technically, if we really get into the weeds, 00:17:03.490 --> 00:17:06.069 is 10 to the zeroth power, which if you recall, 00:17:06.069 --> 00:17:09.410 just means one, 10 to the first power, which is just 10, 00:17:09.410 --> 00:17:12.766 and 10 to the second power, or 10 squared, is 100. 00:17:12.766 --> 00:17:15.099 But what's interesting about representing it in this way 00:17:15.099 --> 00:17:18.339 is that it jumps out that 10 is involved. 00:17:18.339 --> 00:17:24.230 There's 10 digits, zero through nine, so the columns are using this base of 10. 00:17:24.230 --> 00:17:26.230 So you can perhaps now get even ahead of me 00:17:26.230 --> 00:17:29.380 here by considering, well, if in the binary system that computers 00:17:29.380 --> 00:17:31.810 use, you only have two digits, zeros and ones, 00:17:31.810 --> 00:17:35.990 odds are the only thing that's going to change is the meaning of these columns. 00:17:35.990 --> 00:17:39.880 Now we have the ones place still, because 2 to the zero is one, 00:17:39.880 --> 00:17:43.278 but then we have 2 to the first, 2 to the second, and so forth. 00:17:43.278 --> 00:17:45.070 And of course, if we just do out that math, 00:17:45.070 --> 00:17:47.500 in the world of binary that computers use, 00:17:47.500 --> 00:17:52.400 we have the ones place, twos place, fours place, and so forth. 00:17:52.400 --> 00:17:53.980 And now we're good to go. 00:17:53.980 --> 00:17:57.340 Even though we have to now think in a different base system, 00:17:57.340 --> 00:17:59.200 now we can start counting more properly. 00:17:59.200 --> 00:18:01.840 And now we can move away from the metaphor of light bulbs 00:18:01.840 --> 00:18:04.660 and consider that if all of those light bulbs are off, 00:18:04.660 --> 00:18:07.720 we're, again, just going to start thinking of those things as zeros. 00:18:07.720 --> 00:18:12.040 So that would be a pattern of symbols or digits that's 000 in binary. 00:18:12.040 --> 00:18:14.580 But in our human world, the mental math you 00:18:14.580 --> 00:18:17.380 would probably do now instantaneously after today 00:18:17.380 --> 00:18:22.030 would be, well, that's obviously 4 times 0 plus 2 times 0 plus 1 times 0, 00:18:22.030 --> 00:18:24.730 or, of course, zero in decimal. 00:18:24.730 --> 00:18:28.540 But how does a computer represent the number one, for instance? 00:18:28.540 --> 00:18:33.430 Well, it's just going to change that rightmost bit from a zero to a one, 00:18:33.430 --> 00:18:37.270 or, more metaphorically, it's going to turn that switch on and illuminate 00:18:37.270 --> 00:18:40.060 that rightmost light bulb just like I did earlier. 00:18:40.060 --> 00:18:41.560 How do I represent two? 00:18:41.560 --> 00:18:44.140 It's going to be 010 in binary. 00:18:44.140 --> 00:18:45.700 How do I represent three? 00:18:45.700 --> 00:18:47.270 This is where we're about to differ. 00:18:47.270 --> 00:18:49.648 Now I'm putting on two of those switches, 00:18:49.648 --> 00:18:52.690 because I need something in the twos place and the ones place to give me, 00:18:52.690 --> 00:18:54.340 mathematically, three. 00:18:54.340 --> 00:18:57.250 Next, if we go ahead and choose-- 00:18:57.250 --> 00:19:00.220 count up to four, that's going to be 100. 00:19:00.220 --> 00:19:02.950 if I want to count up to five, that's going to be 101, 00:19:02.950 --> 00:19:10.840 six is going to be 110, and finally, the number seven is going to be 111. 00:19:10.840 --> 00:19:14.850 So it would seem that using three bits, each of which can be a zero or one, 00:19:14.850 --> 00:19:17.980 yes, you can permute them in eight different ways. 00:19:17.980 --> 00:19:20.950 Two possibilities for the first times two for the second times two 00:19:20.950 --> 00:19:22.360 for the third gives us eight. 00:19:22.360 --> 00:19:26.110 But as per this math and the intuition of starting counting from zero, 00:19:26.110 --> 00:19:30.560 we can only count up as high as seven in total. 00:19:30.560 --> 00:19:33.100 Well, let's go ahead and actually take this out for a spin. 00:19:33.100 --> 00:19:38.875 When we don't have just, say, let's say, one light bulb or three light bulbs, 00:19:38.875 --> 00:19:40.750 we have, actually, the fortune of having like 00:19:40.750 --> 00:19:42.350 a whole stage worth of light bulbs. 00:19:42.350 --> 00:19:46.060 64 light bulbs adorn the stage here. 00:19:46.060 --> 00:19:47.050 And you know what? 00:19:47.050 --> 00:19:51.170 Sumner, could we go ahead and put up a random number on the screen here? 00:19:51.170 --> 00:19:54.490 All right, so if you can see these light bulbs from your perspective, 00:19:54.490 --> 00:19:57.640 we have eight light bulbs plus another bunch of them, 00:19:57.640 --> 00:19:59.420 and all the others are off. 00:19:59.420 --> 00:20:01.660 So let's go ahead and ask a question then. 00:20:01.660 --> 00:20:07.720 If these light bulbs now represent not just one light bulb or two or three, 00:20:07.720 --> 00:20:10.750 but several more-- in this case, at least six light bulbs, 00:20:10.750 --> 00:20:12.400 what value do we actually get? 00:20:12.400 --> 00:20:15.260 Well, let me go ahead and put a question on the screen here, 00:20:15.260 --> 00:20:18.080 which should pop up on yours in just a moment. 00:20:18.080 --> 00:20:22.010 And you should see now on your end this same question. 00:20:22.010 --> 00:20:26.260 Put in binary terms, what number-- 00:20:26.260 --> 00:20:33.790 in decimal, does binary number 110010 represents? 00:20:33.790 --> 00:20:40.460 What decimal number does binary number 110010 represent, from left to right? 00:20:40.460 --> 00:20:42.710 So here we have an overwhelming response. 00:20:42.710 --> 00:20:45.300 50 is indeed the correct answer. 00:20:45.300 --> 00:20:46.030 Now why is that? 00:20:46.030 --> 00:20:48.450 Well, if I go over to the physical light bulbs here, let's 00:20:48.450 --> 00:20:51.210 just consider for a moment what the pattern actually is. 00:20:51.210 --> 00:20:58.110 This here is the ones place, the twos place, four, eight, 16, 32, 00:20:58.110 --> 00:20:59.110 and we could keep going. 00:20:59.110 --> 00:21:01.318 But it's not going to matter because they're all off. 00:21:01.318 --> 00:21:05.250 So we have 32 plus 16 plus 2, which indeed gives us 00:21:05.250 --> 00:21:07.770 the number you and I know in decimal as 50. 00:21:07.770 --> 00:21:11.250 And just imagine how high we could count with all of the other light bulbs 00:21:11.250 --> 00:21:12.700 as well. 00:21:12.700 --> 00:21:13.230 All right. 00:21:13.230 --> 00:21:15.450 So we started with the story with electricity. 00:21:15.450 --> 00:21:18.090 We then moved on to numbers and representing things, 00:21:18.090 --> 00:21:19.950 either in decimal or in binary. 00:21:19.950 --> 00:21:22.050 But we've kind of painted ourselves into a corner, 00:21:22.050 --> 00:21:26.160 because if we only have at our disposal switches or the metaphorical light 00:21:26.160 --> 00:21:28.830 bulbs, which we can think of as zeros and ones, 00:21:28.830 --> 00:21:32.280 it would seem that the only things computers can do is compute. 00:21:32.280 --> 00:21:33.930 That is, behave as calculators. 00:21:33.930 --> 00:21:37.590 And in fact, early on, that's exactly what computers were designed to do, 00:21:37.590 --> 00:21:41.220 was really facilitate mathematical calculations that were otherwise quite 00:21:41.220 --> 00:21:43.110 tedious or impossible for humans. 00:21:43.110 --> 00:21:45.390 But, of course, what you and I are using right now, 00:21:45.390 --> 00:21:49.290 what we use every day on our phones and our laptops and desktops is much more 00:21:49.290 --> 00:21:50.140 sophisticated. 00:21:50.140 --> 00:21:53.940 So let's consider how could a computer represent not just numbers, 00:21:53.940 --> 00:21:56.700 but letters of the alphabet. 00:21:56.700 --> 00:21:58.620 Brian, could we call on someone for this one? 00:21:58.620 --> 00:22:00.810 If you'd like to raise your virtual hand, 00:22:00.810 --> 00:22:03.870 how could a computer go about representing letters of an alphabet 00:22:03.870 --> 00:22:10.190 like English if, again, all we have at our disposal is switches? 00:22:10.190 --> 00:22:13.550 AUDIENCE: We can assign the numbers that we're getting from binary 00:22:13.550 --> 00:22:15.777 to specific letters of the alphabet. 00:22:15.777 --> 00:22:16.610 DAVID J MALAN: Yeah. 00:22:16.610 --> 00:22:20.047 We can assign the specific numbers in binary to letters of the alphabet. 00:22:20.047 --> 00:22:22.130 That's pretty much our only option, it would seem. 00:22:22.130 --> 00:22:26.060 If we only have the ability to permute these switches or light bulbs or bits, 00:22:26.060 --> 00:22:29.743 well, we just all have to agree how to represent letters in the same way. 00:22:29.743 --> 00:22:32.660 Now, maybe the simplest way for us to do this would be, you know what? 00:22:32.660 --> 00:22:36.230 Let's just all agree that a capital A is going to be the number one. 00:22:36.230 --> 00:22:40.280 So you turn on one light bulb, or represent the binary number one. 00:22:40.280 --> 00:22:42.890 Well, how about for B, we could use the number two. 00:22:42.890 --> 00:22:45.950 For C we could use the number three. 00:22:45.950 --> 00:22:47.510 D could be four, and so forth. 00:22:47.510 --> 00:22:51.930 We all just have to agree to number the letters in that way. 00:22:51.930 --> 00:22:54.240 But it turns out humans did exactly that, 00:22:54.240 --> 00:22:56.150 but a little bit differently years ago. 00:22:56.150 --> 00:22:59.450 They decided for reasons that we won't get into just now, 00:22:59.450 --> 00:23:01.730 that, actually, the capital letter A is actually 00:23:01.730 --> 00:23:05.930 going to be represented by the decimal number you and I know as 65. 00:23:05.930 --> 00:23:08.850 Now in bitwise form, that's going to look like this. 00:23:08.850 --> 00:23:11.600 So this is the pattern of bits that a computer would 00:23:11.600 --> 00:23:15.660 use to represent the decimal number we now know as 65, 00:23:15.660 --> 00:23:18.980 and now what the computer is going to do is just be mindful of what 00:23:18.980 --> 00:23:20.730 type of program you're using. 00:23:20.730 --> 00:23:24.380 So yes, if you're using a calculator or maybe using something like Excel 00:23:24.380 --> 00:23:28.190 to crunch numbers, well, in that context when running software, 00:23:28.190 --> 00:23:32.030 like a calculator or a spreadsheet program doing numerical analysis, 00:23:32.030 --> 00:23:36.260 the program is going to see inside of the computer's hardware 00:23:36.260 --> 00:23:40.490 the pattern of switches that represents the decimal number 65. 00:23:40.490 --> 00:23:43.340 And because it's in the context of a calculator or spreadsheet, what 00:23:43.340 --> 00:23:47.750 you, the human, might see on the screen is literally the decimal number 65. 00:23:47.750 --> 00:23:50.873 But if you and I are using text messaging or email or any number 00:23:50.873 --> 00:23:52.790 of social media apps where we're communicating 00:23:52.790 --> 00:23:56.150 not numerically but in letters, say, English letters, 00:23:56.150 --> 00:23:59.360 in that context your computer is going to be smart enough 00:23:59.360 --> 00:24:03.260 to know, well, that same pattern of bits that represents 65, 00:24:03.260 --> 00:24:07.190 in the context of a text message or in an email or the like 00:24:07.190 --> 00:24:11.208 actually represents the capital letter A. So the pattern is the same. 00:24:11.208 --> 00:24:12.500 The representation is the same. 00:24:12.500 --> 00:24:14.270 But the context is what differs. 00:24:14.270 --> 00:24:20.360 And the system that humans came up with years ago that maps 65 to A, 66 to B, 00:24:20.360 --> 00:24:23.570 67 to C, is called ASCII, the American Standard 00:24:23.570 --> 00:24:25.670 Code for Information Interchange. 00:24:25.670 --> 00:24:28.640 And that just means that there is a well-defined mapping that a bunch 00:24:28.640 --> 00:24:33.650 of humans decades ago decided on, in order to map letters of the alphabet-- 00:24:33.650 --> 00:24:34.830 English in this case-- 00:24:34.830 --> 00:24:36.650 to numbers starting with 65. 00:24:36.650 --> 00:24:39.950 And there's a whole mapping, too for punctuation, for lowercase letters, 00:24:39.950 --> 00:24:41.076 and the like. 00:24:41.076 --> 00:24:47.480 So given that, suppose that you did receive a text message containing 00:24:47.480 --> 00:24:52.250 a pattern of bits, or, really, just a sequence of decimal numbers that 00:24:52.250 --> 00:24:53.720 happened to be this. 00:24:53.720 --> 00:24:57.500 72, 73, 33. 00:24:57.500 --> 00:25:02.930 Suppose that you received a text message containing these patterns of numbers. 00:25:02.930 --> 00:25:05.240 72, 73, 33. 00:25:05.240 --> 00:25:07.860 What message might you have just received? 00:25:07.860 --> 00:25:10.520 Let me go ahead and pull up the abbreviated chart here 00:25:10.520 --> 00:25:13.670 to consider exactly what message you've received. 00:25:13.670 --> 00:25:15.710 72, 73, 33. 00:25:15.710 --> 00:25:19.400 And Sumner, could we go ahead and throw this same three-letter word 00:25:19.400 --> 00:25:21.710 on the lights? 00:25:21.710 --> 00:25:24.910 If you'd like to see it in bitwise form, so to speak, 00:25:24.910 --> 00:25:29.870 it will appear here on these light bulbs now as well. 00:25:29.870 --> 00:25:31.910 What pattern does this represent? 00:25:31.910 --> 00:25:33.920 Lanham, can we go to you? 00:25:33.920 --> 00:25:36.563 AUDIENCE: That would be HI with an exclamation point, correct? 00:25:36.563 --> 00:25:39.230 DAVID J MALAN: Yeah so it's indeed HI with an exclamation point. 00:25:39.230 --> 00:25:41.272 And it's probably pretty easy now, in retrospect, 00:25:41.272 --> 00:25:45.230 to glean that, yes, the 72 and the 73 were H and I respectively. 00:25:45.230 --> 00:25:49.070 But Lanham also noted the exclamation point, which isn't in this chart, 00:25:49.070 --> 00:25:51.530 but per the dot dot dots, there is a well-defined mapping 00:25:51.530 --> 00:25:54.690 for all of the letters of the alphabet that we might care about. 00:25:54.690 --> 00:25:57.260 And so HI is perhaps more obvious than the other. 00:25:57.260 --> 00:25:59.098 That 33, we need a bigger chart. 00:25:59.098 --> 00:26:00.890 And so if you actually go on your computers 00:26:00.890 --> 00:26:04.905 now to asciichart.com, asciichart.com, you'll 00:26:04.905 --> 00:26:06.280 see a little something like this. 00:26:06.280 --> 00:26:08.280 Though you can also just google ASCII in general 00:26:08.280 --> 00:26:09.660 and get copies of the same chart. 00:26:09.660 --> 00:26:13.550 You'll see here that H is indeed 72, I is indeed 73, 00:26:13.550 --> 00:26:17.010 but if we look to the left, 33 is, apparently, an exclamation mark. 00:26:17.010 --> 00:26:19.760 And you would only know that by having looked it up or just having 00:26:19.760 --> 00:26:20.718 committed it to memory. 00:26:20.718 --> 00:26:23.630 But the computers you and I use and the phones you and I use just 00:26:23.630 --> 00:26:24.710 know this intrinsically. 00:26:24.710 --> 00:26:26.510 That's, indeed, how they're programmed. 00:26:26.510 --> 00:26:29.870 But it turns out, too, that we should consider 00:26:29.870 --> 00:26:32.420 just how many zeroes and ones we're using now 00:26:32.420 --> 00:26:35.660 to represent the 72, the 73, and the 33. 00:26:35.660 --> 00:26:38.840 So let's look for one last time at the binary representation, which, 00:26:38.840 --> 00:26:42.390 as per the light bulbs, are these patterns of bits here. 00:26:42.390 --> 00:26:45.410 So when you receive a text message from a friend saying HI! 00:26:45.410 --> 00:26:47.930 H-I exclamation point, in all caps, you're 00:26:47.930 --> 00:26:51.470 technically receiving a pattern of bits, some kind of frequency, 00:26:51.470 --> 00:26:54.920 if it's wireless, that represents this pattern of bits. 00:26:54.920 --> 00:26:58.910 And typically, computers these days use eight bits 00:26:58.910 --> 00:27:00.892 to represent each of those characters. 00:27:00.892 --> 00:27:02.600 When ASCII first came out, they typically 00:27:02.600 --> 00:27:06.830 used seven for efficiency reasons, because space was expensive back then. 00:27:06.830 --> 00:27:09.470 But here we used eight, and, indeed, that's now 00:27:09.470 --> 00:27:12.470 the norm when it comes to representing characters in multiples of eight. 00:27:12.470 --> 00:27:15.690 So we have eight bits here, eight bits here, eight bits here, 00:27:15.690 --> 00:27:20.210 which means to receive the message HI! you are sending or receiving 24 bits 00:27:20.210 --> 00:27:20.840 total. 00:27:20.840 --> 00:27:23.300 Now, frankly, bits are not a very useful unit of measure, 00:27:23.300 --> 00:27:24.800 typically, because they're so small. 00:27:24.800 --> 00:27:26.300 Just a zero or a one. 00:27:26.300 --> 00:27:29.330 But each of these patterns of eight bits, actually 00:27:29.330 --> 00:27:32.540 have a vocabulary word, if you will, which is bytes. 00:27:32.540 --> 00:27:35.840 And odds are, all of us have used this term in some context, 00:27:35.840 --> 00:27:39.180 but generally in the context of megabytes or even gigabytes. 00:27:39.180 --> 00:27:41.870 Indeed when you talk about the sizes of your files these days, 00:27:41.870 --> 00:27:46.580 you're speaking in bytes in some form, either million or billion bytes. 00:27:46.580 --> 00:27:51.410 But each of those bytes, quite simply, is a pattern of eight zeros and ones. 00:27:51.410 --> 00:27:55.160 So, in fact, if we have as many as 64 bulbs at our disposal, 00:27:55.160 --> 00:27:56.800 that's 64 divided by 8. 00:27:56.800 --> 00:27:57.800 That's eight characters. 00:27:57.800 --> 00:28:01.310 So it would seem we could spell on this stage, even an eight-letter word-- 00:28:01.310 --> 00:28:03.920 if, Sumner, we could put up a random eight-letter word, 00:28:03.920 --> 00:28:05.720 that we'll keep up now-- 00:28:05.720 --> 00:28:09.230 can you now spell from left to right, your left 00:28:09.230 --> 00:28:14.780 to your right, an eight-letter word using the system known as ASCII. 00:28:14.780 --> 00:28:17.120 But, of course, we're being a little bit biased here, 00:28:17.120 --> 00:28:20.780 as ASCII is the American Standard Code for Information Interchange. 00:28:20.780 --> 00:28:24.320 And on a typical US English keyboard, there's more characters, certainly, 00:28:24.320 --> 00:28:27.410 than uppercase letters, like A through H and I. 00:28:27.410 --> 00:28:30.860 There's also some punctuation and some numbers, but there's also quite a bit 00:28:30.860 --> 00:28:32.070 missing as well. 00:28:32.070 --> 00:28:34.910 And any of you who are elsewhere in the world, odds are, 00:28:34.910 --> 00:28:41.210 would find using a keyboard like this especially limiting or frustrating. 00:28:41.210 --> 00:28:41.840 Why is that? 00:28:41.840 --> 00:28:44.440 What seems to be missing from ASCII? 00:28:44.440 --> 00:28:46.415 What seems to be missing from ASCII? 00:28:46.415 --> 00:28:49.670 Well, let me ask this one other question here. 00:28:49.670 --> 00:28:56.150 If we do use ASCII, and we therefore give ourselves eight bits or one byte, 00:28:56.150 --> 00:29:03.620 how many different characters could we potentially actually display, actually 00:29:03.620 --> 00:29:04.160 represent? 00:29:04.160 --> 00:29:06.350 So on your screen, you should see this question now. 00:29:06.350 --> 00:29:09.930 How many symbols can you represent with eight bits? 00:29:09.930 --> 00:29:14.660 How many symbols can you represent with eight bits? 00:29:14.660 --> 00:29:17.480 And this speaks to, really, at the end of the day, how 00:29:17.480 --> 00:29:20.180 many letters of the alphabet plus punctuation, 00:29:20.180 --> 00:29:23.930 plus uppercase and lowercase, can ASCII, or really, can computers support? 00:29:23.930 --> 00:29:28.080 Well, it looks like 72% or so of you think that the answer is 256. 00:29:28.080 --> 00:29:32.270 And it is indeed the case that you can represent 256 possibilities. 00:29:32.270 --> 00:29:32.780 Why? 00:29:32.780 --> 00:29:34.155 You can actually do out the math. 00:29:34.155 --> 00:29:37.460 If you've got eight bits, each of which can be a zero or a one, 00:29:37.460 --> 00:29:39.720 that means you have two possibilities for the first, 00:29:39.720 --> 00:29:43.280 times two possibilities for the second, times two times two times two. 00:29:43.280 --> 00:29:46.650 That happens to be 2 to the eighth, or 256. 00:29:46.650 --> 00:29:48.560 It's fine if that's not immediately obvious, 00:29:48.560 --> 00:29:52.310 but if you do have eight bits, each of which can be one of two values, 00:29:52.310 --> 00:29:55.650 you can come up with 256 possibilities. 00:29:55.650 --> 00:29:59.510 Those of you who chimed in to say that the answer is 255 in this case, 00:29:59.510 --> 00:30:02.960 are wrong, only because now we're talking about the total number 00:30:02.960 --> 00:30:05.270 of patterns, which is indeed 256. 00:30:05.270 --> 00:30:10.010 But the highest value we could represent with eight bits or eight light bulbs, 00:30:10.010 --> 00:30:13.198 it would seem to be, indeed, 255. 00:30:13.198 --> 00:30:15.990 And that's because of all of the different patterns we can permute. 00:30:15.990 --> 00:30:18.590 But let me open the question to the audience now. 00:30:18.590 --> 00:30:21.590 Why might a US English keyboard be especially limiting, 00:30:21.590 --> 00:30:26.330 and in turn, why is ASCII really not quite appropriate 00:30:26.330 --> 00:30:29.060 when it comes to representing human language, 00:30:29.060 --> 00:30:33.110 even though this is what computers began with years ago? 00:30:33.110 --> 00:30:35.945 What is missing from ASCII? 00:30:35.945 --> 00:30:41.420 Why might 256 total possibilities not be sufficient? 00:30:41.420 --> 00:30:42.440 Kevin, can we go to you? 00:30:42.440 --> 00:30:43.700 AUDIENCE: Sure. 00:30:43.700 --> 00:30:47.310 I mean, for one thing, missing a lot of the accents in other languages. 00:30:47.310 --> 00:30:49.310 But if you just consider, like, Asian languages, 00:30:49.310 --> 00:30:51.442 there are a lot more than 256 characters. 00:30:51.442 --> 00:30:52.400 DAVID J MALAN: Exactly. 00:30:52.400 --> 00:30:55.370 So not only are we missing accented characters 00:30:55.370 --> 00:30:57.230 that you might need in some languages, we're 00:30:57.230 --> 00:30:59.960 also missing the characters that you might need in Asian languages, 00:30:59.960 --> 00:31:01.543 in languages like Arabic and the like. 00:31:01.543 --> 00:31:05.630 There are way more symbols that we humans use to communicate in print 00:31:05.630 --> 00:31:08.565 and electronically than 256. 00:31:08.565 --> 00:31:10.940 English, we can get away with fitting into this keyboard, 00:31:10.940 --> 00:31:13.280 but not once we introduce things like these characters, 00:31:13.280 --> 00:31:15.360 let alone other symbols as well. 00:31:15.360 --> 00:31:18.710 And it turns out there's other things we humans like to say these days 00:31:18.710 --> 00:31:23.390 and express using characters that have come into vogue, which is, namely, 00:31:23.390 --> 00:31:24.020 these things. 00:31:24.020 --> 00:31:26.510 Odds are, probably sometime today you have 00:31:26.510 --> 00:31:30.230 sent or received one of these things here, otherwise known as an emoji. 00:31:30.230 --> 00:31:33.890 Now, even though these emojis look like pictures, they look like images-- 00:31:33.890 --> 00:31:37.760 and they are, technically-- the way they're implemented in computers 00:31:37.760 --> 00:31:40.190 is actually as patterns of zeros and ones. 00:31:40.190 --> 00:31:44.570 These are actually just characters in an alphabet, the emoji alphabet. 00:31:44.570 --> 00:31:47.030 Which is to say there's some pattern of zeros and ones 00:31:47.030 --> 00:31:50.630 that represents each one of these faces, and the many other emojis that 00:31:50.630 --> 00:31:51.860 nowadays exist. 00:31:51.860 --> 00:31:55.640 And this is because the world has transitioned over the years from ASCII, 00:31:55.640 --> 00:31:57.950 which only used seven, and in some sense, 00:31:57.950 --> 00:32:01.250 eight bits total to represent all possible characters, 00:32:01.250 --> 00:32:07.100 to using either eight or 16 or 24 or even 32 . 00:32:07.100 --> 00:32:11.340 Bits nowadays there's a system called Unicode, which humans have come up 00:32:11.340 --> 00:32:17.550 with that support not only English, but also all of the human languages, 00:32:17.550 --> 00:32:24.570 is the aspirational goal, both written in print or electronically is the goal. 00:32:24.570 --> 00:32:28.240 And in addition to that, this is to say we can represent things like this. 00:32:28.240 --> 00:32:30.810 So this is the so-called face with tears of joy. 00:32:30.810 --> 00:32:32.940 And this face of tears of joy, as of last year, 00:32:32.940 --> 00:32:37.240 was the most popular emoji sent via text messages, emails, 00:32:37.240 --> 00:32:38.720 social media, and the like. 00:32:38.720 --> 00:32:40.170 But at the end of the day, all you're receiving 00:32:40.170 --> 00:32:41.500 is, like, a key on a keyboard. 00:32:41.500 --> 00:32:44.320 So, in fact, you wouldn't know it to look at it. 00:32:44.320 --> 00:32:48.750 But in fact, the decimal number representing this face 00:32:48.750 --> 00:32:52.980 with tears of joy happens to be 128,514. 00:32:52.980 --> 00:32:56.220 So to Kevin's point, to represent not only certain human languages, 00:32:56.220 --> 00:33:00.910 but certainly these emojis, we need way more than 256 characters 00:33:00.910 --> 00:33:05.010 so we can use not just eight bits, but 16 or 24 or 32. 00:33:05.010 --> 00:33:06.955 That's a huge amount of possibilities now. 00:33:06.955 --> 00:33:09.330 In fact, now, to really take the fun out of these things, 00:33:09.330 --> 00:33:12.600 if you receive that face with tears of joy or send it, 00:33:12.600 --> 00:33:16.702 you're technically just sending a pattern of bits that looks like this. 00:33:16.702 --> 00:33:18.660 That's all that's going on underneath the hood, 00:33:18.660 --> 00:33:20.300 every time you use these things. 00:33:20.300 --> 00:33:20.800 All right. 00:33:20.800 --> 00:33:22.620 So we started again with electricity. 00:33:22.620 --> 00:33:24.402 We then represented numbers. 00:33:24.402 --> 00:33:26.610 Now we have the ability to represent letters and even 00:33:26.610 --> 00:33:28.470 emotions in the form of emojis. 00:33:28.470 --> 00:33:30.397 What else is there out there? 00:33:30.397 --> 00:33:33.480 Well, the emojis themselves, of course, at least the ones we've looked at, 00:33:33.480 --> 00:33:35.652 are pictorial in nature. 00:33:35.652 --> 00:33:37.860 And so that invites the question, how does a computer 00:33:37.860 --> 00:33:39.210 represent things like color? 00:33:39.210 --> 00:33:42.150 Like, that face with tears of joy had a lot of yellow in it. 00:33:42.150 --> 00:33:46.140 So how is yellow or any color, for that matter, represented in a computer? 00:33:46.140 --> 00:33:48.180 Well, let me ask the audience again. 00:33:48.180 --> 00:33:52.800 If all you have at your disposal is bits, zeros and ones, 00:33:52.800 --> 00:33:55.800 and we just as humans need to agree how to represent colors, 00:33:55.800 --> 00:33:57.510 what might be one possibility? 00:33:57.510 --> 00:33:59.440 It doesn't need to be the answer. 00:33:59.440 --> 00:34:03.360 But what might your own instinct be if designing this for the first time 00:34:03.360 --> 00:34:05.010 yourself? 00:34:05.010 --> 00:34:09.980 How might a computer represent colors now? 00:34:09.980 --> 00:34:12.110 Yasmin, what do you think? 00:34:12.110 --> 00:34:14.989 AUDIENCE: You would like members to different colors and shapes 00:34:14.989 --> 00:34:16.602 and just do the same system. 00:34:16.602 --> 00:34:17.810 DAVID J MALAN: Yeah, exactly. 00:34:17.810 --> 00:34:18.560 Perfect instincts. 00:34:18.560 --> 00:34:21.020 You would just assign numbers to the different colors, 00:34:21.020 --> 00:34:24.677 and we all just have to agree on what that mapping is actually going to be. 00:34:24.677 --> 00:34:26.760 So it turns out there's different ways to do this. 00:34:26.760 --> 00:34:30.530 And if any of you are artistic and use Photoshop or the like digitally, 00:34:30.530 --> 00:34:34.010 you're probably familiar with acronyms like RGB, red, green, blue. 00:34:34.010 --> 00:34:36.199 But there are other acronyms and other ways 00:34:36.199 --> 00:34:39.590 to implement Yasmin's idea where we just somehow map 00:34:39.590 --> 00:34:41.780 zeros and ones to actual colors. 00:34:41.780 --> 00:34:45.120 Well, RGB just happens to represent red, green, and blue. 00:34:45.120 --> 00:34:48.320 And this is a system humans came up with years ago that says, you know what? 00:34:48.320 --> 00:34:50.510 We can actually get every color of the rainbow 00:34:50.510 --> 00:34:55.370 by mixing together some amount of red and green and blue light, essentially. 00:34:55.370 --> 00:34:58.730 So that just invites the question, well, how do we represent the amount of red, 00:34:58.730 --> 00:35:00.980 how do we represent the amount of green, and how do we 00:35:00.980 --> 00:35:02.720 represent the amount of blue? 00:35:02.720 --> 00:35:05.760 And we have, as Yasmin says, bits at our disposal. 00:35:05.760 --> 00:35:07.470 So we just have to decide how to do this. 00:35:07.470 --> 00:35:12.320 So suppose we receive a pattern of bits, that 72, 73, 33 again, 00:35:12.320 --> 00:35:13.940 but this time it's not an email. 00:35:13.940 --> 00:35:15.320 It's not in a text message. 00:35:15.320 --> 00:35:19.020 It's in the context of a file that I've opened in Photoshop. 00:35:19.020 --> 00:35:21.650 So it's as though I've opened up a photograph that someone 00:35:21.650 --> 00:35:26.030 sent me and I want to do some editing and I see this pattern of numbers, 00:35:26.030 --> 00:35:27.170 or, in turn, bits. 00:35:27.170 --> 00:35:29.420 Well, what is that representing in this case? 00:35:29.420 --> 00:35:32.300 In the context of an email or a text message, it's still HI! 00:35:32.300 --> 00:35:34.670 But in the context of Photoshop or Instagram 00:35:34.670 --> 00:35:37.190 or anything that is oriented around images, 00:35:37.190 --> 00:35:40.730 it's actually going to represent some amount of red, some amount of green, 00:35:40.730 --> 00:35:41.870 some amount of blue. 00:35:41.870 --> 00:35:45.230 And as we discovered earlier, the total number 00:35:45.230 --> 00:35:49.340 of possibilities you can represent with eight bits happens to be 256. 00:35:49.340 --> 00:35:54.200 The highest value you can represent is 255 if we start counting from zero. 00:35:54.200 --> 00:35:56.660 So this is to say that each of these three numbers 00:35:56.660 --> 00:35:59.340 is a number between zero and 255. 00:35:59.340 --> 00:36:04.640 So 72 feels like a medium amount of red, 73 is like a medium amount of green, 33 00:36:04.640 --> 00:36:06.090 is a little bit of blue. 00:36:06.090 --> 00:36:10.610 And if you combine those three amounts of color, eight bits plus eight 00:36:10.610 --> 00:36:13.460 bits plus eight bits, using 24 bits total, 00:36:13.460 --> 00:36:16.880 using the first third to represent redness, the second third greenness, 00:36:16.880 --> 00:36:20.540 and the third third blueness, you get, it turns out, 00:36:20.540 --> 00:36:24.540 a dot that looks like this, a yellow dot. 00:36:24.540 --> 00:36:27.800 And so, indeed, that emoji, when it's being displayed on the screen, 00:36:27.800 --> 00:36:30.380 is the result of the computer interpreting-- 00:36:30.380 --> 00:36:35.120 the 128,514 value is knowing oh, that's the emoji 00:36:35.120 --> 00:36:36.770 with the face of tears of joy. 00:36:36.770 --> 00:36:39.470 But when it comes to displaying the information on your screen, 00:36:39.470 --> 00:36:42.860 now your computer is going to be using different patterns of bits 00:36:42.860 --> 00:36:45.428 to control the colors of the dots on the screen. 00:36:45.428 --> 00:36:46.970 And this term you might already know. 00:36:46.970 --> 00:36:50.240 The dots you and I see on our computer screens or even TVs these days 00:36:50.240 --> 00:36:51.230 are called pixels. 00:36:51.230 --> 00:36:55.250 They're tiny little squares that represent some color 00:36:55.250 --> 00:36:56.600 such as this yellow one here. 00:36:56.600 --> 00:36:58.860 And you can actually see them in some contexts. 00:36:58.860 --> 00:37:02.930 If I go ahead and pull up the same face with tears of joy and zoom in a bit, 00:37:02.930 --> 00:37:05.420 zoom in a bit more, and really zoom in a bit more, 00:37:05.420 --> 00:37:07.857 now you can actually see what we call pixelation. 00:37:07.857 --> 00:37:10.190 And odds are, you have seen this on Facebook, Instagram, 00:37:10.190 --> 00:37:13.790 wherever you might be resizing or editing photos that don't quite 00:37:13.790 --> 00:37:15.210 have enough resolution. 00:37:15.210 --> 00:37:18.740 The resolution of an image is just how many pixels or dots there 00:37:18.740 --> 00:37:21.000 are horizontally and vertically. 00:37:21.000 --> 00:37:25.380 So if you really zoom in on an image, you'll eventually see those pixels. 00:37:25.380 --> 00:37:29.510 And this is to say that even in this zoomed-in happy face, 00:37:29.510 --> 00:37:32.090 there's a huge number of yellow dots and a whole bunch 00:37:32.090 --> 00:37:34.880 of black and gray and brownish dots as well 00:37:34.880 --> 00:37:38.210 that compose this very colorful image. 00:37:38.210 --> 00:37:41.450 And so you can see them in that case, and every one of those dots now, 00:37:41.450 --> 00:37:47.070 a pixel, is using, I claim, like 24 bits or three bytes. 00:37:47.070 --> 00:37:51.320 Now, you can imagine, there's probably what, hundreds, maybe thousands of dots 00:37:51.320 --> 00:37:54.740 in that image if we zoom out and look at all of them again. 00:37:54.740 --> 00:37:58.140 So if every one of those dots or pixels is three bytes, 00:37:58.140 --> 00:38:00.890 this is why the photographs you and I take 00:38:00.890 --> 00:38:03.050 and the images you and I download from the internet 00:38:03.050 --> 00:38:06.590 are typically measured not even in bytes, per se, but in kilobytes, 00:38:06.590 --> 00:38:09.830 for thousands of bytes, or megabytes for millions of bytes, 00:38:09.830 --> 00:38:14.120 or if it's a video file, it might get even bigger, billions, or gigabytes. 00:38:14.120 --> 00:38:17.330 But that's all that is happening underneath the hood. 00:38:17.330 --> 00:38:20.240 We're just representing information in this way. 00:38:20.240 --> 00:38:22.040 Well, let me ask a follow up question now. 00:38:22.040 --> 00:38:26.360 If we've now, thanks to Yasmin, represented colors, 00:38:26.360 --> 00:38:30.680 and in turn, images, because all an image is is a grid of pixels. 00:38:30.680 --> 00:38:32.960 You take the same principle Yasmin proposed, 00:38:32.960 --> 00:38:36.710 where you represent each color of a dot and you have a whole bunch of dots that 00:38:36.710 --> 00:38:41.750 gives us images, how would you propose computers represent video files? 00:38:41.750 --> 00:38:44.630 Again, even if you don't know the answer, how might 00:38:44.630 --> 00:38:50.030 a computer represent video files now using, again, only bits 00:38:50.030 --> 00:38:52.630 at their disposal? 00:38:52.630 --> 00:38:55.030 Who might like to field this one? 00:38:55.030 --> 00:38:59.110 How might a computer represent a video? 00:38:59.110 --> 00:39:01.240 Justin, what do you think? 00:39:01.240 --> 00:39:04.802 AUDIENCE: I-- maybe just, like, rapidly changing the bites? 00:39:04.802 --> 00:39:06.760 DAVID J MALAN: Just rapidly changing the bites. 00:39:06.760 --> 00:39:08.680 I hear-- can you elaborate a little bit? 00:39:08.680 --> 00:39:11.350 What do you mean by changing the bites? 00:39:11.350 --> 00:39:17.582 AUDIENCE: Like rapidly changing the RGB of individual pixels-- 00:39:17.582 --> 00:39:18.540 DAVID J MALAN: Exactly. 00:39:18.540 --> 00:39:22.680 AUDIENCE: --to match the image of that second 00:39:22.680 --> 00:39:24.570 of the video or portion of the video. 00:39:24.570 --> 00:39:25.060 DAVID J MALAN: Perfect. 00:39:25.060 --> 00:39:27.520 So if you think about, like, the rectangular screen that is your phone, 00:39:27.520 --> 00:39:29.910 or your laptop, or your desktop monitor, if you just 00:39:29.910 --> 00:39:33.420 keep changing the colors of those dots once per second 00:39:33.420 --> 00:39:35.460 or a whole bunch of times per second, we'll 00:39:35.460 --> 00:39:39.690 get the illusion that there's actually motion on the screen, ergo video. 00:39:39.690 --> 00:39:43.950 So really, a video, in some sense, it's just a whole bunch of images, 00:39:43.950 --> 00:39:47.750 to Yasmin's definition, flying across the screen really quickly. 00:39:47.750 --> 00:39:49.680 And so you can see this even old school style. 00:39:49.680 --> 00:39:52.170 For instance, let me go ahead and open up on my screen 00:39:52.170 --> 00:39:54.940 a short video that represents a flipbook. 00:39:54.940 --> 00:39:57.690 So you might have made one of these as a kid or maybe your teacher 00:39:57.690 --> 00:40:00.510 did or you saw them, at least, in person somewhere. 00:40:00.510 --> 00:40:04.380 Where if you take a whole bunch of pieces of paper and staple 00:40:04.380 --> 00:40:08.190 or clip them together in some way, draw a whole lot of pictures, all of which 00:40:08.190 --> 00:40:11.650 are similar but slightly different on each page, 00:40:11.650 --> 00:40:14.910 you can create an animation, or, really, a video. 00:40:14.910 --> 00:40:17.450 And this is all a video is in a purely electronic world. 00:40:17.450 --> 00:40:19.200 Even though this happens to be implemented 00:40:19.200 --> 00:40:22.200 in paper, what happens in the computer world 00:40:22.200 --> 00:40:24.330 is indeed just a whole sequence of images 00:40:24.330 --> 00:40:26.830 flying across the screen at some rate. 00:40:26.830 --> 00:40:30.797 And that's what actually gives us the video files that you and I know today. 00:40:30.797 --> 00:40:32.880 And there's even more rabbit holes we can go down. 00:40:32.880 --> 00:40:34.860 For instance, how might you represent music? 00:40:34.860 --> 00:40:37.350 Well, music, could be represented, gosh, in different ways. 00:40:37.350 --> 00:40:39.470 Like, if you play the piano, for instance, you 00:40:39.470 --> 00:40:41.553 might know that there are notes, like A through G. 00:40:41.553 --> 00:40:43.740 But there's also sharps and flats and so forth. 00:40:43.740 --> 00:40:44.490 But you know what? 00:40:44.490 --> 00:40:48.510 Maybe we just need a number to represent each of those possible notes. 00:40:48.510 --> 00:40:51.660 And maybe also we could use another number, 00:40:51.660 --> 00:40:55.020 just like images use multiple numbers to represent dots, 00:40:55.020 --> 00:40:57.780 we could use a number to represent the notes in a song, 00:40:57.780 --> 00:41:01.560 but also another number to represent the duration of that note. 00:41:01.560 --> 00:41:05.147 How many seconds or milliseconds or beats should you hear that note for. 00:41:05.147 --> 00:41:07.230 So you could come up with other formulations, too, 00:41:07.230 --> 00:41:11.250 but music, really, can be quantized in the world of computers 00:41:11.250 --> 00:41:13.330 into just small pieces of information. 00:41:13.330 --> 00:41:16.110 And so long as you and I agree on how to represent it, 00:41:16.110 --> 00:41:17.640 that's how these things all work. 00:41:17.640 --> 00:41:23.400 And if you've ever wondered why there are JPEGs and PNGs and GIFs and Word 00:41:23.400 --> 00:41:27.750 documents and Excel files and all of these different file formats or file 00:41:27.750 --> 00:41:32.640 extensions on computers, those file extensions or formats just 00:41:32.640 --> 00:41:35.250 represent a whole bunch of humans agreeing 00:41:35.250 --> 00:41:38.430 how to store patterns of zeros and ones in a file 00:41:38.430 --> 00:41:43.530 so that when those zeros and ones are loaded into a computer for display 00:41:43.530 --> 00:41:47.460 or for interpretation, it knows what those patterns represent. 00:41:47.460 --> 00:41:49.572 Images are represented slightly differently, 00:41:49.572 --> 00:41:51.780 sound and video are represented slightly differently, 00:41:51.780 --> 00:41:54.820 but it's all zeros and ones at the end of the day. 00:41:54.820 --> 00:41:59.400 So this is all to say, so long as we all agree, ideally around the world, 00:41:59.400 --> 00:42:03.180 how to represent information, now we can represent inputs to problems 00:42:03.180 --> 00:42:05.790 and hopefully solve problems and get outputs. 00:42:05.790 --> 00:42:09.960 So all that remains in problem solving, or really, computer science broadly, 00:42:09.960 --> 00:42:14.950 is to look inside of this black box and to consider how you take inputs, 00:42:14.950 --> 00:42:18.810 whether it's numbers, letters, images, video, sound, 00:42:18.810 --> 00:42:21.930 and convert them into actual solutions. 00:42:21.930 --> 00:42:24.420 And so inside of this black box is what we would typically 00:42:24.420 --> 00:42:26.100 describe as algorithms. 00:42:26.100 --> 00:42:30.250 Algorithms are step by step instructions for solving problems. 00:42:30.250 --> 00:42:32.010 They don't even have to involve computers. 00:42:32.010 --> 00:42:36.630 We humans can execute algorithms just by following someone else's instructions. 00:42:36.630 --> 00:42:40.350 If you've ever prepared something from a cookbook, following a recipe, 00:42:40.350 --> 00:42:43.170 you are executing an algorithm step by step. 00:42:43.170 --> 00:42:46.170 But unlike a lot of recipes or unlike a lot 00:42:46.170 --> 00:42:48.880 of instructions that we humans give to each other, 00:42:48.880 --> 00:42:51.330 there's no room for ambiguity in computers. 00:42:51.330 --> 00:42:54.360 Computers' algorithms, when implemented by machines, 00:42:54.360 --> 00:42:56.910 they really have to be not only correct so 00:42:56.910 --> 00:42:59.070 that you get the right outputs that you care about, 00:42:59.070 --> 00:43:01.170 but they also need to be precise. 00:43:01.170 --> 00:43:04.590 You need to be ever so precise, because unlike we humans who can kind of like 00:43:04.590 --> 00:43:06.990 read between the lines and, yeah, I get what you mean, 00:43:06.990 --> 00:43:09.070 computers are going to take you literally. 00:43:09.070 --> 00:43:13.320 And so when programming a computer, that is, translating an algorithm, 00:43:13.320 --> 00:43:16.290 step by step instructions, into some language the computer understands, 00:43:16.290 --> 00:43:20.910 the onus is on you to make sure that the computer cannot misinterpret what you 00:43:20.910 --> 00:43:21.460 want. 00:43:21.460 --> 00:43:23.170 So let's consider one such algorithm. 00:43:23.170 --> 00:43:26.190 So on all of our phones, whether iOS or Android or the like, 00:43:26.190 --> 00:43:28.217 you have some contacts application. 00:43:28.217 --> 00:43:31.050 And that contacts application's probably storing all of your friends 00:43:31.050 --> 00:43:33.810 and family members and colleagues, probably alphabetically. 00:43:33.810 --> 00:43:36.450 Maybe by first name, maybe by last name, or however 00:43:36.450 --> 00:43:37.980 you've organized that device. 00:43:37.980 --> 00:43:40.590 Well, the old school version of this happens 00:43:40.590 --> 00:43:44.170 to be in paper form, which looks a little something like this, a phone 00:43:44.170 --> 00:43:44.670 book. 00:43:44.670 --> 00:43:48.280 And inside of an old school phone book really is that exact same idea. 00:43:48.280 --> 00:43:49.060 It's much larger. 00:43:49.060 --> 00:43:51.020 It's much more much more printed. 00:43:51.020 --> 00:43:52.020 But it's the same thing. 00:43:52.020 --> 00:43:54.812 There's a whole bunch of names and numbers in a typical phone book, 00:43:54.812 --> 00:43:59.280 sorted alphabetically just like your own Android phone or iOS 00:43:59.280 --> 00:44:00.697 phone might be as well. 00:44:00.697 --> 00:44:02.280 So suppose we want to solve a problem. 00:44:02.280 --> 00:44:05.340 And the input to that problem is not only this phone book, but also 00:44:05.340 --> 00:44:07.710 the name of someone to look up the number for. 00:44:07.710 --> 00:44:08.980 So my own name, for instance. 00:44:08.980 --> 00:44:12.660 If I want to look up my phone number, or you do, you might open up this book 00:44:12.660 --> 00:44:14.710 and start looking for David, for instance, 00:44:14.710 --> 00:44:16.770 if we assume that it's sorted by first name. 00:44:16.770 --> 00:44:20.020 I don't see David on the first page, so I move on to the second. 00:44:20.020 --> 00:44:22.390 I don't see myself there, so I move on to the third. 00:44:22.390 --> 00:44:25.120 I don't see myself there so I move on to the fourth. 00:44:25.120 --> 00:44:29.350 And so forth, one page at a time, looking for my name and, in turn, 00:44:29.350 --> 00:44:30.680 my number. 00:44:30.680 --> 00:44:34.120 Well, if correctness is important-- let me ask that question first. 00:44:34.120 --> 00:44:37.060 Is this algorithm, turning the pages, step by step, 00:44:37.060 --> 00:44:40.730 looking for David correct? 00:44:40.730 --> 00:44:41.480 What do you think? 00:44:41.480 --> 00:44:45.680 Within Zoom, you should see some icons under the participants 00:44:45.680 --> 00:44:47.820 window labeled yes and no. 00:44:47.820 --> 00:44:53.480 If you'd like to go ahead and vote virtually, yes or no, 00:44:53.480 --> 00:44:55.670 is this algorithm correct? 00:44:55.670 --> 00:44:59.390 One page at a time, looking for myself. 00:44:59.390 --> 00:45:01.370 Never mind the fact that this is yellow pages, 00:45:01.370 --> 00:45:03.980 and so I'm not going to be anywhere in the phone book, 00:45:03.980 --> 00:45:07.830 but indeed, we'll assume it contains humans as well. 00:45:07.830 --> 00:45:08.480 All right. 00:45:08.480 --> 00:45:13.430 So it looks like the algorithm is indeed correct, 00:45:13.430 --> 00:45:15.560 but it's terribly, terribly slow. 00:45:15.560 --> 00:45:18.170 And that's OK, because one of the ideas we're 00:45:18.170 --> 00:45:21.590 going to consider in CS50 and in turn, computer science, 00:45:21.590 --> 00:45:25.040 is not only the correctness of an algorithm, but also the efficiency. 00:45:25.040 --> 00:45:27.320 How well designed is the algorithm? 00:45:27.320 --> 00:45:28.070 This is correct. 00:45:28.070 --> 00:45:31.040 It's just incredibly, incredibly tedious and slow. 00:45:31.040 --> 00:45:32.035 But I will find myself. 00:45:32.035 --> 00:45:33.410 But, of course, we can do better. 00:45:33.410 --> 00:45:37.190 Instead of looking for myself one page at a time, why don't I do one page, 00:45:37.190 --> 00:45:40.790 let me do two, four, six, eight, 10. 00:45:40.790 --> 00:45:42.260 It sounds faster and it is faster. 00:45:42.260 --> 00:45:45.320 I'm going twice as fast through the phone book looking for myself. 00:45:45.320 --> 00:45:46.640 Is this algorithm correct? 00:45:46.640 --> 00:45:48.710 Let me go to someone in the audience this time. 00:45:48.710 --> 00:45:52.550 Is this algorithm of searching for someone's name two pages at a time 00:45:52.550 --> 00:45:53.300 correct? 00:45:53.300 --> 00:45:55.070 Because I claim it's more efficient. 00:45:55.070 --> 00:45:59.630 I claim it's better designed because I'll solve the problem twice as fast. 00:45:59.630 --> 00:46:00.800 Aneka, what do you think? 00:46:00.800 --> 00:46:03.373 AUDIENCE: No, because you might skip your name on a page. 00:46:03.373 --> 00:46:05.540 DAVID J MALAN: Yeah, I might skip my name on a page. 00:46:05.540 --> 00:46:07.290 And let me ask a follow up question. 00:46:07.290 --> 00:46:07.930 Can I fix this? 00:46:07.930 --> 00:46:09.680 Do I have to throw out the whole algorithm 00:46:09.680 --> 00:46:11.780 or can we at least fix this problem, do you think? 00:46:11.780 --> 00:46:15.285 AUDIENCE: I think whatever page you flip to, it would help to see, 00:46:15.285 --> 00:46:17.660 like, what name is there and maybe see if your name would 00:46:17.660 --> 00:46:18.890 come before or after. 00:46:18.890 --> 00:46:19.340 DAVID J MALAN: Nice. 00:46:19.340 --> 00:46:20.850 So that's exactly the right intuition. 00:46:20.850 --> 00:46:23.725 I don't think we have to completely sacrifice the idea of speeding up 00:46:23.725 --> 00:46:25.680 this algorithm by moving twice as fast. 00:46:25.680 --> 00:46:29.570 But as you propose, if I go too far-- maybe I get to the E section, 00:46:29.570 --> 00:46:30.920 which is one letter too late-- 00:46:30.920 --> 00:46:33.110 I should at least double back one page. 00:46:33.110 --> 00:46:35.210 Because I could get unlucky, and maybe David 00:46:35.210 --> 00:46:37.863 is kind of sandwiched in between two pages, at which point 00:46:37.863 --> 00:46:41.030 I might fly by, get to the end of the phone book, say, no, there's no David, 00:46:41.030 --> 00:46:43.910 and I just got unlucky with 50% probability. 00:46:43.910 --> 00:46:47.360 But as you propose, I can at least recover and sort of conditionally 00:46:47.360 --> 00:46:50.270 ask myself, wait a minute, maybe I just missed it, and double back. 00:46:50.270 --> 00:46:52.830 So I can get the overall speed improvement, 00:46:52.830 --> 00:46:55.700 but then at least fix that kind of mistake or bug. 00:46:55.700 --> 00:46:57.920 And bug, a term of our-- in programming, a bug 00:46:57.920 --> 00:47:00.080 is just a mistake in a program, or a mistake, more 00:47:00.080 --> 00:47:01.205 generally, in an algorithm. 00:47:01.205 --> 00:47:03.122 But honestly, none of us are going to do that. 00:47:03.122 --> 00:47:05.600 When we actually go to search for someone in a phone book, 00:47:05.600 --> 00:47:09.290 just like our phones do, they typically don't start at the top 00:47:09.290 --> 00:47:10.430 and go to the bottom. 00:47:10.430 --> 00:47:13.160 And computers do exactly what you might do more intuitively. 00:47:13.160 --> 00:47:14.930 They'll probably go roughly to the middle. 00:47:14.930 --> 00:47:16.640 Maybe they'll skew a little to the left, if you know 00:47:16.640 --> 00:47:18.230 D is toward the start of an alphabet. 00:47:18.230 --> 00:47:21.980 But, no I open to the middle sort of sloppily, and I'm in the M section. 00:47:21.980 --> 00:47:25.130 So what do I know when I'm in the M section about this problem? 00:47:25.130 --> 00:47:26.990 Let me call on one more person. 00:47:26.990 --> 00:47:28.160 I'm in the M section. 00:47:28.160 --> 00:47:31.970 What would you do as a human now, taking this as input to solve this problem? 00:47:31.970 --> 00:47:39.530 What do I know about the location, of course, of my name in the phone book? 00:47:39.530 --> 00:47:43.040 What decision can I make here? 00:47:43.040 --> 00:47:44.560 What decision can I make? 00:47:44.560 --> 00:47:45.560 Kyle, what do you think? 00:47:45.560 --> 00:47:46.185 AUDIENCE: Yeah. 00:47:46.185 --> 00:47:49.370 So from the M onwards, you know that your name won't be there for sure. 00:47:49.370 --> 00:47:51.380 DAVID J MALAN: Yeah, so my name is not going to be in the M section. 00:47:51.380 --> 00:47:53.780 But thanks to the alphabetization of the phone book, I at least know, 00:47:53.780 --> 00:47:54.650 you know what? 00:47:54.650 --> 00:47:57.170 I can take a huge bite out of this problem 00:47:57.170 --> 00:48:01.043 and tear the problem in half, both metaphorically and also literally, 00:48:01.043 --> 00:48:02.210 in the case of a phone book. 00:48:02.210 --> 00:48:05.540 And I can literally throw half of the problem away. 00:48:05.540 --> 00:48:08.690 And so if I started with something like 1,000 pages in this phone book 00:48:08.690 --> 00:48:12.050 or 1,000 contacts in my phone, just by going to the middle, 00:48:12.050 --> 00:48:14.990 roughly, and taking a look to the left and the right, I can decide, 00:48:14.990 --> 00:48:18.330 as you note, well, it's not on the page I'm looking for. 00:48:18.330 --> 00:48:20.480 But I can decide it's to the left or to the right. 00:48:20.480 --> 00:48:24.110 I know D comes before M. And so now I can go to the left. 00:48:24.110 --> 00:48:26.210 And you know what's interesting here, is that I 00:48:26.210 --> 00:48:28.057 can use that exact same algorithm. 00:48:28.057 --> 00:48:29.640 I don't have to think any differently. 00:48:29.640 --> 00:48:33.530 I can apply the same logic, open to the middle of this half of the phone, book 00:48:33.530 --> 00:48:35.450 and now I see I'm in the G section. 00:48:35.450 --> 00:48:37.070 So I'm still a little too far. 00:48:37.070 --> 00:48:40.880 But again, I can tear half the problem away, throw it down 00:48:40.880 --> 00:48:45.050 and now I've gone from, like 1,000 pages to 500 pages to 250 pages. 00:48:45.050 --> 00:48:49.040 If I do this again, I might find myself, oh, I made it to the C section now. 00:48:49.040 --> 00:48:52.370 I can tear the problem in half again, throw the left half away, 00:48:52.370 --> 00:48:54.920 and now I'm down to just 125 pages. 00:48:54.920 --> 00:48:56.840 Now, that's still a lot, but my god. 00:48:56.840 --> 00:49:00.020 I've gone from 1,000 to 500 to 250 to 125. 00:49:00.020 --> 00:49:05.150 That is way faster than going from 1,000 to 999 to 998, 00:49:05.150 --> 00:49:11.300 and it's even faster than going from 1,000 to 998 to 996 to 994. 00:49:11.300 --> 00:49:15.030 Both of those algorithms are going to take me much longer as well. 00:49:15.030 --> 00:49:17.840 We have this visualization made by Brian, wonderfully, 00:49:17.840 --> 00:49:23.870 that depicts 1,024 page phone book with one page being flipped at a time. 00:49:23.870 --> 00:49:26.665 And now we're down to 996, 995. 00:49:26.665 --> 00:49:28.790 I mean, honestly, this isn't all that enlightening. 00:49:28.790 --> 00:49:31.970 It's going to take forever to find David or any name in a phone book 00:49:31.970 --> 00:49:35.060 when starting at that kind of pace with that algorithm. 00:49:35.060 --> 00:49:37.070 But what if, instead, I'm a little smarter 00:49:37.070 --> 00:49:38.450 and I'm a little more intuitive? 00:49:38.450 --> 00:49:40.970 And I harness the intuition that probably you had and I 00:49:40.970 --> 00:49:44.390 start with 1,024 pages again, and this time 00:49:44.390 --> 00:49:48.620 divide and conquer, half at a time, splitting the problem in half? 00:49:48.620 --> 00:49:51.900 Tearing the phone book in half, I get down to just one page. 00:49:51.900 --> 00:49:55.610 And if we actually do out the math, if you start at like 1,000-plus pages, 00:49:55.610 --> 00:50:01.070 it will only take me 10 total tears of that phone book in order to get down 00:50:01.070 --> 00:50:05.690 to my number, 949-468-2750. 00:50:05.690 --> 00:50:10.010 So that just is to say that the third algorithm is not only correct, just 00:50:10.010 --> 00:50:14.600 as the first one definitely was and the second one could be with that bug fix, 00:50:14.600 --> 00:50:16.610 but it's also much better designed. 00:50:16.610 --> 00:50:17.670 It's much more efficient. 00:50:17.670 --> 00:50:20.040 And so we can see this a little graphically as well. 00:50:20.040 --> 00:50:23.200 Let me go ahead and propose not a numerical analysis or anything 00:50:23.200 --> 00:50:23.700 like that. 00:50:23.700 --> 00:50:26.550 But just something that's a little visual like this. 00:50:26.550 --> 00:50:28.640 So if I have an x-axis here that represents 00:50:28.640 --> 00:50:32.300 horizontally the size of the problem, the number of pages in a phone book, 00:50:32.300 --> 00:50:36.030 and vertically on the y-axis, the amount of time required to solve a problem. 00:50:36.030 --> 00:50:38.840 What co these algorithms look like, if we just kind of chart them? 00:50:38.840 --> 00:50:42.410 Well, the first algorithm, depicted here in red, it's just a straight line. 00:50:42.410 --> 00:50:45.710 It's a slope of one because there is this one to one 00:50:45.710 --> 00:50:48.560 relationship between number of pages and the amount of time 00:50:48.560 --> 00:50:49.670 it takes me to solve it. 00:50:49.670 --> 00:50:52.730 For every new page of that phone book, maybe year after year, 00:50:52.730 --> 00:50:54.710 if the phone book grows, it's going to take me 00:50:54.710 --> 00:50:57.560 one more step to look for myself or anyone else, 00:50:57.560 --> 00:50:59.120 potentially, in that phone book. 00:50:59.120 --> 00:51:02.060 Unless I get lucky and they're early in the phone book, but one more 00:51:02.060 --> 00:51:04.460 page means one more page turn. 00:51:04.460 --> 00:51:06.980 The second algorithm is actually better. 00:51:06.980 --> 00:51:08.580 It's still a straight line. 00:51:08.580 --> 00:51:11.540 So it's still a linear relationship. 00:51:11.540 --> 00:51:15.020 But for every two pages in the phone book it takes me one more step. 00:51:15.020 --> 00:51:16.400 Two pages, one turn. 00:51:16.400 --> 00:51:17.750 Two pages, one turn. 00:51:17.750 --> 00:51:20.040 So it's strictly better than the first algorithm. 00:51:20.040 --> 00:51:20.540 Why? 00:51:20.540 --> 00:51:22.957 Well, if we consider this-- if the size of the problem is, 00:51:22.957 --> 00:51:24.240 maybe, here, for instance. 00:51:24.240 --> 00:51:26.420 So if we assume, for the sake of discussion, maybe 00:51:26.420 --> 00:51:29.780 the phone book has this many pages depicted with this dotted line. 00:51:29.780 --> 00:51:32.540 Well, how much time is it going to take the second algorithm 00:51:32.540 --> 00:51:34.553 to find someone in that phone book? 00:51:34.553 --> 00:51:36.470 It's going to take this amount of time, right? 00:51:36.470 --> 00:51:37.910 Where those two lines intersect. 00:51:37.910 --> 00:51:40.850 If you're using the first algorithm, though, going one page at a time, 00:51:40.850 --> 00:51:45.210 it's actually going to take this much time, which is literally twice as much. 00:51:45.210 --> 00:51:47.820 So they're both correct, assuming we double back as needed 00:51:47.820 --> 00:51:50.030 if I go too far past a name. 00:51:50.030 --> 00:51:52.400 But both of those are sort of fundamentally the same. 00:51:52.400 --> 00:51:54.400 They're the same shape, and, honestly, they both 00:51:54.400 --> 00:51:56.990 felt slow to say and to act out. 00:51:56.990 --> 00:51:59.210 The third algorithm, if we were to graph it, 00:51:59.210 --> 00:52:01.700 has a fundamentally different relationship 00:52:01.700 --> 00:52:05.480 between the size of the problem and the time required to solve the problem. 00:52:05.480 --> 00:52:07.953 The line goes up, up, up, up, as it should, 00:52:07.953 --> 00:52:11.120 because the more pages there are, the more time it's going to take to solve, 00:52:11.120 --> 00:52:14.360 but notice how much more slowly it goes up. 00:52:14.360 --> 00:52:18.320 This thing barely starts to rise as the size of the problem 00:52:18.320 --> 00:52:19.880 gets bigger and bigger and bigger. 00:52:19.880 --> 00:52:21.618 And why is that, intuitively? 00:52:21.618 --> 00:52:24.410 Well, here, what's powerful is, suppose that phone book, next year, 00:52:24.410 --> 00:52:26.630 for whatever reason, doubled in size. 00:52:26.630 --> 00:52:29.930 Maybe Cambridge and Allston, Massachusetts merged together into one 00:52:29.930 --> 00:52:33.530 big phone book, so there's 2,000-some-odd pages now instead. 00:52:33.530 --> 00:52:35.660 How many more steps would it take next year 00:52:35.660 --> 00:52:37.970 to search for someone in that phone book? 00:52:37.970 --> 00:52:38.770 One. 00:52:38.770 --> 00:52:39.920 One more step. 00:52:39.920 --> 00:52:42.920 And so if you look way out here along this green line, 00:52:42.920 --> 00:52:45.050 doubling the size of the phone book, the line 00:52:45.050 --> 00:52:48.170 itself is only going to rise ever so slightly because no big deal. 00:52:48.170 --> 00:52:52.410 With that third algorithm you're taking much bigger bites out of the problem. 00:52:52.410 --> 00:52:56.120 And so this, too, speaks to what computer science and what programming 00:52:56.120 --> 00:52:57.110 are ultimately like. 00:52:57.110 --> 00:52:59.765 Harnessing ideas that you come into the class with 00:52:59.765 --> 00:53:01.640 and that you might use in your everyday life, 00:53:01.640 --> 00:53:05.720 but you don't necessarily think about how you might represent problems 00:53:05.720 --> 00:53:10.250 using those algorithms and how you might translate them to computer speak. 00:53:10.250 --> 00:53:13.037 And indeed, one way we'll start to think about algorithms 00:53:13.037 --> 00:53:15.620 is not only their correctness, but how well designed they are. 00:53:15.620 --> 00:53:17.540 And so for instance here, I've deliberately 00:53:17.540 --> 00:53:21.800 labeled these three lines n, n over 2, and log base 2 over n. 00:53:21.800 --> 00:53:25.250 That just means that if we use n as number-- so computer scientists tend 00:53:25.250 --> 00:53:30.140 to use n as a variable, much like a mathematician might say x or y or z, 00:53:30.140 --> 00:53:31.220 n for number. 00:53:31.220 --> 00:53:34.603 And so the first red line is the running time, 00:53:34.603 --> 00:53:36.770 the number of steps it might take to solve a problem 00:53:36.770 --> 00:53:38.150 might be, in the worst case, n. 00:53:38.150 --> 00:53:41.368 If there's n pages in the phone book, maybe I'm looking for someone way 00:53:41.368 --> 00:53:43.160 at the end of the phone book and it's going 00:53:43.160 --> 00:53:45.105 to take me all n steps to find them. 00:53:45.105 --> 00:53:47.480 The second algorithm is going to take half as many steps. 00:53:47.480 --> 00:53:50.720 So we express that as n divided by 2, because if we're 00:53:50.720 --> 00:53:54.020 doing two pages at a time we'll get to the end of the phone book-- 00:53:54.020 --> 00:53:57.200 if we're looking for someone whose name starts with Z, for instance-- 00:53:57.200 --> 00:53:58.520 twice as fast. 00:53:58.520 --> 00:54:01.490 But the third algorithm, if you're a little rusty on the mathematics, 00:54:01.490 --> 00:54:04.730 is represented as a logarithm with a base of 2. 00:54:04.730 --> 00:54:08.540 And this just means that this graph, the green line 00:54:08.540 --> 00:54:14.840 describes how much time it takes to solve a problem when on each pass, 00:54:14.840 --> 00:54:19.220 on each step, you are dividing the problem, in this case, by half. 00:54:19.220 --> 00:54:22.740 The other two algorithms are taking one or two bites out of the problem. 00:54:22.740 --> 00:54:27.540 The third algorithm was taking half of the whole problem at a time. 00:54:27.540 --> 00:54:29.850 And that's what made it all the more powerful. 00:54:29.850 --> 00:54:32.240 So when it comes to programming now, we need 00:54:32.240 --> 00:54:35.120 to translate these things called algorithms to code. 00:54:35.120 --> 00:54:36.920 Or, in this case, let's call it pseudocode. 00:54:36.920 --> 00:54:40.160 And in just a bit, we'll focus on an actual programming language, 00:54:40.160 --> 00:54:41.330 albeit a graphical one. 00:54:41.330 --> 00:54:44.350 But for now let's just consider some of the constructs or sort 00:54:44.350 --> 00:54:46.850 of fundamental ideas that are going to be useful to leverage 00:54:46.850 --> 00:54:48.680 here on out in this class. 00:54:48.680 --> 00:54:51.440 So let me propose that what I really just did verbally 00:54:51.440 --> 00:54:55.580 can be translated into pseudocode, which is like an algorithm implemented 00:54:55.580 --> 00:54:58.168 in English, or whatever your spoken or written language is. 00:54:58.168 --> 00:54:59.960 But the key is that it's got to be correct, 00:54:59.960 --> 00:55:03.410 and ideally it had better be precise so that there's no ambiguity. 00:55:03.410 --> 00:55:05.150 Step one was, indeed, what I did. 00:55:05.150 --> 00:55:06.260 Pick up phone book. 00:55:06.260 --> 00:55:08.810 Step two, open to middle of phone book. 00:55:08.810 --> 00:55:10.580 Step three, look at page. 00:55:10.580 --> 00:55:11.780 And indeed I did that. 00:55:11.780 --> 00:55:14.000 And now things got interesting. 00:55:14.000 --> 00:55:15.740 Step four, if person-- 00:55:15.740 --> 00:55:19.430 David, in my case-- is on the page, what do I want to do? 00:55:19.430 --> 00:55:21.290 Well, I should probably call that person. 00:55:21.290 --> 00:55:22.280 The problem is solved. 00:55:22.280 --> 00:55:24.620 I've gotten my output, the person's number. 00:55:24.620 --> 00:55:27.800 But there's another possibility, not if the person's on the page 00:55:27.800 --> 00:55:29.930 but, rather if the person is earlier in the book-- 00:55:29.930 --> 00:55:31.555 and that is what happened a moment ago. 00:55:31.555 --> 00:55:34.820 If I ended up on M, but I'm looking for David, that's to the left, 00:55:34.820 --> 00:55:36.890 I should then do what? 00:55:36.890 --> 00:55:39.830 Open to the middle of the left half of the book. 00:55:39.830 --> 00:55:41.083 And that's indeed what I did. 00:55:41.083 --> 00:55:43.250 And I sort of gratuitously tore the problem in half. 00:55:43.250 --> 00:55:47.810 But algorithmically, I just looked at the left half of the book next. 00:55:47.810 --> 00:55:48.560 What do I do next? 00:55:48.560 --> 00:55:50.268 Well, really, that's the point at which I 00:55:50.268 --> 00:55:53.120 proposed that the algorithm is now just repeatable, again and again, 00:55:53.120 --> 00:55:54.860 and so we'll say go back to line three. 00:55:54.860 --> 00:55:55.460 Why? 00:55:55.460 --> 00:55:58.125 Well, starting at line three, I have an algorithm 00:55:58.125 --> 00:55:59.750 for looking up someone in a phone book. 00:55:59.750 --> 00:56:02.728 It just so happens the phone book now is half as large. 00:56:02.728 --> 00:56:03.770 But there's another case. 00:56:03.770 --> 00:56:05.437 What if the person is later in the book? 00:56:05.437 --> 00:56:07.700 I wasn't searching for David, which starts with D, 00:56:07.700 --> 00:56:10.640 but someone else's name that's toward the end of the alphabet. 00:56:10.640 --> 00:56:13.698 Well, then if that person is later in the book, same idea. 00:56:13.698 --> 00:56:15.740 Open to the middle of the right half of the book, 00:56:15.740 --> 00:56:18.350 and then again, go back to step three. 00:56:18.350 --> 00:56:20.900 But lastly, there's a fourth possibility. 00:56:20.900 --> 00:56:22.160 There's a fourth possibility. 00:56:22.160 --> 00:56:24.950 Either the person's in the phone book, or they're to the left 00:56:24.950 --> 00:56:29.820 or they're to the right, or, frankly, they are just not there at all. 00:56:29.820 --> 00:56:32.480 And this last point, though somewhat subtle, is so important. 00:56:32.480 --> 00:56:34.520 Odds are all of us on our Macs, PCs. 00:56:34.520 --> 00:56:38.910 Maybe even phones, have had that very frustrating experience where 00:56:38.910 --> 00:56:43.130 your computer hangs, you get the stupid spinning beachball or hourglass, 00:56:43.130 --> 00:56:46.430 the thing freezes or just reboots, you know, something goes wrong 00:56:46.430 --> 00:56:48.050 and it's sort of inexplicable. 00:56:48.050 --> 00:56:50.270 And you might think it's your fault, but really it's 00:56:50.270 --> 00:56:54.200 usually the programmer's fault who wrote the software that you're 00:56:54.200 --> 00:56:56.610 using on your computer or your device. 00:56:56.610 --> 00:56:57.260 Why? 00:56:57.260 --> 00:57:00.960 Very often, that programmer, for whatever reason, 00:57:00.960 --> 00:57:03.935 did not anticipate a possible scenario. 00:57:03.935 --> 00:57:05.810 In this case, there's four scenarios, but you 00:57:05.810 --> 00:57:09.230 could imagine kind of forgetting the fact that, oh, well maybe David's 00:57:09.230 --> 00:57:11.000 not even in this phone book. 00:57:11.000 --> 00:57:12.720 But you'd better handle that scenario. 00:57:12.720 --> 00:57:14.930 And when you have a computer that freezes or hangs 00:57:14.930 --> 00:57:17.120 or reboots or just something goes awry, that 00:57:17.120 --> 00:57:20.270 is quite often quite simply because a human did not 00:57:20.270 --> 00:57:23.280 code for some possible scenario. 00:57:23.280 --> 00:57:25.610 So what are the fundamental constructs we've 00:57:25.610 --> 00:57:28.430 seen here that we're going to continue seeing in class? 00:57:28.430 --> 00:57:32.030 Well, highlighted in yellow now are really some verbs or actions 00:57:32.030 --> 00:57:33.870 that we exercised with that phone book. 00:57:33.870 --> 00:57:36.560 These are, in general, in programming called functions. 00:57:36.560 --> 00:57:39.130 A function is an action or a verb. 00:57:39.130 --> 00:57:41.820 It's a statement that gets the computer to do something. 00:57:41.820 --> 00:57:45.725 Next highlighted here are what we'll call conditions or branches. 00:57:45.725 --> 00:57:47.850 These are sort of the proverbial forks in the road. 00:57:47.850 --> 00:57:50.890 You could either do this or this or maybe this other thing. 00:57:50.890 --> 00:57:53.730 And you can have one decision to make or two or three or four, 00:57:53.730 --> 00:57:55.800 however many conditions make sense logically. 00:57:55.800 --> 00:57:57.210 We'll call those conditions. 00:57:57.210 --> 00:58:00.790 But how do you decide which fork in the road to take? 00:58:00.790 --> 00:58:03.030 Whether to do this or that or this other thing? 00:58:03.030 --> 00:58:06.030 For that we need something called Boolean expressions. 00:58:06.030 --> 00:58:09.150 A Boolean expression it's just a question 00:58:09.150 --> 00:58:14.700 whose answer is yes or no, or true or false, or, frankly, one or zero. 00:58:14.700 --> 00:58:16.990 All of those would be equivalent for our purposes. 00:58:16.990 --> 00:58:18.270 So person on page. 00:58:18.270 --> 00:58:19.590 That's a yes or no question. 00:58:19.590 --> 00:58:20.730 Person earlier in book? 00:58:20.730 --> 00:58:21.690 That too is a question. 00:58:21.690 --> 00:58:24.280 Person later in book is a third question as well. 00:58:24.280 --> 00:58:28.860 So if you can imagine a yes-no answer, a true-false answer, a one-zero answer, 00:58:28.860 --> 00:58:31.708 that is what gives us these things called Boolean expressions. 00:58:31.708 --> 00:58:33.750 And then lastly, in yellow here are these things. 00:58:33.750 --> 00:58:35.490 Go back to line three. 00:58:35.490 --> 00:58:38.490 This will induce what we'll call a loop or a cycle, 00:58:38.490 --> 00:58:42.232 which is just a programming construct or principle of an algorithm that 00:58:42.232 --> 00:58:44.190 gets you to do something again and again so you 00:58:44.190 --> 00:58:46.620 don't have to write 100-line algorithm. 00:58:46.620 --> 00:58:51.630 You can write a 13-line algorithm and reuse parts of it again and again. 00:58:51.630 --> 00:58:55.890 And so we'll begin now, and we'll begin CS50 with a look 00:58:55.890 --> 00:58:59.400 at an actual programming language, one that you might have used recently 00:58:59.400 --> 00:59:03.120 or as younger kids, known as Scratch, which is a graphical programming 00:59:03.120 --> 00:59:07.860 language which, while it might be very familiar to some of you, 00:59:07.860 --> 00:59:10.770 it actually represents a lot of these programming fundamentals 00:59:10.770 --> 00:59:13.980 that we'll use as this ground for transitioning in just one week 00:59:13.980 --> 00:59:16.440 to a more traditional more old school language, 00:59:16.440 --> 00:59:19.260 known as C, which is entirely text and keyboard-based. 00:59:19.260 --> 00:59:21.720 But we'll see in all of the languages we look at in CS50, 00:59:21.720 --> 00:59:25.230 these things called functions and conditions, Boolean expressions 00:59:25.230 --> 00:59:27.300 and loops, and today, in just a moment, we'll 00:59:27.300 --> 00:59:31.230 also see some other features that we describe as variables, not unlike x, y, 00:59:31.230 --> 00:59:35.350 and z in math, threads, which allow a computer to do, it would seem, 00:59:35.350 --> 00:59:40.270 multiple things at once, events, and yet other features as well. 00:59:40.270 --> 00:59:46.350 And so from here, we transition from pseudocode to actual code. 00:59:46.350 --> 00:59:49.095 And what you see on the screen here is an example 00:59:49.095 --> 00:59:51.720 of a language called C, where we'll spend a good amount of time 00:59:51.720 --> 00:59:52.303 this semester. 00:59:52.303 --> 00:59:54.810 This is the older school text-based, keyboard-based language 00:59:54.810 --> 00:59:56.130 to which I referred earlier. 00:59:56.130 --> 00:59:58.050 But this language is a bit cryptic. 00:59:58.050 --> 01:00:01.650 And certainly at first glance, you might wonder why is the hash symbol there, 01:00:01.650 --> 01:00:05.220 the angled brackets, the parentheses, the curly braces, the semicolon, 01:00:05.220 --> 01:00:10.020 the quotes, I mean, my god, there is so much syntax to what is on the screen 01:00:10.020 --> 01:00:10.590 now. 01:00:10.590 --> 01:00:12.690 And you can probably guess what this program does. 01:00:12.690 --> 01:00:14.315 Let me just go quickly to the audience. 01:00:14.315 --> 01:00:17.310 What, anyone, does this program probably do, even if you've never 01:00:17.310 --> 01:00:19.470 programmed a computer before? 01:00:19.470 --> 01:00:21.552 AUDIENCE: It just prints out hello, comma, world. 01:00:21.552 --> 01:00:22.510 DAVID J MALAN: Exactly. 01:00:22.510 --> 01:00:23.820 It just prints hello, world. 01:00:23.820 --> 01:00:26.850 And my god, like, look at all of the syntax and all of the keystrokes 01:00:26.850 --> 01:00:29.560 we had to type just to command the computer to do that. 01:00:29.560 --> 01:00:31.888 And so by contrast today is Scratch. 01:00:31.888 --> 01:00:34.680 We'll allow ourselves for just today to look at something much more 01:00:34.680 --> 01:00:39.210 friendly, much more graphical, that will allow us to explore these very ideas 01:00:39.210 --> 01:00:42.810 and set the stage for more sophisticated, more traditional 01:00:42.810 --> 01:00:45.648 languages next week and beyond, but in the context 01:00:45.648 --> 01:00:48.690 where we don't have to worry about parentheses, semicolons, curly braces, 01:00:48.690 --> 01:00:51.520 and where even these keys are on the keyboard. 01:00:51.520 --> 01:00:53.553 So allow me to introduce you, then, to Scratch, 01:00:53.553 --> 01:00:55.470 developed by some of our friends down the road 01:00:55.470 --> 01:00:58.020 here in Cambridge at MIT's Media Lab. 01:00:58.020 --> 01:01:02.490 You can play along at home here on out if you would like at scratch.mit.edu. 01:01:02.490 --> 01:01:05.760 It's web-based, but there's also an offline version 01:01:05.760 --> 01:01:07.770 if you tend not to have the best of internet. 01:01:07.770 --> 01:01:10.170 But the user interface would typically look like this. 01:01:10.170 --> 01:01:11.190 And a quick tour. 01:01:11.190 --> 01:01:15.390 So here on scratch.mit.edu, when you go to create 01:01:15.390 --> 01:01:20.400 a project via the button on the interface, you'll see first Scratch, 01:01:20.400 --> 01:01:22.920 the namesake of the program, this cat who 01:01:22.920 --> 01:01:25.630 lives in this little rectangular world in which you can move up, 01:01:25.630 --> 01:01:26.505 down, left, or right. 01:01:26.505 --> 01:01:29.310 But the cat can be transformed into any number of other characters, 01:01:29.310 --> 01:01:32.850 or what we'll call sprites, visual representations thereof. 01:01:32.850 --> 01:01:37.230 On the left here, now, are all of the building blocks that come with Scratch. 01:01:37.230 --> 01:01:39.780 All of the programming constructs available to you 01:01:39.780 --> 01:01:41.310 in the form of puzzle pieces. 01:01:41.310 --> 01:01:43.477 And you'll notice that they're categorized according 01:01:43.477 --> 01:01:46.650 to color and description and there's a whole bunch of puzzle pieces 01:01:46.650 --> 01:01:48.120 that rather say what they do. 01:01:48.120 --> 01:01:52.080 And today the goal is not to go into the weeds of all of these various puzzle 01:01:52.080 --> 01:01:55.980 pieces, but to highlight some of the fundamental ideas that are possible. 01:01:55.980 --> 01:01:58.695 And we'll explore those ideas via the middle of the screen here. 01:01:58.695 --> 01:02:01.320 We'll be able, in just a moment, to start dragging and dropping 01:02:01.320 --> 01:02:04.110 these puzzle pieces onto this larger screen 01:02:04.110 --> 01:02:09.450 and interlock them together, if it makes logical sense to do so. 01:02:09.450 --> 01:02:11.790 Finally, for the most sophisticated programs, 01:02:11.790 --> 01:02:14.700 we can actually create yet more characters or sprites 01:02:14.700 --> 01:02:18.580 and actually have a lot of interactions on the screen as well. 01:02:18.580 --> 01:02:21.630 But let's go ahead and dive in with just an example quite quickly. 01:02:21.630 --> 01:02:27.630 I'm going to go ahead on my screen and go, indeed, to scratch.mit.edu. 01:02:27.630 --> 01:02:30.430 And you're welcome to play along at home as well. 01:02:30.430 --> 01:02:33.900 And I'm going to click Create in order to get into exactly that interface. 01:02:33.900 --> 01:02:37.140 You do not need to make an account from the get go unless you would like. 01:02:37.140 --> 01:02:39.450 And let me go ahead and start creating a program. 01:02:39.450 --> 01:02:42.300 The very first program that was once written, by lore, 01:02:42.300 --> 01:02:46.170 was quite simply what Iris proposed as "Hello World," a program that 01:02:46.170 --> 01:02:47.850 prints on the screen hello, world. 01:02:47.850 --> 01:02:49.000 Well, how can we do that? 01:02:49.000 --> 01:02:51.360 Well, I can probably do this quite quickly 01:02:51.360 --> 01:02:53.698 because I've used the interface before, but the goal 01:02:53.698 --> 01:02:55.740 for you at hand if you've never used this before, 01:02:55.740 --> 01:02:58.860 with the course's first problem set or programming assignment, really 01:02:58.860 --> 01:03:01.740 is just to get your hands dirty and explore and poke around. 01:03:01.740 --> 01:03:05.790 And odds are, the ideas you are looking for, you'll find eventually pop out. 01:03:05.790 --> 01:03:08.730 And the first one I'm going to try out is this one here. 01:03:08.730 --> 01:03:12.030 This puzzle piece that's a little yellow or orange in color. 01:03:12.030 --> 01:03:16.200 It's in the Events category, and it's called when green flag clicked. 01:03:16.200 --> 01:03:19.440 This is of interest, because if I go to Scratch's stage over here, 01:03:19.440 --> 01:03:24.810 you'll see at top left there's a green flag that's going to signify go, 01:03:24.810 --> 01:03:27.630 and a red stop sign that's going to signify stop. 01:03:27.630 --> 01:03:31.170 So if I want something to happen when I click that green flag, 01:03:31.170 --> 01:03:33.520 I'm going to start with this puzzle piece here. 01:03:33.520 --> 01:03:35.852 Now I'm going to go over into the Looks category. 01:03:35.852 --> 01:03:38.310 And in the Looks category, there's a whole bunch of blocks. 01:03:38.310 --> 01:03:39.935 But we're going to keep it simple here. 01:03:39.935 --> 01:03:44.670 I'm going to go ahead and just say the canonical, as Iris noted, hello, comma, 01:03:44.670 --> 01:03:45.390 world. 01:03:45.390 --> 01:03:46.837 I'll zoom back out. 01:03:46.837 --> 01:03:49.920 I'll move over to Scratch here, and I'm going to click now the green flag. 01:03:49.920 --> 01:03:52.150 And voila, hello, world. 01:03:52.150 --> 01:03:55.410 So that is my-- and perhaps, soon, your-- very first program, 01:03:55.410 --> 01:03:57.480 using in this language Scratch. 01:03:57.480 --> 01:03:59.880 But, of course, this isn't terribly interesting. 01:03:59.880 --> 01:04:01.723 Might be gratifying for the very first time. 01:04:01.723 --> 01:04:04.140 But it's not something you'd want to play again and again. 01:04:04.140 --> 01:04:06.152 But we can make this thing much more interactive 01:04:06.152 --> 01:04:08.110 and we can start to layer these building blocks 01:04:08.110 --> 01:04:10.980 and have an algorithm more like searching that phone book, 01:04:10.980 --> 01:04:12.460 that has multiple steps. 01:04:12.460 --> 01:04:14.380 So let me go ahead and stop that program. 01:04:14.380 --> 01:04:16.320 And let me explore a little bit instead. 01:04:16.320 --> 01:04:19.860 Let me go under Sensing this time, this blue category. 01:04:19.860 --> 01:04:21.540 And you'll see this block here. 01:04:21.540 --> 01:04:23.940 Ask what's your name, and wait. 01:04:23.940 --> 01:04:26.570 But notice that what's your name is in this white oval, 01:04:26.570 --> 01:04:29.320 and that implies that I can change what the question is if I want, 01:04:29.320 --> 01:04:31.120 but I'm fine with that question for now. 01:04:31.120 --> 01:04:34.620 And let me go ahead and first get rid of these blocks, 01:04:34.620 --> 01:04:39.720 and give myself when green flag clicked, and this time start under Sensing 01:04:39.720 --> 01:04:41.700 with ask what's your name and wait. 01:04:41.700 --> 01:04:43.860 But notice that this is kind of a special block. 01:04:43.860 --> 01:04:46.920 It comes with a second block, a so-called variable. 01:04:46.920 --> 01:04:50.070 It turns out that this ask puzzle piece is literally 01:04:50.070 --> 01:04:52.950 going to ask the human who's playing this game a question, 01:04:52.950 --> 01:04:57.150 and it's going to store the answer to that question in a variable, depicted 01:04:57.150 --> 01:04:59.370 here as this blue oval, called answer. 01:04:59.370 --> 01:05:02.280 Just like in math, an x, a y, or a z. 01:05:02.280 --> 01:05:03.970 So what could I do with that. 01:05:03.970 --> 01:05:05.730 Well, let me again go to Looks. 01:05:05.730 --> 01:05:09.480 Let me go to say hello, but this time, you know what? 01:05:09.480 --> 01:05:13.410 Let me go ahead and say hello, comma, and then-- all right, 01:05:13.410 --> 01:05:15.490 let me give myself a second say block. 01:05:15.490 --> 01:05:16.990 But I don't want to say hello again. 01:05:16.990 --> 01:05:18.460 So I'm going to delete that. 01:05:18.460 --> 01:05:22.200 But I'm going to go back to Sensing and I'm going to drag and drop answer. 01:05:22.200 --> 01:05:24.990 Now it looks a little too big, but notice if I get close to it, 01:05:24.990 --> 01:05:27.000 it sort of magnetically wants to connect. 01:05:27.000 --> 01:05:30.490 And indeed, Scratch will grow to fill the puzzle piece for me. 01:05:30.490 --> 01:05:34.850 So now I have a program, it would seem, a program written in Scratch, 01:05:34.850 --> 01:05:38.100 a piece of software written in Scratch that's going to, when the green flag is 01:05:38.100 --> 01:05:41.100 clicked, ask what's your name, and wait-- that's our function-- 01:05:41.100 --> 01:05:42.060 say hello-- 01:05:42.060 --> 01:05:43.935 that's another function-- and then it's going 01:05:43.935 --> 01:05:45.840 to say answer, whatever the human typed in. 01:05:45.840 --> 01:05:48.990 Well, let me go over to Scratch's world here and click the green flag. 01:05:48.990 --> 01:05:50.940 Notice the cat is asking me what's your name. 01:05:50.940 --> 01:05:53.550 I type in David and enter. 01:05:53.550 --> 01:05:55.040 Huh. 01:05:55.040 --> 01:05:55.843 I only see David. 01:05:55.843 --> 01:05:57.260 Well, maybe I did something wrong. 01:05:57.260 --> 01:05:58.052 Let me do it again. 01:05:58.052 --> 01:06:00.720 Green flag, D-A-V-I-D, enter. 01:06:00.720 --> 01:06:02.370 Hmm. 01:06:02.370 --> 01:06:04.290 What's going on? 01:06:04.290 --> 01:06:08.430 This seems to be a bug, because I'm pretty sure I have three functions, 01:06:08.430 --> 01:06:10.680 ask, say, and say. 01:06:10.680 --> 01:06:13.680 But I feel like I'm missing the second instruction. 01:06:13.680 --> 01:06:17.880 Any thoughts on what bug I have made? 01:06:17.880 --> 01:06:19.185 What might explain this? 01:06:22.030 --> 01:06:23.860 Natalie, is it? 01:06:23.860 --> 01:06:26.957 AUDIENCE: So you replaced the output with the same function. 01:06:26.957 --> 01:06:27.790 DAVID J MALAN: Yeah. 01:06:27.790 --> 01:06:29.665 I replaced the output with the same function. 01:06:29.665 --> 01:06:33.070 And honestly, even though we're using a fairly simple program, Scratch, 01:06:33.070 --> 01:06:34.900 my Mac is actually pretty fast. 01:06:34.900 --> 01:06:37.600 And your PC or your Mac or your phone is pretty fast. 01:06:37.600 --> 01:06:42.130 And even though Scratch is saying hello and saying answer, as Natalie notes, 01:06:42.130 --> 01:06:46.030 the answer is sort of overwhelming to say, because I didn't so much as pause. 01:06:46.030 --> 01:06:48.220 So I could go in and find a block-- there's 01:06:48.220 --> 01:06:50.950 a wait block that could allow me to insert an arbitrary pause. 01:06:50.950 --> 01:06:52.790 But I really want this to be one breath. 01:06:52.790 --> 01:06:55.450 I want it to be hello, comma, David, all at once. 01:06:55.450 --> 01:06:56.540 So how can I do that? 01:06:56.540 --> 01:06:58.028 Well, let me go under Operations. 01:06:58.028 --> 01:07:00.820 And it turns out there's a whole bunch of math-related things here, 01:07:00.820 --> 01:07:04.930 but also some English or language-related things down here. 01:07:04.930 --> 01:07:06.135 Join apple banana. 01:07:06.135 --> 01:07:08.260 Now this has nothing to do with apples and bananas. 01:07:08.260 --> 01:07:10.885 Those are just placeholders, but there's this puzzle piece here 01:07:10.885 --> 01:07:12.160 that I can drag and drop. 01:07:12.160 --> 01:07:12.910 And you know what? 01:07:12.910 --> 01:07:14.077 Let me go ahead and do this. 01:07:14.077 --> 01:07:19.870 Let me replace the first input to say, and let me join hello comma, and then 01:07:19.870 --> 01:07:22.102 not banana, but let me drag the answer-- 01:07:22.102 --> 01:07:23.560 and notice that will drop in place. 01:07:23.560 --> 01:07:24.880 Let me throw this other block away. 01:07:24.880 --> 01:07:27.755 To delete things, you can just drag them over to the left and let go. 01:07:27.755 --> 01:07:31.300 And now notice that I have a program that's asking what's your name 01:07:31.300 --> 01:07:35.110 and then I'm going to say the result of joining hello and answer. 01:07:35.110 --> 01:07:37.972 And let me go ahead and play this now, after stopping the old one. 01:07:37.972 --> 01:07:38.680 What's your name? 01:07:38.680 --> 01:07:41.230 I type in David, enter, and voila. 01:07:41.230 --> 01:07:44.710 As Natalie notes, now it's not tripping over itself, 01:07:44.710 --> 01:07:46.270 clobbering what was previously there. 01:07:46.270 --> 01:07:48.070 Now I'm getting it all in one breath. 01:07:48.070 --> 01:07:50.320 Now the program is getting a little more interesting, 01:07:50.320 --> 01:07:52.880 but the paradigm is no different from before. 01:07:52.880 --> 01:07:56.230 In fact, let me propose that everything we've just done 01:07:56.230 --> 01:08:00.340 is fitting perfectly into this whole mental model of what 01:08:00.340 --> 01:08:03.140 it means to solve problems and what computer science itself is. 01:08:03.140 --> 01:08:06.670 So for instance, if this is the problem to be solved 01:08:06.670 --> 01:08:10.070 and I've got inputs and outputs are my goal and an algorithm in between, 01:08:10.070 --> 01:08:12.580 let's consider how Scratch even fits into this mental model. 01:08:12.580 --> 01:08:15.580 My input to the very first program that we wrote a moment ago 01:08:15.580 --> 01:08:18.220 was literally hello, world in its own oval. 01:08:18.220 --> 01:08:23.680 The algorithm was implemented as a function in Scratch called say. 01:08:23.680 --> 01:08:25.569 So an algorithm, step by step instructions, 01:08:25.569 --> 01:08:28.630 a function is the computer's implementation of an algorithm. 01:08:28.630 --> 01:08:30.399 In this case, a function called say. 01:08:30.399 --> 01:08:33.010 The output, of course, was the cat saying hello, world. 01:08:33.010 --> 01:08:37.300 But things got more interesting just now after Natalie's remark, whereby when I 01:08:37.300 --> 01:08:41.170 introduce something like ask what's your name and then wait, 01:08:41.170 --> 01:08:43.460 notice what happens this time in the model. 01:08:43.460 --> 01:08:46.420 Now the input to the problem is what's your name-- 01:08:46.420 --> 01:08:49.630 that's the string that comes by default, and I could change it but I didn't. 01:08:49.630 --> 01:08:52.330 That's being fed now into the ask block. 01:08:52.330 --> 01:08:55.600 And the ask block's purpose in life is to get the cat 01:08:55.600 --> 01:08:58.359 to give me an answer like this. 01:08:58.359 --> 01:09:02.080 Now, that answer is interesting because I can now join it 01:09:02.080 --> 01:09:04.580 in with the word hello as a prefix. 01:09:04.580 --> 01:09:07.000 So this block is interesting because notice, the input, 01:09:07.000 --> 01:09:10.930 the white oval to the say block actually has another puzzle piece and then 01:09:10.930 --> 01:09:12.760 two more puzzle pieces on top of it. 01:09:12.760 --> 01:09:16.090 And what's cool here is that when programming functions, 01:09:16.090 --> 01:09:20.529 you can have the outputs of one function become the input to another function. 01:09:20.529 --> 01:09:22.660 And so the flow here is quite simply this. 01:09:22.660 --> 01:09:26.560 Now I have two inputs to the function, both hello, which I wrote, 01:09:26.560 --> 01:09:30.010 and answer, which came from the ask block. 01:09:30.010 --> 01:09:34.359 The algorithm in question now is the join function, which I just used. 01:09:34.359 --> 01:09:37.540 And its output is hopefully going to be hello, comma, David. 01:09:37.540 --> 01:09:40.960 But I don't want to see a white oval on the screen saying hello, comma, David. 01:09:40.960 --> 01:09:43.359 I want the cat to say hello, comma, David. 01:09:43.359 --> 01:09:46.120 So let me go ahead and focus only on the output. 01:09:46.120 --> 01:09:50.410 Make it become the input to a final function, which is that say block, 01:09:50.410 --> 01:09:54.170 and voila, now the cat says what I want it to. 01:09:54.170 --> 01:09:58.570 So again, even as you start to nest, that is, place these puzzle pieces 01:09:58.570 --> 01:10:02.170 one on top of the other, all we're doing is passing in inputs 01:10:02.170 --> 01:10:03.040 and getting outputs. 01:10:03.040 --> 01:10:06.040 Doing something with those outputs and making them inputs, and so forth. 01:10:06.040 --> 01:10:10.030 That really is the paradigm, ultimately, of what it means to program. 01:10:10.030 --> 01:10:12.190 But we can make the cat do more interesting things. 01:10:12.190 --> 01:10:14.148 And just to have a little bit of fun with this, 01:10:14.148 --> 01:10:16.360 let me go ahead and dig in to this bottom icon 01:10:16.360 --> 01:10:17.740 at the bottom left of the screen. 01:10:17.740 --> 01:10:20.530 Scratch has these so-called extensions, where you can really 01:10:20.530 --> 01:10:22.460 make it do fancier things as well. 01:10:22.460 --> 01:10:25.030 And let me go to Text to Speech at the top right. 01:10:25.030 --> 01:10:27.100 So this is using a cloud-based service-- 01:10:27.100 --> 01:10:29.260 that is some internet-based service-- 01:10:29.260 --> 01:10:32.860 that's going to send the words that I type out on the internet. 01:10:32.860 --> 01:10:37.030 The internet, some server there, is going to respond with a verbalization 01:10:37.030 --> 01:10:39.067 now of what it is I just typed. 01:10:39.067 --> 01:10:40.400 So let me go ahead and try this. 01:10:40.400 --> 01:10:42.610 Let me get rid of the purple say function 01:10:42.610 --> 01:10:45.550 and replace it with this speak block. 01:10:45.550 --> 01:10:48.190 And let me go ahead and drag in the join puzzle piece 01:10:48.190 --> 01:10:49.900 here-- notice it's going to grow to fill, 01:10:49.900 --> 01:10:51.860 and I'm not going to use this one anymore. 01:10:51.860 --> 01:10:54.640 This time I'm going to hit Stop and I'm going to go ahead 01:10:54.640 --> 01:10:56.800 and hit Play once more and type in my name. 01:10:56.800 --> 01:10:57.780 And-- 01:10:57.780 --> 01:10:59.530 COMPUTER: (FEMININE ROBOTIC) Hello, David. 01:10:59.530 --> 01:11:01.530 DAVID J MALAN: OK, not a very natural cat sound, 01:11:01.530 --> 01:11:03.560 but notice we can set the voice differently. 01:11:03.560 --> 01:11:05.350 So notice I can drag this puzzle piece. 01:11:05.350 --> 01:11:07.690 And you can even squeeze blocks inside of others. 01:11:07.690 --> 01:11:09.490 Notice that it can go wherever you want. 01:11:09.490 --> 01:11:10.870 I'll put it at the very top here. 01:11:10.870 --> 01:11:12.953 So I could put it in a couple of different places. 01:11:12.953 --> 01:11:15.220 Right now the default voice is alto. 01:11:15.220 --> 01:11:16.840 Squeak sounds appropriate. 01:11:16.840 --> 01:11:18.790 Let's try that. 01:11:18.790 --> 01:11:20.587 Typing in my name, David. 01:11:20.587 --> 01:11:22.170 COMPUTER: (HIGH PITCHED) Hello, David. 01:11:22.170 --> 01:11:22.530 DAVID J MALAN: All right. 01:11:22.530 --> 01:11:23.610 Still not very catlike. 01:11:23.610 --> 01:11:27.720 Ironically, there is a kitten voice, which if I change it to kitten, 01:11:27.720 --> 01:11:29.520 we'll now hear this. 01:11:29.520 --> 01:11:31.482 Type in my name and enter. 01:11:31.482 --> 01:11:32.940 COMPUTER: (HIGH PITCHED) Meow meow. 01:11:32.940 --> 01:11:35.250 DAVID J MALAN: OK, so it doesn't really matter at that point what I type in. 01:11:35.250 --> 01:11:36.750 But now this is amazing. 01:11:36.750 --> 01:11:39.570 Like, we've gone from just saying hello, world to hello, 01:11:39.570 --> 01:11:41.220 David, which is dynamically changing. 01:11:41.220 --> 01:11:42.600 If you were to type your name, obviously, 01:11:42.600 --> 01:11:43.950 it would say your name instead. 01:11:43.950 --> 01:11:47.580 And now, thanks to the cloud, that is, servers on the internet, 01:11:47.580 --> 01:11:51.090 we're converting automatically text that the human has just 01:11:51.090 --> 01:11:53.190 provided into a sound file-- 01:11:53.190 --> 01:11:55.740 notes and durations and all of that-- 01:11:55.740 --> 01:11:57.700 into something my computer can now play. 01:11:57.700 --> 01:12:00.450 Well, let's actually make this cat sound a little more like a cat. 01:12:00.450 --> 01:12:02.242 Let me go ahead and get rid of those blocks 01:12:02.242 --> 01:12:07.080 here, and let me go and give myself now from the sound category, 01:12:07.080 --> 01:12:07.710 how about this? 01:12:07.710 --> 01:12:09.840 Play sound meow until done. 01:12:09.840 --> 01:12:11.370 Now, this is a simple program. 01:12:11.370 --> 01:12:14.550 When the green flag is clicked, play sound meow until done. 01:12:14.550 --> 01:12:15.130 Here we go. 01:12:15.130 --> 01:12:16.588 I'm going to go ahead and hit Play. 01:12:16.588 --> 01:12:17.900 [MEOW] 01:12:17.900 --> 01:12:19.160 All right, that's it. 01:12:19.160 --> 01:12:21.677 If I want to hear the cat meow again, I got to do it again. 01:12:21.677 --> 01:12:22.840 [MEOW] 01:12:22.840 --> 01:12:23.637 OK, that's great. 01:12:23.637 --> 01:12:26.095 I could kind of amuse myself for a while by just clicking-- 01:12:26.095 --> 01:12:26.595 [MEOW] 01:12:26.595 --> 01:12:27.170 --play, but-- 01:12:27.170 --> 01:12:27.670 [MEOW] 01:12:27.670 --> 01:12:29.170 --surely we can do better than this. 01:12:29.170 --> 01:12:30.360 You can imagine this-- 01:12:30.360 --> 01:12:31.120 [MEOW] 01:12:31.120 --> 01:12:32.540 --getting tedious quickly. 01:12:32.540 --> 01:12:35.397 So how might I get the cat to do this again and again? 01:12:35.397 --> 01:12:36.230 Well, you know what? 01:12:36.230 --> 01:12:39.400 Let me go ahead and let me just kind of grab a few of these. 01:12:39.400 --> 01:12:41.878 Meow, meow, meow, three times. 01:12:41.878 --> 01:12:44.170 So now that's two fewer times I have to hit the button. 01:12:44.170 --> 01:12:46.250 [MEOWING THREE TIMES RAPIDLY] 01:12:46.250 --> 01:12:46.750 All right. 01:12:46.750 --> 01:12:48.400 It doesn't seem like the happiest cat. 01:12:48.400 --> 01:12:53.110 So let me actually go to Control and let me give him a second break in between. 01:12:53.110 --> 01:12:55.210 Wait one second in between here. 01:12:55.210 --> 01:12:56.260 Now let me do it again. 01:12:56.260 --> 01:12:59.560 [MEOWING THREE TIMES] 01:12:59.560 --> 01:13:01.960 OK, slightly happier cat. 01:13:01.960 --> 01:13:03.900 But this seems a little messy now. 01:13:03.900 --> 01:13:05.080 This is correct. 01:13:05.080 --> 01:13:06.280 It is meowing three times. 01:13:06.280 --> 01:13:07.770 But let me go to the audience. 01:13:07.770 --> 01:13:09.080 Let's now consider design. 01:13:09.080 --> 01:13:11.830 Recall that we considered design in the context of the phone book. 01:13:11.830 --> 01:13:15.820 The third algorithm was better designed in that it was faster, 01:13:15.820 --> 01:13:18.940 it was more efficient, but there's another element 01:13:18.940 --> 01:13:23.140 to design, which is that you shouldn't repeat yourself if possible. 01:13:23.140 --> 01:13:25.668 So those of you who have programmed before 01:13:25.668 --> 01:13:27.460 might know what the solution here might be. 01:13:27.460 --> 01:13:31.840 Well, it turns out that go back to line three, we call a loop. 01:13:31.840 --> 01:13:34.060 Turns out Scratch supports these things called loops. 01:13:34.060 --> 01:13:36.185 And, in fact, there's one staring at me right here. 01:13:36.185 --> 01:13:39.100 If I zoom in on the left, notice that under the control blocks, 01:13:39.100 --> 01:13:41.053 these orange blocks there's a repeat block. 01:13:41.053 --> 01:13:43.720 And even though it says 10 by default, I bet we can change that. 01:13:43.720 --> 01:13:45.430 So let me drag that over here. 01:13:45.430 --> 01:13:49.210 Let me throw away a lot of this redundancy, this copy paste. 01:13:49.210 --> 01:13:52.480 Let me move these puzzle pieces inside of the repeat block, 01:13:52.480 --> 01:13:54.070 and it, too, will grow to fit them. 01:13:54.070 --> 01:13:55.090 Not a problem. 01:13:55.090 --> 01:13:56.980 Let me change the repeat to three. 01:13:56.980 --> 01:13:59.080 And now let me reconnect everything. 01:13:59.080 --> 01:14:01.090 And now the program is just tighter. 01:14:01.090 --> 01:14:04.690 It's using fewer puzzle pieces, or fewer lines of code, 01:14:04.690 --> 01:14:07.610 fewer steps, if you will, to achieve the same result. 01:14:07.610 --> 01:14:09.478 So now if I click the green flag-- 01:14:09.478 --> 01:14:13.230 [MEOWING THREE TIMES] 01:14:13.230 --> 01:14:14.123 --it's still working. 01:14:14.123 --> 01:14:16.540 So you could imagine changing this to any number you want. 01:14:16.540 --> 01:14:19.020 There's even a forever block, where we could do it forever, 01:14:19.020 --> 01:14:21.000 if the cat's going to do this in perpetuity. 01:14:21.000 --> 01:14:22.590 But it's a better program now. 01:14:22.590 --> 01:14:26.280 Now it is better designed, because if I want to change the amount of time 01:14:26.280 --> 01:14:28.620 the cat is waiting or if I want to change 01:14:28.620 --> 01:14:30.780 the total number of times the cat meows, I 01:14:30.780 --> 01:14:34.890 can change those details in one place, not in one or two or three, 01:14:34.890 --> 01:14:37.950 as by copying and pasting those same puzzle pieces. 01:14:37.950 --> 01:14:39.487 Well, what about that forever loop? 01:14:39.487 --> 01:14:41.320 What if you do want to do something forever? 01:14:41.320 --> 01:14:42.330 What might I want to do? 01:14:42.330 --> 01:14:44.010 Well, let's get the cat up and moving. 01:14:44.010 --> 01:14:46.410 Let me go under the motion category now. 01:14:46.410 --> 01:14:49.800 Let me go to point towards mouse pointer. 01:14:49.800 --> 01:14:51.240 So let me zoom in on this. 01:14:51.240 --> 01:14:54.510 And every time the cat points toward the mouse pointer, 01:14:54.510 --> 01:14:56.260 let's have him take one step. 01:14:56.260 --> 01:14:58.540 So I'm going to grab the move some number of steps, 01:14:58.540 --> 01:15:00.270 and I'm going to change the 10 to a one. 01:15:00.270 --> 01:15:01.860 And now I'm going to hit Play. 01:15:01.860 --> 01:15:04.350 And now we have our first program where the cat is 01:15:04.350 --> 01:15:07.075 kind of responding to my Mac's cursor. 01:15:07.075 --> 01:15:09.700 And I can move it around, and I can kind of get a little goofy, 01:15:09.700 --> 01:15:11.340 but it's taking me literally. 01:15:11.340 --> 01:15:15.068 It's pointing at the mouse cursor and it's then moving one step. 01:15:15.068 --> 01:15:16.360 Now, I can make it move faster. 01:15:16.360 --> 01:15:17.700 Let me stop this for a second. 01:15:17.700 --> 01:15:21.480 What if I move not one step at a time, but two steps at a time? 01:15:21.480 --> 01:15:24.330 And we'll see that now the cat is moving a little faster. 01:15:24.330 --> 01:15:25.560 Not quite super fast. 01:15:25.560 --> 01:15:28.420 Let's do 20 steps at a time and see what happens. 01:15:28.420 --> 01:15:30.450 And this is really the essence of animation. 01:15:30.450 --> 01:15:33.750 The more you adjust the number of steps or the number of changes happening 01:15:33.750 --> 01:15:36.150 to those pixels per second or per unit of time, 01:15:36.150 --> 01:15:38.580 the more that's going to happen visually on the screen. 01:15:38.580 --> 01:15:41.947 Well, what more can we do from just following? 01:15:41.947 --> 01:15:42.780 Well, you know what? 01:15:42.780 --> 01:15:45.540 If I have the ability now to have the cat follow me, 01:15:45.540 --> 01:15:47.450 let me try something else altogether. 01:15:47.450 --> 01:15:49.568 Let me go ahead and open up another extension. 01:15:49.568 --> 01:15:51.360 Let me go into the Pen tool, which is going 01:15:51.360 --> 01:15:55.620 to allow me now to draw with, like, a pencil or pen on the screen. 01:15:55.620 --> 01:16:00.320 And let me go ahead and still have the cat follow me, I think-- 01:16:00.320 --> 01:16:01.320 actually, you know what? 01:16:01.320 --> 01:16:02.280 Let's change this. 01:16:02.280 --> 01:16:04.230 Let's just have him go to where I am. 01:16:04.230 --> 01:16:06.960 So there's another block that says go to random position. 01:16:06.960 --> 01:16:07.930 I don't want that. 01:16:07.930 --> 01:16:10.200 So I'm going to change it by the little triangle menu 01:16:10.200 --> 01:16:11.820 here, to go to the mouse pointer. 01:16:11.820 --> 01:16:15.270 So now, forever, the cat's just going to go to where the mouse pointer is. 01:16:15.270 --> 01:16:18.210 It's not going to glide or do it slowly or quickly. 01:16:18.210 --> 01:16:20.550 It's just going to go to wherever the cursor is. 01:16:20.550 --> 01:16:23.970 And let me go now to this new Pen category down below. 01:16:23.970 --> 01:16:25.565 And how might I do this? 01:16:25.565 --> 01:16:26.440 You know what I want? 01:16:26.440 --> 01:16:28.470 I want this cat to be able to draw for me. 01:16:28.470 --> 01:16:31.080 When I move the cursor up, down, left, right, I 01:16:31.080 --> 01:16:33.780 want to actually draw something with ink on the screen. 01:16:33.780 --> 01:16:37.590 But I only want to draw something when the pen is down. 01:16:37.590 --> 01:16:41.520 Notice on the left that two of the two puzzle pieces I just introduced over 01:16:41.520 --> 01:16:45.240 here at left are pen down and pen up. 01:16:45.240 --> 01:16:48.090 But there's a piece of missing logic here. 01:16:48.090 --> 01:16:53.550 Let me ask the audience how might we go about enhancing this program 01:16:53.550 --> 01:16:58.500 so not only does the cat follow my cursor, but I also draw on the screen? 01:16:58.500 --> 01:17:02.880 Nicholas, what kinds of solutions would you propose? 01:17:02.880 --> 01:17:07.257 AUDIENCE: So what you could do is take an if statement-- 01:17:07.257 --> 01:17:09.840 so you can control when the pen is up or when the pen is down, 01:17:09.840 --> 01:17:12.630 depending on some condition that you have. 01:17:12.630 --> 01:17:15.420 Like, I know a lot of things, you draw with the mouse click, 01:17:15.420 --> 01:17:18.850 if the mouse is on, then you can say the pen is down. 01:17:18.850 --> 01:17:21.720 And when the mouse click is not on, your pen is up. 01:17:21.720 --> 01:17:24.330 And then while it follows it forever it also 01:17:24.330 --> 01:17:26.700 senses to see if your mouse click is on or off. 01:17:26.700 --> 01:17:27.697 I don't really know. 01:17:27.697 --> 01:17:29.280 DAVID J MALAN: No, you really do know. 01:17:29.280 --> 01:17:30.450 That was, like, perfect. 01:17:30.450 --> 01:17:32.820 Because you took this principle of having the forever 01:17:32.820 --> 01:17:35.580 block not only go to the mouse pointer, but you proposed 01:17:35.580 --> 01:17:37.590 asking a question by a condition. 01:17:37.590 --> 01:17:39.450 So let me actually go under Control, where 01:17:39.450 --> 01:17:44.070 I happen to know this puzzle piece is, and notice similar to our phone book 01:17:44.070 --> 01:17:48.420 pseudocode, where I said if else, if else, if else, well, 01:17:48.420 --> 01:17:52.340 here there's only two questions, I think, as you're proposing. 01:17:52.340 --> 01:17:55.210 Is the mouse button down or up? 01:17:55.210 --> 01:17:57.700 So I think we can get away with just an if else. 01:17:57.700 --> 01:18:01.200 So let me go ahead and drag this below the go to mouse pointer. 01:18:01.200 --> 01:18:04.300 And then notice this little trapezoid-like shape in the middle 01:18:04.300 --> 01:18:04.800 here. 01:18:04.800 --> 01:18:07.570 Let me go to Sensing here. 01:18:07.570 --> 01:18:09.990 And notice if I scroll down-- yep, there it is. 01:18:09.990 --> 01:18:11.520 On the left, notice this one? 01:18:11.520 --> 01:18:13.200 Mouse down, question mark? 01:18:13.200 --> 01:18:15.090 These are our Boolean expressions. 01:18:15.090 --> 01:18:18.052 Let me drag that Boolean expression into that similar shape. 01:18:18.052 --> 01:18:19.260 It's going to grow to fit it. 01:18:19.260 --> 01:18:20.510 And then what do I want to do? 01:18:20.510 --> 01:18:24.020 If the mouse is down, I think I want to put the pen down. 01:18:24.020 --> 01:18:28.950 Else, if the mouse is implicitly up, let me go ahead and put the pen up 01:18:28.950 --> 01:18:29.820 like this. 01:18:29.820 --> 01:18:31.290 Well, let me go ahead and full screen this, just 01:18:31.290 --> 01:18:32.540 so we can see a little better. 01:18:32.540 --> 01:18:33.450 Let me hit Play. 01:18:33.450 --> 01:18:35.820 And now the cat is following me, as promised. 01:18:35.820 --> 01:18:37.230 But this is now a drawing cat. 01:18:37.230 --> 01:18:44.470 If I click the mouse button, I can say something like, very poorly in cursive, 01:18:44.470 --> 01:18:45.580 Hello. 01:18:45.580 --> 01:18:46.150 Sort of. 01:18:46.150 --> 01:18:47.920 Been a long time since I've done cursive. 01:18:47.920 --> 01:18:50.050 So we now have the cat actually drawing something. 01:18:50.050 --> 01:18:52.270 And honestly, it's a little ridiculous that it's a cat drawing. 01:18:52.270 --> 01:18:53.050 But you know what? 01:18:53.050 --> 01:18:54.520 Scratch has these costumes. 01:18:54.520 --> 01:18:57.880 We could go at top left here, and even though Scratch comes with two cat 01:18:57.880 --> 01:19:01.060 costumes, we could change it to be a pen or a marker or, really, anything 01:19:01.060 --> 01:19:01.710 we want. 01:19:01.710 --> 01:19:03.460 Because at the end of the day, this sprite 01:19:03.460 --> 01:19:05.920 is really just a character on the screen that 01:19:05.920 --> 01:19:08.662 can take any form that we might want. 01:19:08.662 --> 01:19:10.120 Well, how can we take this further? 01:19:10.120 --> 01:19:13.060 I like this introduction of conditions and loops, 01:19:13.060 --> 01:19:15.620 but there's some other principles we can introduce here. 01:19:15.620 --> 01:19:19.330 Let me go ahead and start a new program here altogether. 01:19:19.330 --> 01:19:21.700 And let's see if we can't start counting up 01:19:21.700 --> 01:19:23.630 and start keeping track of information. 01:19:23.630 --> 01:19:25.390 So for this time, let's do this. 01:19:25.390 --> 01:19:29.033 When the green flag is clicked, this time let's go under variables 01:19:29.033 --> 01:19:30.700 and let's give ourselves a new variable. 01:19:30.700 --> 01:19:33.820 Scratch lets you create puzzle pieces, this one being a variable, 01:19:33.820 --> 01:19:35.830 and I'm going to call this a counter. 01:19:35.830 --> 01:19:38.650 Just something that's going to keep count from one on up. 01:19:38.650 --> 01:19:42.160 Now this has given me some custom puzzle pieces over here 01:19:42.160 --> 01:19:45.280 called counter, and then the default my variable, which was there already. 01:19:45.280 --> 01:19:46.863 And I'm going to go ahead and do this. 01:19:46.863 --> 01:19:49.960 I'm going to set the counter initially equal to one. 01:19:49.960 --> 01:19:51.790 And then I'm going to do something forever. 01:19:51.790 --> 01:19:53.560 Let me grab one of those forever blocks. 01:19:53.560 --> 01:19:55.820 And I want the cat now to do this forever. 01:19:55.820 --> 01:20:00.160 I want it to just say whatever the current count is. 01:20:00.160 --> 01:20:02.200 So I don't want it to say hello for two seconds. 01:20:02.200 --> 01:20:05.620 I want it to say something for one second, let's say. 01:20:05.620 --> 01:20:07.880 So I'm going to go back to variables. 01:20:07.880 --> 01:20:11.980 And I'm going to grab this new circular shape, counter, that I created 01:20:11.980 --> 01:20:13.365 and drag it right there. 01:20:13.365 --> 01:20:15.240 So you can read this literally top to bottom. 01:20:15.240 --> 01:20:18.890 So counter to one, then forever say the counter for one second. 01:20:18.890 --> 01:20:22.990 But if we don't want the cat to say the same number again and again and again, 01:20:22.990 --> 01:20:26.260 let's go ahead and change the counter by one. 01:20:26.260 --> 01:20:29.080 And that's implicitly going to add one to the counter. 01:20:29.080 --> 01:20:31.990 Now if I go ahead and hit Play, we see a cat 01:20:31.990 --> 01:20:34.338 that's counting from one to two to three, 01:20:34.338 --> 01:20:36.880 and it's going to count up, ideally, all the way to infinity. 01:20:36.880 --> 01:20:39.010 The difference being now, we have this feature 01:20:39.010 --> 01:20:41.800 of actually using a variable, a variable that's keeping 01:20:41.800 --> 01:20:44.290 track of some amount of information. 01:20:44.290 --> 01:20:47.080 In this case, the number that's constantly being updated, 01:20:47.080 --> 01:20:49.490 and the screen is being redrawn again and again. 01:20:49.490 --> 01:20:51.490 Well, now let me go ahead and just start opening 01:20:51.490 --> 01:20:53.680 a few programs that I wrote in advance, just 01:20:53.680 --> 01:20:55.480 so that we can get a tour of some of those. 01:20:55.480 --> 01:20:58.997 I've got this program called Bounce that works a little something like this. 01:20:58.997 --> 01:21:00.580 And this, too, is part of programming. 01:21:00.580 --> 01:21:03.417 Not only writing your own code, but reading your own code. 01:21:03.417 --> 01:21:06.250 And let me go ahead and zoom in on this, which I've already created, 01:21:06.250 --> 01:21:08.170 and consider what it says. 01:21:08.170 --> 01:21:09.965 First set, rotation style, left, right. 01:21:09.965 --> 01:21:13.090 This is just a fix what would otherwise be a bug where the cat accidentally 01:21:13.090 --> 01:21:14.110 ends up upside down. 01:21:14.110 --> 01:21:15.662 But let me wave my hand at that. 01:21:15.662 --> 01:21:16.870 This is the interesting part. 01:21:16.870 --> 01:21:19.670 Forever have the cat moved 10 steps. 01:21:19.670 --> 01:21:25.490 And then if it's touching the edge, then turn around 180 degrees. 01:21:25.490 --> 01:21:27.940 So now we can reintroduce the idea of animation. 01:21:27.940 --> 01:21:30.670 But not that's driven by me, the human, with my cursor. 01:21:30.670 --> 01:21:35.890 I can now make a game, and interactive piece of art, or anything 01:21:35.890 --> 01:21:37.540 now where the cat is self-driven. 01:21:37.540 --> 01:21:40.390 Because when I hit Play now, notice that it's 01:21:40.390 --> 01:21:42.620 moving back and forth, back and forth. 01:21:42.620 --> 01:21:45.250 And if it is touching the edge and the answer 01:21:45.250 --> 01:21:50.740 to that Boolean question is, actually, yes or true or one, 01:21:50.740 --> 01:21:53.780 then it's going to turn 180 degrees. 01:21:53.780 --> 01:21:56.037 But this looks kind of stupid, admittedly. 01:21:56.037 --> 01:21:58.370 You know, one, the cat, yes, is bouncing off the screen, 01:21:58.370 --> 01:21:59.530 which is maybe a little unrealistic. 01:21:59.530 --> 01:22:01.030 But he's not really walking. 01:22:01.030 --> 01:22:02.360 He's gliding. 01:22:02.360 --> 01:22:04.150 But this is the thing about animation. 01:22:04.150 --> 01:22:07.120 Just as we noted before that videos, at the end of the day, 01:22:07.120 --> 01:22:10.857 are really just images flying across the screen-- 01:22:10.857 --> 01:22:11.440 you know what? 01:22:11.440 --> 01:22:15.220 I bet we can create our own illusion of movement, just like in a real video, 01:22:15.220 --> 01:22:18.850 by taking not just one costume, the cat with his feet like this. 01:22:18.850 --> 01:22:22.630 What if we gave ourselves a second costume, where it's almost the same 01:22:22.630 --> 01:22:24.760 but his feet are slightly differently positioned? 01:22:24.760 --> 01:22:27.970 Just like the paper based flipbook that we looked at earlier. 01:22:27.970 --> 01:22:28.720 And you know what? 01:22:28.720 --> 01:22:32.440 I bet if I toggle between these two costumes, 01:22:32.440 --> 01:22:34.930 changing the condition of the cat again and again, 01:22:34.930 --> 01:22:38.360 I bet we can create the illusion of actual movement. 01:22:38.360 --> 01:22:41.080 And that's what we have here in this other bounce example. 01:22:41.080 --> 01:22:44.530 In this other bounce example, we have the cat now moving 01:22:44.530 --> 01:22:48.010 not only back and forth, but notice this purple puzzle piece. 01:22:48.010 --> 01:22:52.210 After it bounces off the edge, or considers bouncing off the edge, 01:22:52.210 --> 01:22:55.630 it constantly changes its costume to the next one, to the next one, 01:22:55.630 --> 01:22:58.670 to the next one, essentially alternating between the two. 01:22:58.670 --> 01:23:01.000 So now it's not quite perfect. 01:23:01.000 --> 01:23:02.920 Like, it has what we call very low frame rate. 01:23:02.920 --> 01:23:05.530 This is like watching a really bad animated GIF online 01:23:05.530 --> 01:23:08.680 that only has two different frames in it. 01:23:08.680 --> 01:23:11.230 But it looks more like he's walking and much less 01:23:11.230 --> 01:23:14.470 like he's gliding back and forth on the screen. 01:23:14.470 --> 01:23:16.510 So we can actually have some fun with this, too. 01:23:16.510 --> 01:23:18.112 Scratch support sounds. 01:23:18.112 --> 01:23:20.320 So, for instance, here's the meow we've heard before. 01:23:20.320 --> 01:23:21.160 [MEOW] 01:23:21.160 --> 01:23:24.910 I can record my own, though, if I click this little plus icon down here. 01:23:24.910 --> 01:23:28.060 Click Record, and allow Scratch to access my microphone. 01:23:28.060 --> 01:23:29.690 Click OK a couple of times. 01:23:29.690 --> 01:23:30.190 Here we go. 01:23:30.190 --> 01:23:32.140 Let me record my own voice. 01:23:32.140 --> 01:23:33.140 Ouch. 01:23:33.140 --> 01:23:33.640 All right. 01:23:33.640 --> 01:23:36.432 That's what the word Ouch looks like, at least when I pronounce it. 01:23:36.432 --> 01:23:38.620 I can trim off the beginning here. 01:23:38.620 --> 01:23:39.760 Let me save that. 01:23:39.760 --> 01:23:42.000 I'm going to give this recording a name, Ouch, 01:23:42.000 --> 01:23:43.952 and now let me go back to my code. 01:23:43.952 --> 01:23:45.660 And under the sound block, you know what? 01:23:45.660 --> 01:23:48.540 Let me go ahead and say this. 01:23:48.540 --> 01:23:52.330 If I'm touching the edge, not only do I want to turn 180 degrees. 01:23:52.330 --> 01:23:56.470 Now I can kind of make this a little more playful. 01:23:56.470 --> 01:23:57.790 COMPUTER: Ouch. 01:23:57.790 --> 01:23:59.080 Ouch. 01:23:59.080 --> 01:23:59.590 Ouch. 01:23:59.590 --> 01:23:59.860 DAVID J MALAN: All right. 01:23:59.860 --> 01:24:02.693 Still not very catlike, but again, we're just layering and layering. 01:24:02.693 --> 01:24:05.655 And the takeaway here really is, as these programs 01:24:05.655 --> 01:24:08.780 get more and more complicated, the goal should never be, when writing code, 01:24:08.780 --> 01:24:10.540 whether it's in Scratch or C or eventually 01:24:10.540 --> 01:24:13.210 Python in this class or others, to just start and try 01:24:13.210 --> 01:24:14.920 to implement your entire vision. 01:24:14.920 --> 01:24:17.920 Notice with every one of these programs that I wrote from scratch, 01:24:17.920 --> 01:24:22.180 no pun intended, did I start small and add one or two or three puzzle 01:24:22.180 --> 01:24:25.810 pieces, building up from something simple to something more complex. 01:24:25.810 --> 01:24:26.560 And you know what? 01:24:26.560 --> 01:24:30.130 I bet if we synthesize some of these ideas, we can do yet other things too. 01:24:30.130 --> 01:24:33.370 Here's another example that involves, perhaps, petting a cat. 01:24:33.370 --> 01:24:35.380 Let me go ahead and see inside this program. 01:24:35.380 --> 01:24:40.090 This one's relatively simple, but it's not doing anything just yet. 01:24:40.090 --> 01:24:41.710 I already hit the green flag. 01:24:41.710 --> 01:24:45.190 Let me Zoom in on the code, and you can, perhaps, now read my own code 01:24:45.190 --> 01:24:46.660 that I wrote in advance. 01:24:46.660 --> 01:24:50.650 The cat is forever asking the question, if touching mouse pointer then 01:24:50.650 --> 01:24:52.650 play that sound meow until done. 01:24:52.650 --> 01:24:55.150 Well, it would seem that even though the program is running, 01:24:55.150 --> 01:24:56.350 it's not doing anything. 01:24:56.350 --> 01:24:56.980 But it is. 01:24:56.980 --> 01:24:59.060 It's waiting for something to happen. 01:24:59.060 --> 01:25:01.780 So let me move my cursor over the cat like-- 01:25:01.780 --> 01:25:03.160 [MEOW] 01:25:03.160 --> 01:25:04.080 --this. 01:25:04.080 --> 01:25:05.920 [MEOW] 01:25:05.920 --> 01:25:08.890 So it would seem, and if I leave it on there, he'll keep meowing. 01:25:08.890 --> 01:25:11.590 And it's kind of like a program that's petting a cat. 01:25:11.590 --> 01:25:13.660 And so you can imagine now having conditions 01:25:13.660 --> 01:25:16.270 inside of loops that are using Boolean expressions to decide 01:25:16.270 --> 01:25:18.550 exactly what you want something to do. 01:25:18.550 --> 01:25:22.460 And even more powerfully, even in a language like Scratch can we do this. 01:25:22.460 --> 01:25:26.530 Let me open up the sea lion here, who has a very distinct bark. 01:25:26.530 --> 01:25:30.280 But he's demonstrative now of a program that has multiple scripts. 01:25:30.280 --> 01:25:33.730 So inside of this Scratch project now, we're not just one program but two. 01:25:33.730 --> 01:25:36.682 Notice both of which start when a green flag is clicked. 01:25:36.682 --> 01:25:38.390 And let me put them both onto the screen. 01:25:38.390 --> 01:25:39.290 And it looks longer. 01:25:39.290 --> 01:25:41.930 But that's just because the puzzle pieces are growing to fit each other. 01:25:41.930 --> 01:25:43.360 Let's go ahead and hit Play on this. 01:25:43.360 --> 01:25:43.860 Play 01:25:43.860 --> 01:25:47.040 [SEA LION BARKING] 01:25:47.040 --> 01:25:51.420 Notice that every second or so, the sea lion is barking. 01:25:51.420 --> 01:25:53.490 And frankly, this gets annoying quickly. 01:25:53.490 --> 01:25:54.840 But how can I stop it? 01:25:54.840 --> 01:25:58.230 Well, let me go ahead and look over here on the left while it's still barking. 01:25:58.230 --> 01:26:01.830 Notice the sea lion is forever asking a question. 01:26:01.830 --> 01:26:07.090 If muted equals false, start sounds sea lion, think hi hi hi for two seconds. 01:26:07.090 --> 01:26:07.980 So what is muted? 01:26:07.980 --> 01:26:09.780 Well, the shape of it, recall, represents 01:26:09.780 --> 01:26:13.870 a variable, like x or y or z, which is just some way of retaining information. 01:26:13.870 --> 01:26:17.880 So this is like saying, is the value of the muted variable false? 01:26:17.880 --> 01:26:22.590 If so, you should bark, because if it's false muted, if it's not muted, 01:26:22.590 --> 01:26:24.480 go ahead and play the sea lion sound. 01:26:24.480 --> 01:26:27.900 But my god, lets-- on the right here, notice there's another program. 01:26:27.900 --> 01:26:31.050 When the green flag is clicked, forever ask the question. 01:26:31.050 --> 01:26:34.980 If the spacebar is pressed, then if muted is true, 01:26:34.980 --> 01:26:38.560 set muted to false, else set muted to true. 01:26:38.560 --> 01:26:42.240 So the program on the right is going to change the value of muted 01:26:42.240 --> 01:26:44.270 from false to true or true to false. 01:26:44.270 --> 01:26:45.780 Because, my god. 01:26:45.780 --> 01:26:47.325 I've hit the space bar-- 01:26:47.325 --> 01:26:48.540 [SEA LION BARKING] 01:26:48.540 --> 01:26:52.160 And now it's over. 01:26:52.160 --> 01:26:54.380 The program is still running, but it's no longer 01:26:54.380 --> 01:26:58.397 playing because muted is now true and not false. 01:26:58.397 --> 01:26:59.480 Well, what else can we do? 01:26:59.480 --> 01:27:01.640 Things can get pretty fancy pretty quickly. 01:27:01.640 --> 01:27:04.220 Let me go ahead and create one other program here. 01:27:04.220 --> 01:27:06.770 And I'll go ahead and do one with just two blocks. 01:27:06.770 --> 01:27:10.820 This one-- let me go into the extensions again, video sensing this time, 01:27:10.820 --> 01:27:13.940 and notice there's different types of ways to start programs. 01:27:13.940 --> 01:27:17.060 Not every program has to start when you click the green flag. 01:27:17.060 --> 01:27:19.490 There's a similar shape here, but this one in green, that 01:27:19.490 --> 01:27:21.290 says when video motion is greater than 10. 01:27:21.290 --> 01:27:23.030 Like 10% of the screen is moving. 01:27:23.030 --> 01:27:25.010 Let me increase that to 50%. 01:27:25.010 --> 01:27:26.810 And let me go ahead and do this. 01:27:26.810 --> 01:27:30.050 Let me go ahead and find the sound puzzle piece. 01:27:30.050 --> 01:27:31.850 Play sound meow until done. 01:27:31.850 --> 01:27:34.520 So now I have a two block program. 01:27:34.520 --> 01:27:38.780 When video motion is more than 50, play sound meow until done. 01:27:38.780 --> 01:27:39.488 Let me zoom out. 01:27:39.488 --> 01:27:41.780 And you'll notice that I'm actually in the screen here. 01:27:41.780 --> 01:27:43.640 Let me move off stage. 01:27:43.640 --> 01:27:47.040 And now nothing is happening. 01:27:47.040 --> 01:27:49.055 Let me go and pet the cat, though. 01:27:49.055 --> 01:27:51.180 [MEOW] 01:27:51.180 --> 01:27:52.385 Let me do it again. 01:27:52.385 --> 01:27:53.570 [MEOW] 01:27:53.570 --> 01:27:54.300 And again. 01:27:54.300 --> 01:27:55.970 So it's using my computer's camera-- 01:27:55.970 --> 01:27:56.510 [MEOW] 01:27:56.510 --> 01:28:00.408 --detecting motion, and then executing that particular program. 01:28:00.408 --> 01:28:02.450 So again, with just these simple building blocks, 01:28:02.450 --> 01:28:04.880 can we get more and more interesting things to happen. 01:28:04.880 --> 01:28:05.630 And you know what? 01:28:05.630 --> 01:28:07.310 We can even have multiple sprites. 01:28:07.310 --> 01:28:09.410 Let me go ahead and open up an old school game 01:28:09.410 --> 01:28:11.270 that you might have played in, like, a swimming pool, perhaps, 01:28:11.270 --> 01:28:14.330 growing up, where one person yells out Marco and the other people 01:28:14.330 --> 01:28:16.100 are supposed to yell out Polo. 01:28:16.100 --> 01:28:19.010 Notice here we have a program with two sprites. 01:28:19.010 --> 01:28:21.680 So two puppets, an orange puppet and a blue puppet. 01:28:21.680 --> 01:28:24.390 And down here at the bottom, for the very first time, 01:28:24.390 --> 01:28:27.440 We have two different sprites' abilities to write programs. 01:28:27.440 --> 01:28:29.610 So right now the orange puppet is selected, 01:28:29.610 --> 01:28:31.670 which means the program at top left here, 01:28:31.670 --> 01:28:34.072 up here, belongs to the orange puppet. 01:28:34.072 --> 01:28:35.780 And the orange puppet has been programmed 01:28:35.780 --> 01:28:39.440 to say forever, if the keyboard's space key is pressed, 01:28:39.440 --> 01:28:41.240 then say Marco for two seconds. 01:28:41.240 --> 01:28:42.650 And then here's the new feature. 01:28:42.650 --> 01:28:46.040 There's a way in programming to have, like, one program talk 01:28:46.040 --> 01:28:49.070 to another, or in this case, one sprite talk to another. 01:28:49.070 --> 01:28:52.440 Sort of passing a secret message that you don't see on the screen. 01:28:52.440 --> 01:28:54.410 But one program can hear from another. 01:28:54.410 --> 01:28:56.540 And that's called broadcasting an event. 01:28:56.540 --> 01:28:58.430 And that's what the orange puppet is doing. 01:28:58.430 --> 01:29:00.627 If I click on the blue puppet's icon here, 01:29:00.627 --> 01:29:02.210 he's not going to do very much at all. 01:29:02.210 --> 01:29:05.010 But instead of doing anything when the green flag is clicked, 01:29:05.010 --> 01:29:07.700 instead of doing something when the camera sees motion, 01:29:07.700 --> 01:29:14.090 he instead is going to, when he receives the event, say Polo for two seconds. 01:29:14.090 --> 01:29:17.690 And so in this case, if I hit Play now, nothing happens yet. 01:29:17.690 --> 01:29:23.300 But when I do hit the spacebar, orange says Marco, blue says Polo. 01:29:23.300 --> 01:29:25.550 But they are written independently. 01:29:25.550 --> 01:29:28.250 I've written one program for orange, one program for blue, 01:29:28.250 --> 01:29:30.440 and they're somehow communicating. 01:29:30.440 --> 01:29:32.540 And speaking of communicating, there's even 01:29:32.540 --> 01:29:36.230 more things you can do these days thanks to the internet and the cloud. 01:29:36.230 --> 01:29:40.130 Let me go ahead and open up one other new canvas here. 01:29:40.130 --> 01:29:42.710 Very quickly give myself a when green flag clicked. 01:29:42.710 --> 01:29:45.950 Let me go ahead and ask that same question before, ask what's your name 01:29:45.950 --> 01:29:46.490 and wait. 01:29:46.490 --> 01:29:48.710 But now let me go into these extensions and let 01:29:48.710 --> 01:29:50.990 me find the translate extension, which is, again, 01:29:50.990 --> 01:29:54.620 going to use the cloud to send whatever I type in out on the internet 01:29:54.620 --> 01:29:57.840 and get back a response, and then say it on the screen here. 01:29:57.840 --> 01:30:01.680 So let me go ahead and say something on the screen, like say hello. 01:30:01.680 --> 01:30:02.930 But I don't want to say hello. 01:30:02.930 --> 01:30:05.360 I want to go back to the Translate category, 01:30:05.360 --> 01:30:07.357 and I want to go ahead and translate-- 01:30:07.357 --> 01:30:07.940 you know what? 01:30:07.940 --> 01:30:08.780 I like this block. 01:30:08.780 --> 01:30:11.250 Translate something to another language. 01:30:11.250 --> 01:30:14.390 But let me get one of those join blocks again, and let me go ahead 01:30:14.390 --> 01:30:19.200 and join the word hello and then the name that the person has typed in. 01:30:19.200 --> 01:30:21.690 So to get that, I need the answer block again. 01:30:21.690 --> 01:30:24.480 So I'm just recreating some of our blocks from earlier. 01:30:24.480 --> 01:30:26.780 And notice, before I just did this. 01:30:26.780 --> 01:30:31.400 I said the result of joining hello and answer, albeit with a comma last time. 01:30:31.400 --> 01:30:32.880 But now let's do this. 01:30:32.880 --> 01:30:37.370 Let me take the output of join, make it the input to translate. 01:30:37.370 --> 01:30:39.590 Let me translate, say, to Arabic here. 01:30:39.590 --> 01:30:41.700 Let me drag and drop into the say block. 01:30:41.700 --> 01:30:46.100 So now we have two inputs going into join, join's output going 01:30:46.100 --> 01:30:50.570 into the input of translate, and the output of translate going into say. 01:30:50.570 --> 01:30:55.400 But the net result is going to be I'll type in my name David and hit Enter. 01:30:55.400 --> 01:30:57.560 Hello, David, now in Arabic. 01:30:57.560 --> 01:31:00.920 All thanks to these principles of functions, conditions, and loops, 01:31:00.920 --> 01:31:03.770 and now even adding in the internet. 01:31:03.770 --> 01:31:07.190 Now let's consider finally, before we play a final couple of games. 01:31:07.190 --> 01:31:10.340 In conclusion, there's a way to even improve 01:31:10.340 --> 01:31:12.270 the design of a lot of what we've done. 01:31:12.270 --> 01:31:16.610 In fact, let me go back just a moment to where we left off with that meowing. 01:31:16.610 --> 01:31:19.370 And in one of our meowing examples, we had 01:31:19.370 --> 01:31:22.490 code that looked like this, where I repeated three times, 01:31:22.490 --> 01:31:26.720 recall, and I played the sound meow again and again and again. 01:31:26.720 --> 01:31:29.070 And I argued at the time that this was better designed. 01:31:29.070 --> 01:31:29.570 Why? 01:31:29.570 --> 01:31:32.370 Because I didn't just drag and drop the same puzzle piece again 01:31:32.370 --> 01:31:33.260 and again and again. 01:31:33.260 --> 01:31:36.560 I used a repeat block, I threw away all of the redundancy, 01:31:36.560 --> 01:31:38.150 and I've arguably kept it simple. 01:31:38.150 --> 01:31:40.790 I'm using some fancier ideas, but the code is simpler 01:31:40.790 --> 01:31:42.860 and it's fewer puzzle pieces now. 01:31:42.860 --> 01:31:45.140 But it turns out that there's a missed opportunity 01:31:45.140 --> 01:31:47.840 here to apply another principle of computer science, 01:31:47.840 --> 01:31:50.750 and this is what we would generally describe as abstraction. 01:31:50.750 --> 01:31:53.300 Abstraction is this amazing problem solving 01:31:53.300 --> 01:31:55.460 technique that's really just a fancy way of saying 01:31:55.460 --> 01:31:59.150 let's take a very complicated idea, or a slightly complicated idea 01:31:59.150 --> 01:32:04.670 and simplify it in such a way that we all agree that you can implement it 01:32:04.670 --> 01:32:10.040 the complicated way, but let's now just stipulate 01:32:10.040 --> 01:32:14.430 that we're going to think about it as on a more simple level. 01:32:14.430 --> 01:32:17.510 So let me go over to this same program. 01:32:17.510 --> 01:32:18.260 And you know what? 01:32:18.260 --> 01:32:22.250 Scratch, curiously, did not anticipate having a meow block. 01:32:22.250 --> 01:32:27.350 Like, there is a say block and there's a think block, but there's no meow block. 01:32:27.350 --> 01:32:31.100 And that seems appropriate for a program where it comes with a cat built in. 01:32:31.100 --> 01:32:32.030 So we can do this. 01:32:32.030 --> 01:32:35.340 Just as you can create your own variables, notice at bottom left here, 01:32:35.340 --> 01:32:38.610 you can create your own blocks with this pink category. 01:32:38.610 --> 01:32:40.430 And if I go here, I'm going to make a block 01:32:40.430 --> 01:32:42.440 and I'm going to call this block meow. 01:32:42.440 --> 01:32:45.840 And quite simply, I'm going to click OK. 01:32:45.840 --> 01:32:49.160 Now notice I get this new puzzle piece that says define meow. 01:32:49.160 --> 01:32:51.590 And it's ready to have other pieces connected to it. 01:32:51.590 --> 01:32:53.130 How am I going to define meow? 01:32:53.130 --> 01:32:56.005 I'm just going to go ahead and drag this over here, because I already 01:32:56.005 --> 01:32:57.200 implemented meow before. 01:32:57.200 --> 01:32:59.960 And now, notice what I have on the left hand side. 01:32:59.960 --> 01:33:03.230 Because I've just made this custom block or puzzle piece, 01:33:03.230 --> 01:33:07.370 I now have a pink piece called meow, just as though it came with Scratch. 01:33:07.370 --> 01:33:10.640 And now what's compelling about this is that I can sort of think 01:33:10.640 --> 01:33:12.350 of this as out of sight, out of mind. 01:33:12.350 --> 01:33:14.190 Who cares how meow is implemented? 01:33:14.190 --> 01:33:16.040 We know we implemented it earlier. 01:33:16.040 --> 01:33:19.380 Let's now just stipulate that we can take for granted it exists. 01:33:19.380 --> 01:33:23.210 And if I zoom in now on the new program, now it's more readable in some sense. 01:33:23.210 --> 01:33:24.290 It's a little shorter. 01:33:24.290 --> 01:33:26.060 It has a fewer puzzle piece. 01:33:26.060 --> 01:33:29.210 But it also is more self-descriptive. 01:33:29.210 --> 01:33:30.202 I can read my code. 01:33:30.202 --> 01:33:31.910 I can look at this code and say, OK, it's 01:33:31.910 --> 01:33:34.850 obviously going to repeat three times a meow block. 01:33:34.850 --> 01:33:36.310 But let's play that. 01:33:36.310 --> 01:33:36.810 [MEOW] 01:33:36.810 --> 01:33:38.460 It's no different. 01:33:38.460 --> 01:33:38.960 [MEOW] 01:33:38.960 --> 01:33:40.384 Two. 01:33:40.384 --> 01:33:41.330 [MEOW] 01:33:41.330 --> 01:33:44.240 But I bet we can simplify this one step further and make 01:33:44.240 --> 01:33:45.650 it a little more flexible. 01:33:45.650 --> 01:33:49.520 Let me go ahead and right click or control click on the meow custom block. 01:33:49.520 --> 01:33:53.270 And let me actually add an input here that we'll call n. 01:33:53.270 --> 01:33:55.850 And let me just add a label that says times. 01:33:55.850 --> 01:33:57.510 And let me go ahead and click OK. 01:33:57.510 --> 01:33:59.677 And notice that my puzzle piece now looks different. 01:33:59.677 --> 01:34:02.060 It looks more like some of MIT's blocks that take input 01:34:02.060 --> 01:34:03.390 with these little white ovals. 01:34:03.390 --> 01:34:06.350 And, in fact, now notice what I can do. 01:34:06.350 --> 01:34:11.330 I can change the definition of meow, as Scratch already has for me, 01:34:11.330 --> 01:34:13.560 such that I can now do more inside. 01:34:13.560 --> 01:34:15.930 Let me actually disconnect all of this stuff. 01:34:15.930 --> 01:34:20.040 Let me move the repeat block to the definition of meow itself. 01:34:20.040 --> 01:34:23.450 Let me go ahead and play the sound and wait inside of that repeat block, 01:34:23.450 --> 01:34:26.120 but notice this little circle around the end. 01:34:26.120 --> 01:34:29.270 Let me just repeat an arbitrary number of times now. 01:34:29.270 --> 01:34:32.430 I don't have to worry about hard coding three or 10 or anything else. 01:34:32.430 --> 01:34:36.500 And now, out of sight, out of mind, don't have to worry about that anymore. 01:34:36.500 --> 01:34:40.550 Let's now just whittle down that increasingly complicated program 01:34:40.550 --> 01:34:43.730 that we wrote earlier into, really, just two puzzle pieces. 01:34:43.730 --> 01:34:47.520 When the green flag is clicked, meow, sure, three times. 01:34:47.520 --> 01:34:50.720 I don't have to know or care any more how meow is implemented. 01:34:50.720 --> 01:34:54.590 I just need to know that someone did it for me, whether MIT or maybe 01:34:54.590 --> 01:34:56.360 myself, minutes ago. 01:34:56.360 --> 01:34:57.835 I'll click play again. 01:34:57.835 --> 01:34:59.655 [MEOWING THREE TIMES] 01:34:59.655 --> 01:35:03.040 Two, and three. 01:35:03.040 --> 01:35:05.920 And so now we have an implementation of abstraction. 01:35:05.920 --> 01:35:09.190 Taking a somewhat complicated idea, like getting a cat to meow, 01:35:09.190 --> 01:35:11.680 not worrying about the so-called implementation details, 01:35:11.680 --> 01:35:15.160 and just defining a puzzle piece or function called meow. 01:35:15.160 --> 01:35:17.380 Well, now let's take all of this together 01:35:17.380 --> 01:35:21.910 and see some of the creations of some of your predecessors in past terms. 01:35:21.910 --> 01:35:26.230 Here, for instance, is a sort of story that one of your classmates years ago 01:35:26.230 --> 01:35:28.713 made involving a gingerbread tale. 01:35:28.713 --> 01:35:30.880 Let me go ahead and full screen this and click Play. 01:35:30.880 --> 01:35:31.505 [MUSIC PLAYING] 01:35:31.505 --> 01:35:35.200 And you'll see now that we have multiple sprites already, each of which 01:35:35.200 --> 01:35:37.830 have different costumes, and I'm being asked a question. 01:35:37.830 --> 01:35:38.830 Would you like an apple? 01:35:38.830 --> 01:35:39.400 Yes or no. 01:35:39.400 --> 01:35:40.780 So I'm no longer being asked my name. 01:35:40.780 --> 01:35:42.100 I'm being asked arbitrary questions. 01:35:42.100 --> 01:35:42.470 Sure. 01:35:42.470 --> 01:35:43.887 Let me go ahead and have an apple. 01:35:43.887 --> 01:35:45.460 I type in yes and hit Enter. 01:35:45.460 --> 01:35:46.450 Notice the movement. 01:35:46.450 --> 01:35:48.430 We've seen movement before. 01:35:48.430 --> 01:35:50.170 [CHOMPING SOUNDS] 01:35:50.170 --> 01:35:50.950 [MUSIC PLAYING] 01:35:50.950 --> 01:35:54.100 OK, unfortunately, that was the wrong decision to make in this story. 01:35:54.100 --> 01:35:55.090 So that's OK. 01:35:55.090 --> 01:35:56.290 Let's start it again. 01:35:56.290 --> 01:35:57.280 Red stop sign. 01:35:57.280 --> 01:35:57.970 [MUSIC PLAYING] 01:35:57.970 --> 01:35:58.757 Green flag. 01:35:58.757 --> 01:36:00.340 Hello dearie, would you like an apple? 01:36:00.340 --> 01:36:02.440 No, let's learn from that lesson. 01:36:02.440 --> 01:36:04.270 Cupcake sounds much better. 01:36:04.270 --> 01:36:05.680 I'll type yes this time. 01:36:05.680 --> 01:36:07.270 Notice, again, the motion. 01:36:07.270 --> 01:36:09.370 So there's some animation there. 01:36:09.370 --> 01:36:11.410 It's touching the other sprite. 01:36:11.410 --> 01:36:12.910 That, too, was unfortunate. 01:36:12.910 --> 01:36:16.600 Let's try one last time with this art. 01:36:16.600 --> 01:36:18.470 And now we have an apple, no. 01:36:18.470 --> 01:36:19.210 Learned a lesson. 01:36:19.210 --> 01:36:19.960 Cupcake, no. 01:36:19.960 --> 01:36:21.040 Learned a lesson. 01:36:21.040 --> 01:36:24.288 OK, now let's see what happens with that loop. 01:36:24.288 --> 01:36:25.752 [CACKLING] 01:36:25.752 --> 01:36:29.170 [SCREAMING] 01:36:29.170 --> 01:36:31.270 [CHOMPING SOUNDS] 01:36:31.270 --> 01:36:32.650 OK, surprise ending. 01:36:32.650 --> 01:36:35.680 But this is all to say that by taking these building blocks of loops, 01:36:35.680 --> 01:36:37.420 conditions, and functions, can you start to make things 01:36:37.420 --> 01:36:38.770 that are a little more interactive. 01:36:38.770 --> 01:36:41.380 In fact, I myself did something years ago-- the very first thing 01:36:41.380 --> 01:36:44.213 I myself wrote in Scratch was actually when I was in graduate school 01:36:44.213 --> 01:36:47.630 and cross-registered for a class at MIT, the professor for which 01:36:47.630 --> 01:36:50.830 was the author of and the originator of Scratch itself. 01:36:50.830 --> 01:36:53.470 And let me go ahead and full screen this and propose 01:36:53.470 --> 01:36:58.030 how I thought about solving, now, a fairly large problem back in the day. 01:36:58.030 --> 01:37:00.670 Drag as much falling trash into the can as you can. 01:37:00.670 --> 01:37:01.840 So what's happening now? 01:37:01.840 --> 01:37:03.958 A piece of trash is falling on the screen. 01:37:03.958 --> 01:37:06.250 You'll see that it's moving from the top to the bottom, 01:37:06.250 --> 01:37:08.040 and we've seen animations like that. 01:37:08.040 --> 01:37:08.680 But watch this. 01:37:08.680 --> 01:37:11.800 I bet using a condition and a forever loop, 01:37:11.800 --> 01:37:14.600 we can make it possible to pick this up. 01:37:14.600 --> 01:37:18.010 Notice now the trash is following my cursor, just like the cat was. 01:37:18.010 --> 01:37:21.550 And notice if touching this other trash can sprite, 01:37:21.550 --> 01:37:24.760 maybe we can even get Oscar to pop out of the can. 01:37:24.760 --> 01:37:27.700 And he, then, starts counting up my score, thereby using a variable, 01:37:27.700 --> 01:37:30.400 and indeed, as more sprites or more trash falls, 01:37:30.400 --> 01:37:32.613 I can continue to play a game in this way. 01:37:32.613 --> 01:37:35.530 But here, too, even though things are starting to happen more quickly, 01:37:35.530 --> 01:37:38.280 there's more on the screen, the song is playing in the background, 01:37:38.280 --> 01:37:40.720 it all reduces to basic building blocks. 01:37:40.720 --> 01:37:42.040 And I can't emphasize enough. 01:37:42.040 --> 01:37:44.380 When I wrote that first program years ago, 01:37:44.380 --> 01:37:46.390 I did not implement what you just saw. 01:37:46.390 --> 01:37:49.240 I think the very first thing I did was I googled around 01:37:49.240 --> 01:37:52.515 and found Sesame Street's street lamp and I put that on the screen. 01:37:52.515 --> 01:37:53.890 And that was sort of version one. 01:37:53.890 --> 01:37:56.170 It didn't do anything, but it looked like what I want. 01:37:56.170 --> 01:37:57.880 Then I added the trash can. 01:37:57.880 --> 01:38:02.200 Then I think I programmed one piece of trash or one sprite to fall. 01:38:02.200 --> 01:38:04.570 So I changed the cat to a piece of trash and then 01:38:04.570 --> 01:38:07.010 I had it animate from top to bottom. 01:38:07.010 --> 01:38:10.420 Then version four or five, I then added a forever loop 01:38:10.420 --> 01:38:13.330 and a condition that checks if the mouse button is down, 01:38:13.330 --> 01:38:16.180 and if so, I have it follow the mouse pointer. 01:38:16.180 --> 01:38:19.330 So I took a big problem and broke it down bit by bit 01:38:19.330 --> 01:38:21.140 into much smaller steps. 01:38:21.140 --> 01:38:25.363 And this was the same approach that CS50's own Andrew Berry took years ago, 01:38:25.363 --> 01:38:26.530 one of our teaching fellows. 01:38:26.530 --> 01:38:30.040 The very first year I taught CS50, created his very own first Scratch 01:38:30.040 --> 01:38:33.400 project that I thought I'd leave us with here today. 01:38:33.400 --> 01:38:36.100 This is a program that he called Raining Men. 01:38:36.100 --> 01:38:38.200 It might have a familiar tune, and I would 01:38:38.200 --> 01:38:41.530 propose that you consider, when watching this, 01:38:41.530 --> 01:38:46.780 our final Scratch program today, how it is that Andrew went about programming 01:38:46.780 --> 01:38:48.370 everything that you see. 01:38:48.370 --> 01:38:51.530 Now Andrew went off into the real world and didn't pursue computer science, 01:38:51.530 --> 01:38:52.030 per se. 01:38:52.030 --> 01:38:55.420 He's actually now the general manager for the Cleveland Browns, 01:38:55.420 --> 01:38:57.010 which is an American football team. 01:38:57.010 --> 01:39:00.760 But this, too, speaks to just what kind of foundation you 01:39:00.760 --> 01:39:03.862 can form, irrespective of your intended major, your possible major, 01:39:03.862 --> 01:39:06.820 considering, after all, that a lot of the ideas we're going to focus on 01:39:06.820 --> 01:39:09.160 in this class are ultimately about problem solving, 01:39:09.160 --> 01:39:11.710 programming being just one tool for the trade. 01:39:11.710 --> 01:39:13.870 And, indeed, even within the world of sports, 01:39:13.870 --> 01:39:17.350 are there so many opportunities nowadays for algorithms, for analysis, 01:39:17.350 --> 01:39:21.190 for video simulations thereof, and so many of Andrew's worlds 01:39:21.190 --> 01:39:24.430 and your worlds will invariably start to collide 01:39:24.430 --> 01:39:26.410 as you begin to build up your own toolkit 01:39:26.410 --> 01:39:28.370 and your own understanding thereof. 01:39:28.370 --> 01:39:31.540 So in conclusion, we'll take a look at this, Andrews program. 01:39:31.540 --> 01:39:37.364 In the meantime, this was CS50, and now it's raining men. 01:39:37.364 --> 01:39:40.346 [MUSIC - "IT'S RAINING MEN] 01:40:05.196 --> 01:40:06.187 COMPUTER: Hi. 01:40:06.187 --> 01:40:06.687 Hi. 01:40:06.687 --> 01:40:08.178 We're your weather girls. 01:40:08.178 --> 01:40:09.172 Uh huh. 01:40:09.172 --> 01:40:11.657 And have we got news for you. 01:40:11.657 --> 01:40:12.651 You better listen. 01:40:12.651 --> 01:40:18.118 Get ready, all you lonely girls, and leave those umbrellas at home. 01:40:18.118 --> 01:40:19.112 All right. 01:40:19.112 --> 01:40:24.576 (SINGING) Humidity's rising, barometer's getting low. 01:40:24.576 --> 01:40:25.076 Oh. 01:40:25.076 --> 01:40:26.070 Uh oh. 01:40:26.070 --> 01:40:28.280 According to all sources-- 01:40:28.280 --> 01:40:29.750 What sources now? 01:40:29.750 --> 01:40:32.690 The street's the place to go. 01:40:32.690 --> 01:40:40.040 'Cause tonight for the first time, at just about half past 10, 01:40:40.040 --> 01:40:47.880 for the first time in history it's gonna start raining men! 01:40:47.880 --> 01:40:54.340 It's raining men, hallelujah, it's raining men-- 01:40:54.340 --> 01:40:58.890 [MUSIC PLAYING]