WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:03.486 [MUSIC PLAYING] 00:01:30.636 --> 00:01:34.620 [AUDIENCE EXCLAIMING] 00:01:34.620 --> 00:01:38.106 [APPLAUSE] 00:01:42.588 --> 00:01:44.170 SPOT: Hello, world. 00:01:44.170 --> 00:01:47.860 This is CS50, Harvard University's introduction 00:01:47.860 --> 00:01:50.740 to the intellectual enterprises of computer science 00:01:50.740 --> 00:01:53.080 and the art of programming. 00:01:53.080 --> 00:01:54.190 Woof, woof. 00:01:54.190 --> 00:01:57.613 [LAUGHTER] 00:02:08.870 --> 00:02:10.857 I'm sorry, Dave. 00:02:10.857 --> 00:02:12.440 [PINK, "WHAT ABOUT US"] What about us? 00:02:15.350 --> 00:02:16.310 What about us? 00:02:16.310 --> 00:02:17.534 La-da-da-da-da-da-da. 00:02:17.534 --> 00:02:18.965 La-da-da-da-da-da-da. 00:02:18.965 --> 00:02:21.827 We are searchlights. 00:02:21.827 --> 00:02:23.750 We can see in the dark. 00:02:27.020 --> 00:02:31.280 We are rockets pointed up at the stars. 00:02:34.710 --> 00:02:38.610 We are billions of beautiful hearts. 00:02:41.977 --> 00:02:46.750 And you sold us down the river too far. 00:02:49.570 --> 00:02:51.920 What about us? 00:02:51.920 --> 00:02:57.270 What about all the times you said you had the answers? 00:02:57.270 --> 00:02:59.270 What about us? 00:02:59.270 --> 00:03:05.270 What about all the broken happy ever-afters? 00:03:05.270 --> 00:03:06.770 What about us? 00:03:06.770 --> 00:03:12.770 What about all the plans that ended in disaster? 00:03:12.770 --> 00:03:14.980 What about love? 00:03:14.980 --> 00:03:16.149 What about trust? 00:03:16.149 --> 00:03:18.145 What about us? 00:03:21.139 --> 00:03:24.632 La-da-da-da-da-da-da. 00:03:24.632 --> 00:03:28.624 La-da-da-da-da-da-da. 00:03:28.624 --> 00:03:30.121 La-da-da-da-da-da-da. 00:03:33.115 --> 00:03:34.612 La-da-da-da-da-da-da. 00:03:55.107 --> 00:03:56.190 DAVID J. MALAN: All right. 00:03:56.190 --> 00:03:56.840 So-- 00:03:56.840 --> 00:04:00.340 [APPLAUSE, CHEERING] 00:04:02.660 --> 00:04:05.480 This is CS50, Harvard University's introduction 00:04:05.480 --> 00:04:07.790 to the intellectual enterprises of computer science 00:04:07.790 --> 00:04:09.200 and the art of programming. 00:04:09.200 --> 00:04:11.090 And this is Spot. 00:04:11.090 --> 00:04:13.370 And our thanks to our friends at 3D Cowboys 00:04:13.370 --> 00:04:15.080 for bringing him by class today. 00:04:15.080 --> 00:04:18.524 Perhaps a round of applause for our special Professor [INAUDIBLE].. 00:04:18.524 --> 00:04:21.860 [APPLAUSE] 00:04:21.860 --> 00:04:22.820 My name is David Malan. 00:04:22.820 --> 00:04:26.270 And I actually took this class, CS50, myself, some years ago. 00:04:26.270 --> 00:04:27.500 But I almost didn't. 00:04:27.500 --> 00:04:30.590 And I didn't because when I got to campus, as a first-year, 00:04:30.590 --> 00:04:33.350 I really gravitated toward things already familiar, things 00:04:33.350 --> 00:04:35.450 with which I was already comfortable, specifically, government. 00:04:35.450 --> 00:04:38.700 And I came in here thinking I was going to major or concentrate in government. 00:04:38.700 --> 00:04:41.840 And it was only once I got to sophomore year, fall semester, 00:04:41.840 --> 00:04:44.300 that some friends of mine were coming to this class called 00:04:44.300 --> 00:04:48.290 CS50 that was very much to beware at the time, in that it 00:04:48.290 --> 00:04:50.870 was an unfamiliar field to so many of us, myself included. 00:04:50.870 --> 00:04:54.320 But I got up the nerve to go over the threshold, sit in on the first class, 00:04:54.320 --> 00:04:56.280 just shop it, so to speak. 00:04:56.280 --> 00:04:57.570 And I was hooked. 00:04:57.570 --> 00:05:00.470 I found that homework was, for the first time, fun. 00:05:00.470 --> 00:05:03.783 And this was after having enrolled only because the professor let 00:05:03.783 --> 00:05:06.200 me enroll pass/fail or [INAUDIBLE] because I didn't really 00:05:06.200 --> 00:05:07.283 think I could even cut it. 00:05:07.283 --> 00:05:10.020 But fast forward to five Mondays later in the semester, 00:05:10.020 --> 00:05:12.020 I actually switched to a letter grade, deciding, 00:05:12.020 --> 00:05:14.240 wow, this is actually something for me. 00:05:14.240 --> 00:05:17.480 And I say this because computer science, what 00:05:17.480 --> 00:05:19.640 I didn't realize about it at the time, really 00:05:19.640 --> 00:05:23.680 is this very general purpose way of thinking and way of solving problems. 00:05:23.680 --> 00:05:26.490 And even if this is the only CS class you ever take, 00:05:26.490 --> 00:05:29.940 even if this is the only formal training you have in programming 00:05:29.940 --> 00:05:32.550 as a practical skill, it's just so darn applicable 00:05:32.550 --> 00:05:35.670 to so many other fields-- the arts, humanities, social sciences, 00:05:35.670 --> 00:05:39.660 natural sciences, and beyond, and certainly within the STEM fields, 00:05:39.660 --> 00:05:40.530 themselves. 00:05:40.530 --> 00:05:43.810 That said, it's going to often feel a little something like this. 00:05:43.810 --> 00:05:47.310 This is from our friends at MIT, down the road, which is one of their hacks 00:05:47.310 --> 00:05:51.300 whereby they connected a fire hydrant to a drinking fountain, 00:05:51.300 --> 00:05:54.960 with a little sign up there that says, "GETTING AN EDUCATION FROM MIT IS LIKE 00:05:54.960 --> 00:05:57.480 DRINKING FROM A FIRE HOSE," which is to say that it's 00:05:57.480 --> 00:05:59.310 going to feel, quite often, in this class, 00:05:59.310 --> 00:06:01.260 too, that there's just a lot of information. 00:06:01.260 --> 00:06:03.960 And you can't possibly absorb it all, but realize 00:06:03.960 --> 00:06:06.750 that's to be expected, especially in an introductory class. 00:06:06.750 --> 00:06:11.520 The whole point is for so much of it to feel new, but with practice, with time, 00:06:11.520 --> 00:06:14.390 with years, even, looking back, will you get all the more 00:06:14.390 --> 00:06:15.640 comfortable with the material. 00:06:15.640 --> 00:06:18.340 But you're not meant to feel comfortable along the way. 00:06:18.340 --> 00:06:20.340 And so let me encourage you, invite you, expect 00:06:20.340 --> 00:06:23.580 you to get comfortable feeling uncomfortable along the way, 00:06:23.580 --> 00:06:27.390 whether you have or have not prior computer science or programming 00:06:27.390 --> 00:06:28.080 experience. 00:06:28.080 --> 00:06:32.130 Now, back in my day, the class' syllabus was a little bit different. 00:06:32.130 --> 00:06:35.438 And, really, when I and all of my classmates exited the class, 00:06:35.438 --> 00:06:37.230 we would say to friends that we learned how 00:06:37.230 --> 00:06:40.860 to program in C, which is a language that we, ourselves, will still 00:06:40.860 --> 00:06:41.860 learn in this class. 00:06:41.860 --> 00:06:44.040 But that was it for languages. 00:06:44.040 --> 00:06:46.950 But nowadays, as you'll see, we'll explore not just 00:06:46.950 --> 00:06:49.560 this older language called C, but a more modern language 00:06:49.560 --> 00:06:53.580 called Python that's very much in vogue for data science, and web applications, 00:06:53.580 --> 00:06:54.460 and much more. 00:06:54.460 --> 00:06:56.293 But we'll also introduce you, along the way, 00:06:56.293 --> 00:07:01.020 to another language called S-Q-L or SQL, which is specifically for databases. 00:07:01.020 --> 00:07:03.330 And SQL is a different type of programming language 00:07:03.330 --> 00:07:06.060 that's just going to give you different ways of solving problems, 00:07:06.060 --> 00:07:09.480 different building blocks with which to express problems you want to solve. 00:07:09.480 --> 00:07:11.670 We'll introduce you, toward the end of the semester, 00:07:11.670 --> 00:07:14.040 yet another language, JavaScript, often used 00:07:14.040 --> 00:07:17.415 with markup languages called HTML and CSS, with which maybe you 00:07:17.415 --> 00:07:19.290 have some experience if you've made websites. 00:07:19.290 --> 00:07:23.640 But we'll do this because so many of today's actual real-world software 00:07:23.640 --> 00:07:24.630 is web-based. 00:07:24.630 --> 00:07:27.450 Or it's phone-based, mobile-based, but even then, 00:07:27.450 --> 00:07:30.000 it's using these same languages, like JavaScript. 00:07:30.000 --> 00:07:32.700 And so by the end of CS50, in particular, 00:07:32.700 --> 00:07:35.820 you won't know how to program in X, or Y, or Z, per se. 00:07:35.820 --> 00:07:39.150 But, period, you'll learn how to program. 00:07:39.150 --> 00:07:41.190 And, indeed, among the goals of this class 00:07:41.190 --> 00:07:45.540 is to equip you with enough of a set of concepts, enough practical skills 00:07:45.540 --> 00:07:49.440 and experience that after CS50, again, if you never take another CS class, 00:07:49.440 --> 00:07:51.690 you can teach yourself new languages. 00:07:51.690 --> 00:07:52.650 And you won't feel-- 00:07:52.650 --> 00:07:55.440 you won't have been reliant on a class to, indeed, 00:07:55.440 --> 00:07:57.508 introduce you to each and one of those ideas. 00:07:57.508 --> 00:07:59.550 And what are we going to do, then, in this class? 00:07:59.550 --> 00:08:01.260 And, really, what are we going to start doing today? 00:08:01.260 --> 00:08:03.402 Well, we're going to learn how to solve problems. 00:08:03.402 --> 00:08:05.610 And that's really what computer science is all about. 00:08:05.610 --> 00:08:09.090 We'll, very specifically, today, knock off a few to-dos. 00:08:09.090 --> 00:08:12.130 We'll learn how to represent simple things, like numbers. 00:08:12.130 --> 00:08:14.880 We'll learn how to represent letters of an alphabet, like English. 00:08:14.880 --> 00:08:17.130 We'll learn how to represent colors-- red, green, blue, 00:08:17.130 --> 00:08:18.240 and everything in between. 00:08:18.240 --> 00:08:20.610 We'll learn how to represent, more interestingly, 00:08:20.610 --> 00:08:23.430 full-fledged images that you might see on the web or your phone. 00:08:23.430 --> 00:08:26.730 We'll talk about representing videos and even audio files. 00:08:26.730 --> 00:08:29.540 So, by the end of today, alone, you will exit here 00:08:29.540 --> 00:08:31.850 with a newfound mental model for how you can 00:08:31.850 --> 00:08:36.140 represent all of today's media and multimedia that we take for granted 00:08:36.140 --> 00:08:37.760 and use most every day. 00:08:37.760 --> 00:08:40.370 But we'll also focus today, ultimately, on how 00:08:40.370 --> 00:08:43.520 to write algorithms, like step-by-step instructions for solving 00:08:43.520 --> 00:08:47.375 some problem, specifically, implementing algorithms with code. 00:08:47.375 --> 00:08:48.500 And that's what we'll find. 00:08:48.500 --> 00:08:50.417 An algorithm is just something you can express 00:08:50.417 --> 00:08:54.350 in English or any human language, but code is a translation of that 00:08:54.350 --> 00:08:57.260 to, presumably, the 0's and 1's that you've probably heard 00:08:57.260 --> 00:08:59.840 is all that computers ultimately speak. 00:08:59.840 --> 00:09:02.030 And if you're feeling like that's a lot for today, 00:09:02.030 --> 00:09:04.488 if you're feeling like that's a lot for the whole semester, 00:09:04.488 --> 00:09:07.190 realize and take comfort in knowing that -thirds of you 00:09:07.190 --> 00:09:09.720 have never taken a CS course before. 00:09:09.720 --> 00:09:12.140 So even if you think, like I did all those years ago, 00:09:12.140 --> 00:09:15.047 that, surely, my friends, the kids I didn't even in the class, 00:09:15.047 --> 00:09:16.880 must know more than I, have been programming 00:09:16.880 --> 00:09:18.500 since they were six years old. 00:09:18.500 --> 00:09:19.880 That's just not the case. 00:09:19.880 --> 00:09:21.260 You're in very much good company. 00:09:21.260 --> 00:09:24.003 And within the class will you find different tracks, 00:09:24.003 --> 00:09:26.420 by way of the homework assignments, called "problem sets," 00:09:26.420 --> 00:09:28.003 by way of the sections or recitations. 00:09:28.003 --> 00:09:29.962 There will be different tracks for those of you 00:09:29.962 --> 00:09:32.610 less comfortable, more comfortable, somewhere in between. 00:09:32.610 --> 00:09:34.940 And if you really don't know why you are here today, 00:09:34.940 --> 00:09:37.970 we'll even have sections for those least comfortable, where you just 00:09:37.970 --> 00:09:41.690 don't necessarily feel ready to dive into CS or maybe STEM, more generally. 00:09:41.690 --> 00:09:44.840 But we'll get you there by way of the course's support structure. 00:09:44.840 --> 00:09:47.780 And it's very much grounded in this mindset. 00:09:47.780 --> 00:09:50.150 What ultimately matters in this class is not so much 00:09:50.150 --> 00:09:52.025 where you end up relative to your classmates, 00:09:52.025 --> 00:09:55.040 but where you end up relative to yourself, when you began. 00:09:55.040 --> 00:09:58.323 So whether you have or have not prior programming or CSS experience, 00:09:58.323 --> 00:10:00.740 it's not going to matter when it comes to evaluation, when 00:10:00.740 --> 00:10:04.320 it comes to the output, be it a grade, or a satisfactory mark, or the like. 00:10:04.320 --> 00:10:06.860 It's going to depend on, really, where you are today, 00:10:06.860 --> 00:10:11.960 in this, what we call week zero, versus the very end of the semester, when 00:10:11.960 --> 00:10:15.050 you will have built something grand, of your very own, in software. 00:10:15.050 --> 00:10:17.270 But CS50 is also characterized, fairly uniquely, 00:10:17.270 --> 00:10:19.147 by its community, its culture. 00:10:19.147 --> 00:10:21.230 And along the way, you'll see that your experience 00:10:21.230 --> 00:10:24.680 is punctuated by a lot of social and academic events alike. 00:10:24.680 --> 00:10:27.890 CS50 lunches, most every Friday, we'll gather at a nearby restaurant 00:10:27.890 --> 00:10:30.280 called Changsho where we'll have Chinese lunch together. 00:10:30.280 --> 00:10:32.030 And as many of you as might want to attend 00:10:32.030 --> 00:10:34.988 that week will be able to join us, sit down, and chat casually with me, 00:10:34.988 --> 00:10:39.680 the course's teaching staff, friends of ours, alumni from industry, as well. 00:10:39.680 --> 00:10:42.630 CS50 Puzzle Day, coming up this weekend, will be an opportunity, 00:10:42.630 --> 00:10:46.740 even if you have no prior CS experience, just to solve problems, 00:10:46.740 --> 00:10:49.790 not jigsaw puzzles, but puzzles in the logical sense. 00:10:49.790 --> 00:10:52.490 We'll hand you a packet of puzzles-- 00:10:52.490 --> 00:10:54.230 logic problems, or riddles, or the like-- 00:10:54.230 --> 00:10:56.087 that you, as teams, can solve together. 00:10:56.087 --> 00:10:57.920 And at the very end, we'll walk you through. 00:10:57.920 --> 00:11:01.350 And along the way, there'll be not only these puzzles, but pizza, and prizes, 00:11:01.350 --> 00:11:02.960 and so much more, as well. 00:11:02.960 --> 00:11:06.350 Towards the end of the semester, we'll have a CS50 Hackathon whereby 00:11:06.350 --> 00:11:09.830 we'll get together around 7:00 PM, at the start of reading period, 00:11:09.830 --> 00:11:12.740 and we'll finish up around 7:00 AM the next morning. 00:11:12.740 --> 00:11:15.080 And it will be this opportunity to really bond 00:11:15.080 --> 00:11:18.140 with your classmates, your project partners, if you work in a team, 00:11:18.140 --> 00:11:20.510 on your very own final project, which is meant 00:11:20.510 --> 00:11:23.750 to be a capstone of the course, something you build, yourselves, 00:11:23.750 --> 00:11:25.760 that we don't hand you a specification for. 00:11:25.760 --> 00:11:29.090 But it's your final offboarding, so that when you exit CS50, 00:11:29.090 --> 00:11:31.340 you don't need CS50 anymore. 00:11:31.340 --> 00:11:32.100 You don't need me. 00:11:32.100 --> 00:11:33.058 You don't need your TF. 00:11:33.058 --> 00:11:35.910 You can actually write code and solve problems on your own. 00:11:35.910 --> 00:11:39.740 So this picture here is one of our past photos from earlier in the evening. 00:11:39.740 --> 00:11:42.590 Things get a little quieter as we then, around 5:00 AM, 00:11:42.590 --> 00:11:46.340 drive anyone who's still awake and energetic to a nearby IHOP 00:11:46.340 --> 00:11:48.140 for pancakes, around then. 00:11:48.140 --> 00:11:51.710 But here is how most of the evenings tend to end for at least some 00:11:51.710 --> 00:11:53.000 of your classmates prior. 00:11:53.000 --> 00:11:55.670 But at the very end of the class is the so-called CS50 Fair, 00:11:55.670 --> 00:11:59.670 an exhibition of all of your final projects for friends, faculty, 00:11:59.670 --> 00:12:01.610 students, and staff across campus, whereby 00:12:01.610 --> 00:12:03.440 you'll be invited to a space like this. 00:12:03.440 --> 00:12:04.220 Bring your laptop. 00:12:04.220 --> 00:12:06.920 Bring your phone, whatever it is you have built and created. 00:12:06.920 --> 00:12:09.260 And we'll just show it off for a bit of the afternoon, 00:12:09.260 --> 00:12:11.870 complete with music, and friends from industry, 00:12:11.870 --> 00:12:16.000 and candy, and all what makes gathering together at term's end fond. 00:12:16.000 --> 00:12:17.750 And you'll wear, ultimately, very proudly, 00:12:17.750 --> 00:12:21.770 we hope, your very own "I took CS50," stating, very matter of factly, 00:12:21.770 --> 00:12:25.790 what I did some years ago; that, indeed, this was a course I, myself, took. 00:12:25.790 --> 00:12:29.120 So, today, let's focus, then, on computer science, like what is it. 00:12:29.120 --> 00:12:31.790 Well, it's really the study of information. 00:12:31.790 --> 00:12:34.910 But, really, it's, more specifically, about solving problems, 00:12:34.910 --> 00:12:38.360 using certain ideas and techniques, all of which you'll exit the course with. 00:12:38.360 --> 00:12:43.378 So, as such, problem solving is a goal that we'll 00:12:43.378 --> 00:12:45.920 approach by way of something called "computational thinking." 00:12:45.920 --> 00:12:49.760 So computational thinking, you can oversimplistically think about it 00:12:49.760 --> 00:12:51.500 as thinking like a computer. 00:12:51.500 --> 00:12:55.410 But it's really the application of ideas that we'll dive into today 00:12:55.410 --> 00:12:57.380 and we'll finish some weeks from now, that you 00:12:57.380 --> 00:13:01.880 can apply to problems from this field or most any other, but in a computational, 00:13:01.880 --> 00:13:04.770 that is, a very methodical, very careful way. 00:13:04.770 --> 00:13:07.520 And that's what CS really did for me and does for a lot of people. 00:13:07.520 --> 00:13:09.740 It helps clean up your thought processes. 00:13:09.740 --> 00:13:12.560 Even if you go off into the real world and do nothing in tech, 00:13:12.560 --> 00:13:14.840 you have an ability, after classes like this, 00:13:14.840 --> 00:13:18.020 to express yourself a little more correctly, more 00:13:18.020 --> 00:13:20.660 precisely and, generally, having better command 00:13:20.660 --> 00:13:22.980 of your own ideas and your language. 00:13:22.980 --> 00:13:24.410 So what's problem solving? 00:13:24.410 --> 00:13:25.790 Let me propose that this is it. 00:13:25.790 --> 00:13:29.270 This is as simple as we can make today's goals and the semester's goals. 00:13:29.270 --> 00:13:30.960 Problems to be solved look like this. 00:13:30.960 --> 00:13:32.960 You've got some input, the problem to be solved. 00:13:32.960 --> 00:13:35.750 You've got a goal being the output, like the solution there, too. 00:13:35.750 --> 00:13:38.000 And then somewhere in the middle is the secret sauce, 00:13:38.000 --> 00:13:40.375 where we'll spend the next several weeks figuring out how 00:13:40.375 --> 00:13:42.350 we can convert these inputs to outputs. 00:13:42.350 --> 00:13:44.870 But before we can do that, we all just have 00:13:44.870 --> 00:13:48.260 to agree on how to represent these inputs and outputs, 00:13:48.260 --> 00:13:51.260 whether it's with English or, really, any type of language. 00:13:51.260 --> 00:13:54.290 But, as I spoiled earlier, you probably came in here already 00:13:54.290 --> 00:13:56.900 with a general sense that, yeah, computers somehow 00:13:56.900 --> 00:14:00.823 only speak or know 0's and 1's, the so-called binary system. 00:14:00.823 --> 00:14:02.990 But that's just one way of representing information. 00:14:02.990 --> 00:14:05.150 Even simpler than binary is unary. 00:14:05.150 --> 00:14:10.670 So if you've ever, at this age or any prior age, counted on your fingers, 00:14:10.670 --> 00:14:14.480 this is unary notation, whereby each of your digits, your fingers 00:14:14.480 --> 00:14:17.000 literally represent some piece of information; 00:14:17.000 --> 00:14:19.760 taking attendance, like 1, 2, 3, 4, 5. 00:14:19.760 --> 00:14:25.800 But on your one human hand, how high can you count in this unary notation? 00:14:25.800 --> 00:14:26.505 AUDIENCE: Five. 00:14:26.505 --> 00:14:27.130 AUDIENCE: Five. 00:14:27.130 --> 00:14:29.550 DAVID J. MALAN: Five, I'm hearing five. 00:14:29.550 --> 00:14:31.890 Six, I heard one six. 00:14:31.890 --> 00:14:35.610 But I'm going to go further and say the answer, if you're clever about it, 00:14:35.610 --> 00:14:36.500 is actually-- 00:14:36.500 --> 00:14:37.132 AUDIENCE: 40? 00:14:37.132 --> 00:14:38.340 DAVID J. MALAN: Not quite 40. 00:14:38.340 --> 00:14:39.660 You overbid, but-- 00:14:39.660 --> 00:14:40.500 AUDIENCE: 31. 00:14:40.500 --> 00:14:43.885 DAVID J. MALAN: 31 is as high as I can actually count. 00:14:43.885 --> 00:14:46.260 And that's because if I actually-- and if you're thinking 00:14:46.260 --> 00:14:49.530 this is weirdly painful now, it will be, but this 00:14:49.530 --> 00:14:51.600 is my hand representing the number 0. 00:14:51.600 --> 00:14:55.980 Normally, in unary, this is 1, 2, 3, 4, 5, of course, obviously. 00:14:55.980 --> 00:14:58.290 But what if I take into account the order in which I'm 00:14:58.290 --> 00:14:59.590 putting my fingers up and down? 00:14:59.590 --> 00:15:00.840 So maybe this is still 0. 00:15:00.840 --> 00:15:02.490 Maybe this is still 1. 00:15:02.490 --> 00:15:07.410 But maybe this is now 2, where it's just the single second finger up, 00:15:07.410 --> 00:15:08.940 not two of them, total. 00:15:08.940 --> 00:15:10.860 Maybe this is now 3. 00:15:10.860 --> 00:15:14.730 Maybe this is now-- often offensive, with just the middle finger up. 00:15:14.730 --> 00:15:17.250 This is now [LAUGHS] 5. 00:15:17.250 --> 00:15:19.260 This is now 6. 00:15:19.260 --> 00:15:21.160 This is now 7. 00:15:21.160 --> 00:15:24.010 And my hand just hurts too much if I try to count higher than seven. 00:15:24.010 --> 00:15:27.640 But, theoretically, because each of my fingers can be down or up 00:15:27.640 --> 00:15:32.230 and I've got five of them, that's actually 32 possible permutations, 00:15:32.230 --> 00:15:32.890 up and down. 00:15:32.890 --> 00:15:33.640 But wait a minute. 00:15:33.640 --> 00:15:36.130 We said, 31, but if you start at 0. 00:15:36.130 --> 00:15:38.735 You have to subtract 1 from the biggest possible value. 00:15:38.735 --> 00:15:41.860 So this is to say you and I have been using unary because it's just simple, 00:15:41.860 --> 00:15:43.100 and it gets the job done. 00:15:43.100 --> 00:15:46.750 But if we just think about representation a little more cleverly, 00:15:46.750 --> 00:15:50.410 we can do exactly what computers do, using not what mathematicians call 00:15:50.410 --> 00:15:54.460 "base-1," where the finger is either there or it's not, but base-2. 00:15:54.460 --> 00:15:58.390 And in base-2, we just need two digits at our disposal. 00:15:58.390 --> 00:16:02.720 And we could call these digits 1 and 2, A and B, black or white. 00:16:02.720 --> 00:16:06.370 We just need two words to describe two pieces of information. 00:16:06.370 --> 00:16:07.600 Computers keep it simple. 00:16:07.600 --> 00:16:10.960 And just like we humans start counting 0, 1, 2, 3 on up, 00:16:10.960 --> 00:16:14.230 computers use 0 and 1, and that's it. 00:16:14.230 --> 00:16:15.710 But that's by convention. 00:16:15.710 --> 00:16:16.940 But why do they do that? 00:16:16.940 --> 00:16:21.930 Well, it turns out, when you use base-2, otherwise known as binary, well, 00:16:21.930 --> 00:16:24.030 it just maps really readily to the real world. 00:16:24.030 --> 00:16:25.947 Because, at the end of the day, what do we all 00:16:25.947 --> 00:16:28.740 do, if you've got a laptop, or a phone, or any device? 00:16:28.740 --> 00:16:32.310 You plug it into the wall because it needs electricity at some point. 00:16:32.310 --> 00:16:35.820 And what if you have electricity or not? 00:16:35.820 --> 00:16:38.190 Well, there's your two possible values. 00:16:38.190 --> 00:16:39.910 Either it's there, or it's not. 00:16:39.910 --> 00:16:42.000 And because computers are electrical devices, 00:16:42.000 --> 00:16:43.675 this is why binary is just useful. 00:16:43.675 --> 00:16:44.550 It's nice and simple. 00:16:44.550 --> 00:16:46.920 Either electricity is there, or it's not. 00:16:46.920 --> 00:16:48.810 So when you plug this device in and you've 00:16:48.810 --> 00:16:51.060 got all these electrons or whatever flowing, maybe 00:16:51.060 --> 00:16:53.250 if we just hang on to some of that electricity, 00:16:53.250 --> 00:16:55.230 we can represent what we'll call a 1. 00:16:55.230 --> 00:16:58.470 And maybe if we let it dissipate or go away, that's a 0. 00:16:58.470 --> 00:17:02.760 So on and off maps very readily to this idea of just 0's and 1's. 00:17:02.760 --> 00:17:06.750 And if you've ever thought of this, now, as binary digits-- 00:17:06.750 --> 00:17:10.380 "bi" implying 2, 0 and 1-- well, if you've ever heard this term now, 00:17:10.380 --> 00:17:12.690 "bit," it just means binary digit. 00:17:12.690 --> 00:17:14.688 A single bit is just a 0 or 1. 00:17:14.688 --> 00:17:16.980 But we could have called these things anything we want. 00:17:16.980 --> 00:17:20.069 Now how does this map to be clear to the real world? 00:17:20.069 --> 00:17:23.470 Well, we can't see the tiny little switches inside of our Macs, 00:17:23.470 --> 00:17:26.140 PCs, and our phones that are actually turning the electricity on 00:17:26.140 --> 00:17:28.480 or off, storing electricity or not. 00:17:28.480 --> 00:17:29.920 But they're called transistors. 00:17:29.920 --> 00:17:32.470 They've got millions of them in today's hardware. 00:17:32.470 --> 00:17:35.317 And they're just on or off, like a switch or a light bulb. 00:17:35.317 --> 00:17:37.150 So, for instance, if there's no electricity, 00:17:37.150 --> 00:17:40.690 the switch is off, we would call this, by convention, a 0. 00:17:40.690 --> 00:17:45.160 If, though, you throw the switch and it actually turns on, we would call this-- 00:17:45.160 --> 00:17:45.940 AUDIENCE: On. 00:17:45.940 --> 00:17:47.607 DAVID J. MALAN: --an "on," exactly, a 1. 00:17:47.607 --> 00:17:48.690 We could have reversed it. 00:17:48.690 --> 00:17:51.160 But this is just the way the world decided to standardize. 00:17:51.160 --> 00:17:51.910 And that's it. 00:17:51.910 --> 00:17:55.780 So you've either got something on or off, a 1 or a 0. 00:17:55.780 --> 00:18:00.020 And this, then, is this thing we know now as a binary digit or a bit. 00:18:00.020 --> 00:18:04.490 So once we've got these values, what about how-- 00:18:04.490 --> 00:18:07.090 how can we go about, perhaps, representing things? 00:18:07.090 --> 00:18:08.060 Well, you know what? 00:18:08.060 --> 00:18:10.352 It turns out we've got a lot of light bulbs right here. 00:18:10.352 --> 00:18:11.260 Let me grab-- thanks. 00:18:11.260 --> 00:18:12.010 Excuse me, spot. 00:18:12.010 --> 00:18:16.540 Let me grab the little music stand here. 00:18:16.540 --> 00:18:18.610 Let me borrow a couple of these bulbs and see 00:18:18.610 --> 00:18:22.310 if we can't make clearer than my hand, alone, what's going on here. 00:18:22.310 --> 00:18:24.970 So I'm going to go ahead and grab two of these. 00:18:24.970 --> 00:18:26.670 And I'll just put them here. 00:18:26.670 --> 00:18:28.420 And I can turn these things on or off now. 00:18:28.420 --> 00:18:32.560 So if I've got two bits, two switches, two transistors, if you will, well, 00:18:32.560 --> 00:18:38.660 if I go ahead and turn on this one, I'm representing what number in binary, 00:18:38.660 --> 00:18:39.160 perhaps? 00:18:39.160 --> 00:18:39.880 AUDIENCE: 1. 00:18:39.880 --> 00:18:41.200 DAVID J. MALAN: So just 1. 00:18:41.200 --> 00:18:45.130 Now, if I'm using unary, I would turn this one on and be done with it. 00:18:45.130 --> 00:18:46.980 And that's 2, but not in binary. 00:18:46.980 --> 00:18:51.350 Binary, it's the permutations, which ones are on and off, that matters. 00:18:51.350 --> 00:18:53.790 So what, now, am I representing here, perhaps? 00:18:53.790 --> 00:18:54.370 AUDIENCE: 2. 00:18:54.370 --> 00:18:55.120 DAVID J. MALAN: 2. 00:18:55.120 --> 00:18:58.210 So this is when I put my single pointer finger up. 00:18:58.210 --> 00:19:00.430 But then when I did this, in my human hand, 00:19:00.430 --> 00:19:03.310 this was like representing the number 3. 00:19:03.310 --> 00:19:06.640 How do I represent the number 4. 00:19:06.640 --> 00:19:11.000 Yeah, I need another light bulb, so I need more hardware, so to speak. 00:19:11.000 --> 00:19:13.000 So if I turn-- 00:19:13.000 --> 00:19:19.180 if I leave this one-- if I turn this one on, this one off, this one off, 00:19:19.180 --> 00:19:21.380 now I have the number 4. 00:19:21.380 --> 00:19:24.593 And someone tell me, saying the words "on" 00:19:24.593 --> 00:19:26.510 and "on" and "on," or "on," or "off," or "on," 00:19:26.510 --> 00:19:28.850 using combinations of "on," "off" and-- "on" 00:19:28.850 --> 00:19:32.832 and "off," how do I represent 5, from your left to your right? 00:19:32.832 --> 00:19:33.665 How about over here? 00:19:33.665 --> 00:19:34.880 AUDIENCE: On, off, on. 00:19:34.880 --> 00:19:36.620 DAVID J. MALAN: "On, off, on," I heard. 00:19:36.620 --> 00:19:38.480 And that's exactly right. 00:19:38.480 --> 00:19:41.085 And how do I represent, maybe, 6? 00:19:41.085 --> 00:19:41.585 Over here? 00:19:41.585 --> 00:19:43.010 AUDIENCE: Off, on, on. 00:19:43.010 --> 00:19:45.080 DAVID J. MALAN: Off, on, on, not quite. 00:19:45.080 --> 00:19:45.995 From left to right? 00:19:45.995 --> 00:19:47.390 AUDIENCE: Off-- the other way. 00:19:47.390 --> 00:19:47.620 DAVID J. MALAN: The-- 00:19:47.620 --> 00:19:49.290 [LAUGHS] OK, so from right to left. 00:19:49.290 --> 00:19:50.960 So I think we leave this one on. 00:19:50.960 --> 00:19:58.760 This one, I'm going to claim, represents, now, 6 and 7. 00:19:58.760 --> 00:19:59.570 AUDIENCE: On, off. 00:19:59.570 --> 00:20:02.300 DAVID J. MALAN: I'm just going to-- it's actually going to be "on, on, on." 00:20:02.300 --> 00:20:04.737 Now, if you're wondering, where are these people coming up 00:20:04.737 --> 00:20:07.070 with these combinations, there's actually a system here. 00:20:07.070 --> 00:20:09.770 It's actually hard for me to do it backwards. 00:20:09.770 --> 00:20:14.202 But it turns out there's actually a system that's not all that unfamiliar. 00:20:14.202 --> 00:20:15.410 In fact, let me propose this. 00:20:15.410 --> 00:20:18.770 Let me propose that we consider what you and I all 00:20:18.770 --> 00:20:23.100 learned in grade school, which was something like the base-10 system, 10 00:20:23.100 --> 00:20:25.770 meaning that you use 10 different digits, not two, 10. 00:20:25.770 --> 00:20:30.300 So 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, that's the base-10 system, otherwise known 00:20:30.300 --> 00:20:32.370 as decimal, "dec" implying 10. 00:20:32.370 --> 00:20:34.140 So that's what you and I use every day. 00:20:34.140 --> 00:20:37.140 Well, let's think about how we represent numbers in decimal, 00:20:37.140 --> 00:20:38.430 in the normal human way. 00:20:38.430 --> 00:20:39.520 Well, here is a number. 00:20:39.520 --> 00:20:41.380 It's what number, of course? 00:20:41.380 --> 00:20:42.180 AUDIENCE: 123. 00:20:42.180 --> 00:20:43.542 DAVID J. MALAN: 123. 00:20:43.542 --> 00:20:45.750 So we all just have an intuition for that, obviously. 00:20:45.750 --> 00:20:48.420 But it's not necessarily 123. 00:20:48.420 --> 00:20:51.640 You're just assigning meaning to the positions of these digits. 00:20:51.640 --> 00:20:54.030 This is really the pattern 1-2-3. 00:20:54.030 --> 00:20:57.390 But you immediately jump, mathematically, to 123, but why? 00:20:57.390 --> 00:21:00.390 Well, odds are, in grade school, you learned that the rightmost digit is 00:21:00.390 --> 00:21:02.160 the ones place or the ones column. 00:21:02.160 --> 00:21:04.800 This is the tens place or the tens column. 00:21:04.800 --> 00:21:06.490 This is the hundreds place. 00:21:06.490 --> 00:21:08.230 And so why is this relevant? 00:21:08.230 --> 00:21:11.190 Well, this is like doing 100 times 1 plus 10 times 00:21:11.190 --> 00:21:16.410 2 plus 1 times 3 or, if we multiply that out, 100 plus 20 plus 3, 00:21:16.410 --> 00:21:21.240 ergo, the number, now, that we just take for granted is 123. 00:21:21.240 --> 00:21:24.240 But that's in base-10, the so-called decimal system, 00:21:24.240 --> 00:21:27.930 whereby each of these digits is in a different column. 00:21:27.930 --> 00:21:31.080 And those columns are, again, ones place, tens, hundreds. 00:21:31.080 --> 00:21:33.790 If we keep going, thousand, ten thousand, and so forth. 00:21:33.790 --> 00:21:35.040 But where did these come from? 00:21:35.040 --> 00:21:35.957 Well, here's the base. 00:21:35.957 --> 00:21:37.770 If you remember exponents and all of that, 00:21:37.770 --> 00:21:41.910 this is just 10 to the 0, 10 to the 1, 10 to the 2, ad infinitum. 00:21:41.910 --> 00:21:46.860 And so, now, what if we just changed the base from 10, 0 through 9, 00:21:46.860 --> 00:21:48.480 to just two digits, 0 and 1? 00:21:48.480 --> 00:21:50.610 Well, now the math is fundamentally the same. 00:21:50.610 --> 00:21:52.880 But it's 2 to the 0, 2 to the 1, 2 to the 2, 00:21:52.880 --> 00:21:57.030 which gives us the ones place, twos place, and fours place. 00:21:57.030 --> 00:21:58.410 Now, why is this relevant? 00:21:58.410 --> 00:22:02.790 If you've got three light bulbs or three bits that are off, off, off, 00:22:02.790 --> 00:22:03.930 what have I done? 00:22:03.930 --> 00:22:07.710 4 times 0 plus 2 times 0 plus 1 times 0 is, obviously, the number 00:22:07.710 --> 00:22:09.990 you and I know, in decimal, as 0. 00:22:09.990 --> 00:22:12.630 This, though, represents the number you and I know as-- 00:22:12.630 --> 00:22:13.230 AUDIENCE: 1 00:22:13.230 --> 00:22:14.605 DAVID J. MALAN: This represents-- 00:22:14.605 --> 00:22:15.195 AUDIENCE: 2. 00:22:15.195 --> 00:22:17.910 DAVID J. MALAN: --3, 4. 00:22:17.910 --> 00:22:21.540 And just to be clear, this is why, when I grabbed the additional light bulb, 00:22:21.540 --> 00:22:25.170 we claimed that this now was 4. 00:22:25.170 --> 00:22:29.430 Because we had "on, off, off," "on, off, off." 00:22:29.430 --> 00:22:30.960 This now is 5. 00:22:30.960 --> 00:22:32.190 This now is 6. 00:22:32.190 --> 00:22:33.240 This now is 7. 00:22:33.240 --> 00:22:36.480 And if I wanted to count higher, what would the pattern of 0's and 1's be 00:22:36.480 --> 00:22:37.478 for the number 8? 00:22:37.478 --> 00:22:38.942 AUDIENCE: 1, 0-- 00:22:38.942 --> 00:22:40.410 DAVID J. MALAN: 1, 0, 0, 0. 00:22:40.410 --> 00:22:42.330 So we just need more hardware, more bits. 00:22:42.330 --> 00:22:45.630 So it wasn't arbitrary, even if it was non-obvious, 00:22:45.630 --> 00:22:49.660 what I was actually doing with turning these light bulbs on and off. 00:22:49.660 --> 00:22:53.730 Now, it turns out here we are talking about how to represent information, 00:22:53.730 --> 00:22:59.490 like numbers, but we could also use bits, 0's and 1's, light bulbs 00:22:59.490 --> 00:23:01.590 to represent instructions, as well. 00:23:01.590 --> 00:23:04.050 Because, at the end of the day, that's all computers do. 00:23:04.050 --> 00:23:07.950 They process data, information of some sort, whether it's files, 00:23:07.950 --> 00:23:10.290 or numbers, or images, or videos, or the like. 00:23:10.290 --> 00:23:13.290 And you do things with those files. 00:23:13.290 --> 00:23:13.985 You open them. 00:23:13.985 --> 00:23:14.610 You print them. 00:23:14.610 --> 00:23:15.610 You edit them and the like. 00:23:15.610 --> 00:23:18.720 So there's this notion of instructions, what the computer can actually do. 00:23:18.720 --> 00:23:22.470 And I bet we could come up with some pattern of 0's and 1's 00:23:22.470 --> 00:23:26.970 or, equivalently, light bulbs that tell even Spot what to do. 00:23:26.970 --> 00:23:30.785 Maybe, go up, or down, or left, or right. 00:23:30.785 --> 00:23:33.660 And it could certainly do this autonomously by using various sensors. 00:23:33.660 --> 00:23:35.077 We need to keep things safe today. 00:23:35.077 --> 00:23:38.248 We're using Wi-Fi in sending Spot these very instructions. 00:23:38.248 --> 00:23:41.040 But what's happening, wirelessly, with our friend Andrew, here, is, 00:23:41.040 --> 00:23:45.330 essentially, he's sending Spot instructions just encoded, wirelessly, 00:23:45.330 --> 00:23:47.610 somehow, as patterns of 0's and 1's. 00:23:47.610 --> 00:23:51.390 And the folks at Boston Dynamics, who built this robot, programmed 00:23:51.390 --> 00:23:54.330 Spot to recognize certain patterns as meaning "up," 00:23:54.330 --> 00:23:57.030 a certain pattern as meaning "down," "left," "right," 00:23:57.030 --> 00:23:58.505 and any number of other things. 00:23:58.505 --> 00:24:00.630 So, in fact, Spot, come on over here, if you could. 00:24:00.630 --> 00:24:01.830 Come on, Spot. 00:24:01.830 --> 00:24:03.510 [FOOTSTEPS] 00:24:03.510 --> 00:24:05.640 OK. 00:24:05.640 --> 00:24:08.400 So Spot, for instance, may very well have a pattern 00:24:08.400 --> 00:24:11.190 of 0's and 1's that represents shake. 00:24:11.190 --> 00:24:12.720 Shake, Spot. 00:24:12.720 --> 00:24:16.330 So he could do that and any number of other movements, as well. 00:24:16.330 --> 00:24:19.680 And maybe, especially with sensors here and also a little human help over here, 00:24:19.680 --> 00:24:22.540 for today, what if we went ahead and did something-- maybe 00:24:22.540 --> 00:24:23.870 ask Spot some questions? 00:24:23.870 --> 00:24:25.660 So let's go ahead, and we'll start simple. 00:24:25.660 --> 00:24:31.150 Spot, here we have some bits, "off, off, on." 00:24:31.150 --> 00:24:34.030 Spot, what [LAUGHS]-- OK. 00:24:34.030 --> 00:24:41.260 Spot, what is this representing, "off, off, on"? 00:24:41.260 --> 00:24:43.710 [SPOT TAPS STAGE] 00:24:43.710 --> 00:24:44.970 Correct? 00:24:44.970 --> 00:24:45.900 I think so. 00:24:45.900 --> 00:24:46.900 Very horse-like, OK. 00:24:46.900 --> 00:24:47.400 Thank you. 00:24:47.400 --> 00:24:49.400 All right, so a round of applause for Spot here. 00:24:49.400 --> 00:24:50.310 [APPLAUSE] 00:24:50.310 --> 00:24:51.450 All right. 00:24:51.450 --> 00:24:54.400 So, Spot, what if we [LAUGHS] turn-- 00:24:54.400 --> 00:24:54.900 OK. 00:24:54.900 --> 00:24:56.160 We'll turn that one off here. 00:24:56.160 --> 00:24:57.535 How about if we turn this one on? 00:24:57.535 --> 00:24:59.280 So it's "off, on, off." 00:24:59.280 --> 00:25:02.470 Spot, what's this number? 00:25:02.470 --> 00:25:04.310 [SPOT TAPPING STAGE] 00:25:04.310 --> 00:25:05.570 Is that correct? 00:25:05.570 --> 00:25:06.150 Nice. 00:25:06.150 --> 00:25:06.650 OK. 00:25:06.650 --> 00:25:07.655 [APPLAUSE] 00:25:07.655 --> 00:25:09.620 [LAUGHS] How about one final flourish? 00:25:09.620 --> 00:25:12.440 How about, Spot, instead of "off, on, off," 00:25:12.440 --> 00:25:16.280 let's go ahead and do "off, on, on"? 00:25:16.280 --> 00:25:20.810 So think, in your mind's eye, what the answer should be. 00:25:20.810 --> 00:25:24.104 All right, Spot, "off, on, on." 00:25:24.104 --> 00:25:27.420 [SPOT LANDING ON THE STAGE] 00:25:27.920 --> 00:25:28.460 OK. 00:25:28.460 --> 00:25:30.830 And a round of applause for, Spot, as well. 00:25:30.830 --> 00:25:31.816 [LAUGHS] 00:25:31.816 --> 00:25:33.790 [APPLAUSE] 00:25:33.790 --> 00:25:36.450 So this is to say, no matter how-- 00:25:36.450 --> 00:25:37.140 thank you, Spot. 00:25:37.140 --> 00:25:40.620 No matter how fancy today's hardware or software is, 00:25:40.620 --> 00:25:42.720 it really just boils down to representing 00:25:42.720 --> 00:25:44.838 information and instructions. 00:25:44.838 --> 00:25:47.130 And computers, and phones, and the like really are just 00:25:47.130 --> 00:25:51.460 operating on those same pieces of information, 00:25:51.460 --> 00:25:55.240 whether implemented in 0's and 1's or with, really, anything else. 00:25:55.240 --> 00:25:55.740 All right. 00:25:55.740 --> 00:25:59.880 So, where can we take this, once we have this agreed-upon system 00:25:59.880 --> 00:26:03.030 for representing [LAUGHS] information? 00:26:03.030 --> 00:26:06.960 Well, it turns out that using three bits, three 0's and 1's, at a time, 00:26:06.960 --> 00:26:08.700 isn't actually all that useful. 00:26:08.700 --> 00:26:12.240 And you and I, even in conversation, don't often say the word "bit." 00:26:12.240 --> 00:26:13.500 We say the word "byte." 00:26:13.500 --> 00:26:15.280 And what is a byte, if familiar? 00:26:15.280 --> 00:26:15.780 Yeah? 00:26:15.780 --> 00:26:16.750 AUDIENCE: It's eight bits. 00:26:16.750 --> 00:26:18.417 DAVID J. MALAN: So it's just eight bits. 00:26:18.417 --> 00:26:20.200 It's just a more useful unit of measure. 00:26:20.200 --> 00:26:22.715 And it happens to be a power of 2, 2 to the third, 00:26:22.715 --> 00:26:24.340 which just makes math work out cleanly. 00:26:24.340 --> 00:26:27.610 But it's just a convention, to have more interesting units of measure 00:26:27.610 --> 00:26:28.540 than individual bits. 00:26:28.540 --> 00:26:30.350 So a byte is eight bits. 00:26:30.350 --> 00:26:34.690 So, for instance, this represents, using eight bits, eight light bulbs, 00:26:34.690 --> 00:26:36.460 the number you and I know is 0. 00:26:36.460 --> 00:26:40.960 And this byte of all 1 bits-- 00:26:40.960 --> 00:26:46.040 now you've got to do some quick math-- represents what number, instead? 00:26:46.040 --> 00:26:48.657 So it's all 1's; eight of them, total. 00:26:48.657 --> 00:26:49.990 How about over here, on the end? 00:26:49.990 --> 00:26:51.050 AUDIENCE: 255. 00:26:51.050 --> 00:26:52.992 DAVID J. MALAN: So it's, indeed, 255. 00:26:52.992 --> 00:26:54.700 Now, that's not the kind of math that you 00:26:54.700 --> 00:26:57.310 need to do in your head for a class like this, but you could. 00:26:57.310 --> 00:27:04.690 This is the ones place, twos, fours, eight, 16, 32, 64, 128. 00:27:04.690 --> 00:27:08.800 And because they're all 1, you just have to add up all of those columns' values. 00:27:08.800 --> 00:27:10.570 And you get 255. 00:27:10.570 --> 00:27:13.690 But a little mental trick, too, is that, if you've got eight bits 00:27:13.690 --> 00:27:17.270 and each of them can be two possible values, 0 or 1, 00:27:17.270 --> 00:27:21.740 that's like 2 possibilities here times 2 times 2 times 2 times 2, eight times. 00:27:21.740 --> 00:27:25.310 So that's 2 to the eighth, so that is maybe a little easier to do. 00:27:25.310 --> 00:27:26.720 That's 256. 00:27:26.720 --> 00:27:29.780 Or easier, in the sense that you get used to seeing these numbers in CS. 00:27:29.780 --> 00:27:31.220 That's 256. 00:27:31.220 --> 00:27:32.030 But wait a minute. 00:27:32.030 --> 00:27:35.300 How do I reconcile this with your 255? 00:27:35.300 --> 00:27:36.650 Because you start at 0. 00:27:36.650 --> 00:27:41.120 So you lose one on the high end because we started counting and representing 00:27:41.120 --> 00:27:42.830 the number, like 0. 00:27:42.830 --> 00:27:43.760 All right. 00:27:43.760 --> 00:27:47.570 Questions on how we've represented just numbers or, for instance, 00:27:47.570 --> 00:27:51.600 instructions to Spot, thus far? 00:27:51.600 --> 00:27:57.410 Any questions on binary, unary, or the like? 00:27:57.410 --> 00:27:58.010 No? 00:27:58.010 --> 00:28:01.137 All right, so seeing none, let's let things escalate a bit. 00:28:01.137 --> 00:28:03.470 So how do you represent letters because, obviously, this 00:28:03.470 --> 00:28:05.262 makes our devices more useful, whether it's 00:28:05.262 --> 00:28:07.020 in English or any other human language. 00:28:07.020 --> 00:28:10.740 How could we go about representing the letter A, for instance? 00:28:10.740 --> 00:28:13.370 If, at the end of the day, all our computers, 00:28:13.370 --> 00:28:17.390 all our phones have access to is electricity; or, equivalently, 00:28:17.390 --> 00:28:20.570 switches; or, metaphorically, tiny little light bulbs 00:28:20.570 --> 00:28:23.750 inside of them that can be on and off-- that's it. 00:28:23.750 --> 00:28:26.990 There's no more building blocks to give you. 00:28:26.990 --> 00:28:30.530 How could we represent something like the letter A? 00:28:30.530 --> 00:28:32.000 Yeah, how about here? 00:28:32.000 --> 00:28:32.812 Yeah? 00:28:32.812 --> 00:28:34.312 AUDIENCE: You could assign a number. 00:28:34.312 --> 00:28:35.343 DAVID J. MALAN: Perfect. 00:28:35.343 --> 00:28:37.260 So we could just assign every letter a number. 00:28:37.260 --> 00:28:38.677 And we could do this super simply. 00:28:38.677 --> 00:28:42.660 Maybe 0 is A, and 1 is B. Or maybe 1 is A, 00:28:42.660 --> 00:28:46.110 and 2 is B. It doesn't really matter, so long as we all agree and we all 00:28:46.110 --> 00:28:48.660 use the same types of computers, in some sense, ultimately. 00:28:48.660 --> 00:28:51.730 Well, for various reasons, the humans that designed this system, 00:28:51.730 --> 00:28:53.160 they went with the number 65. 00:28:53.160 --> 00:28:57.540 So, nowadays, anytime your computer is showing you the capital letter 00:28:57.540 --> 00:29:00.690 A on the screen, underneath the hood, so to speak, 00:29:00.690 --> 00:29:06.780 it's actually storing a pattern of 0's and 1's that represents the number 65. 00:29:06.780 --> 00:29:09.930 And it tends to use seven bits or, typically, eight bits, total, 00:29:09.930 --> 00:29:12.790 even if it doesn't need all of those bits in total. 00:29:12.790 --> 00:29:14.980 So how do we get there? 00:29:14.980 --> 00:29:17.490 Well, here, for instance, is that same pattern. 00:29:17.490 --> 00:29:19.980 Here is that pattern of bits that represents 65. 00:29:19.980 --> 00:29:20.520 And why? 00:29:20.520 --> 00:29:22.200 Well, quick check here. 00:29:22.200 --> 00:29:29.160 This is the ones place, twos, fours, eights, 16, 32, 64's place. 00:29:29.160 --> 00:29:32.640 OK, so 64 plus 1 gives me 65. 00:29:32.640 --> 00:29:36.830 So that is to say here's how a computer, using some light switches, so to speak, 00:29:36.830 --> 00:29:38.620 would represent the number 65. 00:29:38.620 --> 00:29:41.540 And our Macs, our PCs, our phones just all know this. 00:29:41.540 --> 00:29:44.410 So whenever they see that in their memory, so to speak, 00:29:44.410 --> 00:29:47.560 they show a capital letter A on the screen. 00:29:47.560 --> 00:29:48.520 So that's it. 00:29:48.520 --> 00:29:51.250 That's the system known as ASCII, the American Standard 00:29:51.250 --> 00:29:52.840 Code for Information Interchange. 00:29:52.840 --> 00:29:57.340 And the A is actually operative there because we're only talking, thus far, 00:29:57.340 --> 00:29:59.500 about English letters in our alphabet. 00:29:59.500 --> 00:30:02.530 And, in fact, I claimed, a moment ago, that we only use 00:30:02.530 --> 00:30:06.250 seven, maybe eight bits to represent letters of an alphabet. 00:30:06.250 --> 00:30:08.410 So, just to come back to you, if I may, how many 00:30:08.410 --> 00:30:10.900 possible letters of the alphabet could-- how 00:30:10.900 --> 00:30:14.350 many possible letters of any alphabet could we represent with eight bits? 00:30:14.350 --> 00:30:15.310 AUDIENCE: 256. 00:30:15.310 --> 00:30:18.400 DAVID J. MALAN: 256, the numbers 0 through 255. 00:30:18.400 --> 00:30:20.650 Now, that's more than enough for English because we've 00:30:20.650 --> 00:30:23.498 got A through Z, uppercase, lowercase, a bunch of numbers, 00:30:23.498 --> 00:30:24.790 a bunch of punctuation symbols. 00:30:24.790 --> 00:30:28.660 But in a lot of languages, with accented characters, a lot of Asian characters, 00:30:28.660 --> 00:30:32.230 this is not nearly enough memory or bits with which 00:30:32.230 --> 00:30:34.717 to represent all of those possible values. 00:30:34.717 --> 00:30:36.550 So we need to do a little better than ASCII, 00:30:36.550 --> 00:30:39.200 but we can build on top of what they did years ago. 00:30:39.200 --> 00:30:41.080 So here is a chart of ASCII codes. 00:30:41.080 --> 00:30:44.270 It's just a bunch of columns showing us the mapping between letters 00:30:44.270 --> 00:30:44.770 and numbers. 00:30:44.770 --> 00:30:49.420 So, for instance, up here is the capital letter A, 65; capital B, 66; 00:30:49.420 --> 00:30:52.060 capital C, 67; dot, dot, dot. 00:30:52.060 --> 00:30:56.333 72 is H. 73 is I and so forth. 00:30:56.333 --> 00:30:58.750 There's some weird things over here, like special symbols, 00:30:58.750 --> 00:31:00.125 that we'll learn about over time. 00:31:00.125 --> 00:31:02.230 But there's a mapping between every English letter 00:31:02.230 --> 00:31:04.810 of the alphabet and some number, just as you'd propose, 00:31:04.810 --> 00:31:06.710 both for uppercase and lowercase. 00:31:06.710 --> 00:31:10.580 So, for instance, if we highlight just a few of these for now 00:31:10.580 --> 00:31:14.998 and I say that I've just received a text message or an email that, 00:31:14.998 --> 00:31:17.290 underneath the hood, so to speak, if I have the ability 00:31:17.290 --> 00:31:22.930 to look at what switches are on and off, I received this message here. 00:31:22.930 --> 00:31:26.020 Well, first-- and this is not what CS is about, but just fun fact. 00:31:26.020 --> 00:31:29.470 Does anyone know what number this would represent in decimal, 00:31:29.470 --> 00:31:34.565 if this is the binary pattern, like ones place, twos place? 00:31:34.565 --> 00:31:35.420 AUDIENCE: 72? 00:31:35.420 --> 00:31:37.080 DAVID J. MALAN: 72 is correct. 00:31:37.080 --> 00:31:39.600 And again, not, intellectually, all that interesting 00:31:39.600 --> 00:31:41.780 and this is not the kind of math that we spend all day as CS-- 00:31:41.780 --> 00:31:42.905 a computer scientist doing. 00:31:42.905 --> 00:31:45.380 But it's just following the same darn pattern, which 00:31:45.380 --> 00:31:47.780 is to say it might look cryptic, but, conceptually, 00:31:47.780 --> 00:31:50.542 intellectually, it ultimately is exactly as we did before. 00:31:50.542 --> 00:31:52.250 So, yes, I'll spoil the rest of the math. 00:31:52.250 --> 00:31:54.860 It's 72, 73, 33. 00:31:54.860 --> 00:31:58.370 Now, anyone remember, in your mind's eye, what message we just spelled? 00:31:58.370 --> 00:31:59.180 AUDIENCE: Hi. 00:31:59.180 --> 00:31:59.810 AUDIENCE: Hi. 00:31:59.810 --> 00:32:01.520 DAVID J. MALAN: Yeah, so it is, in fact, "Hi!" 00:32:01.520 --> 00:32:03.270 Though, no one really said that excitedly. 00:32:03.270 --> 00:32:04.580 What's the 33, if you noticed? 00:32:04.580 --> 00:32:05.600 AUDIENCE: Exclamation point. 00:32:05.600 --> 00:32:06.770 DAVID J. MALAN: OK, so a lot of people noticed. 00:32:06.770 --> 00:32:08.630 Yes, it's an exclamation point. 00:32:08.630 --> 00:32:11.300 And that's, indeed, noticeable right here. 00:32:11.300 --> 00:32:13.063 33 is the exclamation point. 00:32:13.063 --> 00:32:15.480 And that's just something, eventually, that might sink in. 00:32:15.480 --> 00:32:18.350 But, for the most part, if you remember capital A is 65, 00:32:18.350 --> 00:32:22.100 you can figure out at least 25 other answers to these kinds of questions 00:32:22.100 --> 00:32:24.060 because they're all contiguous like that. 00:32:24.060 --> 00:32:25.740 So there's the exclamation point. 00:32:25.740 --> 00:32:30.350 But at the end of the day, we might just have this mapping known as ASCII. 00:32:30.350 --> 00:32:33.000 And it's how our phones, and computers, and devices, more 00:32:33.000 --> 00:32:35.310 generally, actually store information. 00:32:35.310 --> 00:32:37.050 So we thought we'd make-- 00:32:37.050 --> 00:32:38.970 maybe take a little pressure off of me here. 00:32:38.970 --> 00:32:41.550 And could we maybe flip things around? 00:32:41.550 --> 00:32:44.160 How about we try applying this newfound knowledge-- 00:32:44.160 --> 00:32:48.430 if it's, indeed, new to you-- with seven volunteers, seven bits, if we could? 00:32:48.430 --> 00:32:48.930 OK. 00:32:48.930 --> 00:32:49.847 I saw your hand first. 00:32:49.847 --> 00:32:50.640 Come on down. 00:32:50.640 --> 00:32:52.650 Maybe your hand, there. 00:32:52.650 --> 00:32:54.870 OK, three, come on down over here. 00:32:54.870 --> 00:32:56.940 How about four and five? 00:32:56.940 --> 00:32:57.810 Yep, come on down. 00:32:57.810 --> 00:32:59.018 Yep, in the black shirt, yep. 00:32:59.018 --> 00:33:00.393 How about let me go farther back? 00:33:00.393 --> 00:33:01.890 How about in the green, over there? 00:33:01.890 --> 00:33:03.460 And how about you, seven, over here? 00:33:03.460 --> 00:33:03.960 All right. 00:33:03.960 --> 00:33:04.650 Come on down. 00:33:04.650 --> 00:33:05.790 [CHATTER] 00:33:05.790 --> 00:33:08.400 Come on down. 00:33:08.400 --> 00:33:10.600 So a round of applause for our brave volunteers. 00:33:10.600 --> 00:33:12.740 [APPLAUSE] 00:33:12.740 --> 00:33:13.850 All right. 00:33:13.850 --> 00:33:17.060 So if you'd like to stand, roughly, side by side, here in the middle 00:33:17.060 --> 00:33:17.830 of the stage. 00:33:20.430 --> 00:33:22.530 First of all, thank you. 00:33:22.530 --> 00:33:23.030 Let's see. 00:33:23.030 --> 00:33:25.250 1, 2, 3, 4, 5, 6, 7, perfect. 00:33:25.250 --> 00:33:25.880 OK. 00:33:25.880 --> 00:33:29.900 And let's go all the way over to this place, here. 00:33:29.900 --> 00:33:32.600 If you would like to introduce yourself to the class. 00:33:32.600 --> 00:33:34.418 RACHEL RICHEY: I'm Rachel Richey. 00:33:34.418 --> 00:33:35.210 DAVID J. MALAN: OK. 00:33:35.210 --> 00:33:36.128 And what year? 00:33:36.128 --> 00:33:36.920 Anything about you? 00:33:36.920 --> 00:33:38.915 RACHEL RICHEY: Oh, first year, concentrating in CS. 00:33:38.915 --> 00:33:39.707 DAVID J. MALAN: OK. 00:33:39.707 --> 00:33:40.670 Welcome to the stage. 00:33:40.670 --> 00:33:41.210 Next. 00:33:41.210 --> 00:33:42.080 SPEAKER: Hi. 00:33:42.080 --> 00:33:46.190 I'm [? Kang. ?] Also a first-year concentrating in CS. 00:33:46.190 --> 00:33:47.390 SPEAKER: Hello. 00:33:47.390 --> 00:33:52.665 My name is [? Lam. ?] I'm a [INAUDIBLE] student from education department. 00:33:52.665 --> 00:33:53.540 DAVID J. MALAN: Nice. 00:33:53.540 --> 00:33:53.810 OK. 00:33:53.810 --> 00:33:54.368 Next. 00:33:54.368 --> 00:33:55.160 JORDAN MITTLER: Hi. 00:33:55.160 --> 00:33:59.120 I'm Jordan Mittler, concentrating in economics and maybe some CS. 00:33:59.120 --> 00:34:00.110 SPEAKER: So, hi. 00:34:00.110 --> 00:34:02.645 I'm [? Natalia. ?] First year, and I want to do CS. 00:34:02.645 --> 00:34:03.435 SPEAKER: Hi. 00:34:03.435 --> 00:34:05.810 I'm [? Khadija. ?] I'm a first-year, and I want to do CS. 00:34:05.810 --> 00:34:07.080 DAVID J. MALAN: [LAUGHS]. 00:34:07.080 --> 00:34:08.030 SPEAKER: Hello. 00:34:08.030 --> 00:34:11.418 I'm [? Caleb. ?] And, once again, first year, concentrating in CS. 00:34:11.418 --> 00:34:12.210 DAVID J. MALAN: OK. 00:34:12.210 --> 00:34:12.710 Wonderful. 00:34:12.710 --> 00:34:13.770 A pattern, yes. 00:34:13.770 --> 00:34:14.340 Thank you. 00:34:14.340 --> 00:34:14.880 Thank you. 00:34:14.880 --> 00:34:15.969 [APPLAUSE] 00:34:15.969 --> 00:34:20.010 So, if you haven't guessed already, each of these volunteers 00:34:20.010 --> 00:34:23.429 is going to represent a bit, from left to right, or right to left, 00:34:23.429 --> 00:34:24.010 in this case. 00:34:24.010 --> 00:34:24.750 So let's see. 00:34:24.750 --> 00:34:27.719 If you want to represent-- how about the twos place? 00:34:27.719 --> 00:34:37.350 How about the fours place, the eighths place, 16ths place, 32's, 64, and 128? 00:34:37.350 --> 00:34:39.060 Although, wait a-- I think I screwed up. 00:34:39.060 --> 00:34:40.889 We needed one-- eighth volunteer. 00:34:40.889 --> 00:34:42.974 I think you know-- well, I think-- 00:34:42.974 --> 00:34:45.330 [CLEARS THROAT] Spot? 00:34:45.330 --> 00:34:46.108 OK. 00:34:46.108 --> 00:34:46.650 Come on over. 00:34:46.650 --> 00:34:49.567 If you guys could step forward a little bit, and then scooch this way, 00:34:49.567 --> 00:34:51.090 just to give Spot some room. 00:34:51.090 --> 00:34:52.650 [FOOTSTEPS] 00:34:52.650 --> 00:34:55.239 So Spot will represent the ones place. 00:34:55.239 --> 00:34:58.440 Now, what our volunteers have on the back of their sheets of paper 00:34:58.440 --> 00:34:59.670 are little instructions. 00:34:59.670 --> 00:35:05.460 We're going to spell out a three-letter word in English by using three bytes, 00:35:05.460 --> 00:35:08.170 from left to right, because now we have eight volunteers. 00:35:08.170 --> 00:35:10.503 I'm going to propose that you raise your hand, if you're 00:35:10.503 --> 00:35:12.185 supposed to represent a 1. 00:35:12.185 --> 00:35:14.310 Or you just stand there, without raising your hand, 00:35:14.310 --> 00:35:15.780 if you're meant to represent a 0. 00:35:15.780 --> 00:35:18.450 And what we'll have the audience do is do the quick math 00:35:18.450 --> 00:35:22.170 to figure out-- one, two, three-- each letter, what number is it. 00:35:22.170 --> 00:35:23.430 What letter is it? 00:35:23.430 --> 00:35:26.370 And we'll see what word that we have finally spelled. 00:35:26.370 --> 00:35:26.880 All right. 00:35:26.880 --> 00:35:28.410 So, in round one-- 00:35:28.410 --> 00:35:31.410 you have instructions on your back of your sheet that will tell you to-- 00:35:31.410 --> 00:35:32.580 what your number is. 00:35:32.580 --> 00:35:33.660 If you're 0, stand there. 00:35:33.660 --> 00:35:35.010 If you're a 1, raise your hand. 00:35:37.700 --> 00:35:40.060 [PAPER RUSTLING] 00:35:40.060 --> 00:35:43.420 What number do these guys seem to be representing? 00:35:43.420 --> 00:35:44.560 AUDIENCE: 68. 00:35:44.560 --> 00:35:46.360 DAVID J. MALAN: 66, I think. 00:35:46.360 --> 00:35:49.495 64 plus 2, so 66, which is the letter-- 00:35:49.495 --> 00:35:50.260 AUDIENCE: B. 00:35:50.260 --> 00:35:52.930 DAVID J. MALAN: OK, so, B. OK, so, B. All right. 00:35:52.930 --> 00:35:54.470 Hands down. 00:35:54.470 --> 00:35:57.565 Second letter is going to be spelled how? 00:36:01.160 --> 00:36:02.202 [SPOT LANDS ON THE STAGE] 00:36:02.202 --> 00:36:02.827 AUDIENCE: Whoa. 00:36:02.827 --> 00:36:03.530 AUDIENCE: Whoa. 00:36:03.530 --> 00:36:04.829 AUDIENCE: Whoa. 00:36:04.829 --> 00:36:07.570 DAVID J. MALAN: [LAUGHS] All right. 00:36:07.570 --> 00:36:09.670 What are we spelling now? 00:36:09.670 --> 00:36:11.635 [INDISTINCT CHATTER] 00:36:11.635 --> 00:36:15.580 I think-- psst, yep, OK. 00:36:15.580 --> 00:36:17.081 Yeah, I think you're one. 00:36:17.081 --> 00:36:19.490 [LAUGHTER] 00:36:19.490 --> 00:36:20.980 OK. 00:36:20.980 --> 00:36:23.430 Now what number are we spelling? 00:36:23.430 --> 00:36:24.370 AUDIENCE: 79 00:36:24.370 --> 00:36:26.770 DAVID J. MALAN: 79, I heard, which is the letter? 00:36:26.770 --> 00:36:27.383 AUDIENCE: O. 00:36:27.383 --> 00:36:28.300 DAVID J. MALAN: O. OK. 00:36:28.300 --> 00:36:29.320 So hands down. 00:36:29.320 --> 00:36:30.100 Thank you, Spot. 00:36:30.100 --> 00:36:31.460 One final flourish. 00:36:31.460 --> 00:36:33.250 So we've spelled B-O-- 00:36:33.250 --> 00:36:34.855 third letter, go ahead. 00:36:37.942 --> 00:36:40.230 [SPOT LANDS ON THE STAGE] 00:36:40.230 --> 00:36:42.885 What number, now, is this? 00:36:42.885 --> 00:36:43.815 AUDIENCE: 87. 00:36:43.815 --> 00:36:44.460 AUDIENCE: 87. 00:36:44.460 --> 00:36:45.460 DAVID J. MALAN: I heard it here, 80-- 00:36:45.460 --> 00:36:46.320 AUDIENCE: Seven. 00:36:46.320 --> 00:36:46.875 DAVID J. MALAN: --seven, which is? 00:36:46.875 --> 00:36:47.505 AUDIENCE: W. 00:36:47.505 --> 00:36:49.620 DAVID J. MALAN: W, which, of course, spells "bow." 00:36:49.620 --> 00:36:53.437 So if our volunteers could take a bow, Spot included. 00:36:53.437 --> 00:36:56.636 [APPLAUSE] 00:36:58.010 --> 00:37:01.540 So this will make more sense in week one, 00:37:01.540 --> 00:37:04.750 when we have an assignment involving a certain someone from the Nintendo 00:37:04.750 --> 00:37:04.900 World. 00:37:04.900 --> 00:37:06.760 But we have a lovely parting gift for each of you. 00:37:06.760 --> 00:37:07.510 SPEAKER: Thank you. 00:37:07.510 --> 00:37:07.630 [LAUGHS] 00:37:07.630 --> 00:37:08.180 DAVID J. MALAN: Thank you for volunteering. 00:37:08.180 --> 00:37:08.980 SPEAKER: Thanks. 00:37:08.980 --> 00:37:11.897 DAVID J. MALAN: You might need to share it with the folks next to you. 00:37:11.897 --> 00:37:15.106 [CHATTER] 00:37:18.600 --> 00:37:19.470 Oop, here we go. 00:37:19.470 --> 00:37:19.970 There we go. 00:37:19.970 --> 00:37:20.360 Thank you-- 00:37:20.360 --> 00:37:21.080 SPEAKER: Thank you. 00:37:21.080 --> 00:37:21.380 RACHEL RICHEY: Thank you. 00:37:21.380 --> 00:37:21.560 DAVID J. MALAN: --so much. 00:37:21.560 --> 00:37:23.250 One more round of applause, if we could, for our volunteers. 00:37:23.250 --> 00:37:23.810 Thank you. 00:37:23.810 --> 00:37:24.734 [APPLAUSE] 00:37:24.734 --> 00:37:26.120 Did you lose something? 00:37:26.120 --> 00:37:27.490 OK. 00:37:27.490 --> 00:37:28.030 All right. 00:37:28.030 --> 00:37:31.190 So, [LAUGHS] Spot's had it. 00:37:31.190 --> 00:37:34.060 So let's see, then, if we've solved, now, 00:37:34.060 --> 00:37:36.640 the problem of representing English letters of the alphabet, 00:37:36.640 --> 00:37:39.850 being able to spell out words like "bow," B-O-W. What if we actually do 00:37:39.850 --> 00:37:41.210 have accented characters? 00:37:41.210 --> 00:37:43.883 What if we do have other glyphs that we want to represent? 00:37:43.883 --> 00:37:47.050 Well, here, of course, is a standard US English keyboard, which a lot of you 00:37:47.050 --> 00:37:47.650 might have. 00:37:47.650 --> 00:37:50.260 But there's also characters that you can type much more 00:37:50.260 --> 00:37:52.930 easily if you have a foreign keyboard, relative to the US, 00:37:52.930 --> 00:37:55.780 or with certain keystrokes on your own Mac, PC, and phone. 00:37:55.780 --> 00:37:59.230 But, nowadays, too, there's this menu that, probably, you've 00:37:59.230 --> 00:38:03.400 used in the past hour or two to actually send some emoji. 00:38:03.400 --> 00:38:06.040 An emoji, even though they look like pictures 00:38:06.040 --> 00:38:08.080 and they actually are pictures on the screen, 00:38:08.080 --> 00:38:13.120 they're, technically, just characters, characters of an emoji alphabet 00:38:13.120 --> 00:38:16.330 that happened to use a certain pattern of 0's and 1's to represent 00:38:16.330 --> 00:38:19.880 each of these faces, each of these people, and places, and things. 00:38:19.880 --> 00:38:22.540 And it turns out that one of the reasons that we have just 00:38:22.540 --> 00:38:29.240 so many [LAUGHS] such characters nowadays is because we now 00:38:29.240 --> 00:38:31.370 use Unicode instead of ASCII. 00:38:31.370 --> 00:38:34.370 So Unicode is a superset, so to speak, of ASCII, 00:38:34.370 --> 00:38:38.030 which is to say that we, humans, realized, some time ago, that just 00:38:38.030 --> 00:38:40.370 using eight bits to represent letters of the alphabet 00:38:40.370 --> 00:38:43.070 certainly isn't very good when we want to represent 00:38:43.070 --> 00:38:44.960 other, non-English languages. 00:38:44.960 --> 00:38:47.240 So Unicode doesn't just use eight bits. 00:38:47.240 --> 00:38:52.520 It sometimes uses 16 bits per character, sometimes 24 bits per character, 00:38:52.520 --> 00:38:55.610 and sometimes even 32 bits per character. 00:38:55.610 --> 00:38:56.690 Now, why those numbers? 00:38:56.690 --> 00:39:00.680 That's just one byte, two bytes, three bytes, or four bytes. 00:39:00.680 --> 00:39:02.132 And that gives us-- 00:39:02.132 --> 00:39:02.840 does anyone know? 00:39:02.840 --> 00:39:06.380 That gives us the ability to represent as many as 4 00:39:06.380 --> 00:39:08.630 billion possible characters. 00:39:08.630 --> 00:39:12.150 Because if the longest one is 32 bits, that's 2 to the 32, 00:39:12.150 --> 00:39:15.090 which, if you do out the math, trust me, is roughly 4 billion. 00:39:15.090 --> 00:39:16.430 So that's a lot of characters. 00:39:16.430 --> 00:39:19.040 And we've got a lot of room, then, for these emoji. 00:39:19.040 --> 00:39:22.130 But it's not just about having fun, pictorially, on the screen. 00:39:22.130 --> 00:39:27.830 Unicode's mission really is to represent and to preserve all human languages 00:39:27.830 --> 00:39:31.530 digitally, both past, present, and future. 00:39:31.530 --> 00:39:35.420 So it is really about capturing the entirety of human knowledge, 00:39:35.420 --> 00:39:37.430 as we've expressed it in language, but also 00:39:37.430 --> 00:39:42.020 giving this newfound ability that's been used centuries ago, too-- in writings, 00:39:42.020 --> 00:39:44.300 on walls, and the like-- pictograms via which 00:39:44.300 --> 00:39:46.640 we can still communicate, even independently 00:39:46.640 --> 00:39:48.030 of our own human language. 00:39:48.030 --> 00:39:51.320 So we'll reduce it, today, to just patterns of 0's and 1's, but 00:39:51.320 --> 00:39:56.190 the problem being solved is much greater and well-beyond CS, itself, there. 00:39:56.190 --> 00:39:58.610 So here is a pattern of 0's and 1's using 00:39:58.610 --> 00:40:04.310 Unicode, so more than eight bits, that represents a very popular emoji, which 00:40:04.310 --> 00:40:06.090 might be a bit of a hint. 00:40:06.090 --> 00:40:08.870 This is the most popular emoji, as of last year, at least, 00:40:08.870 --> 00:40:10.330 statistically, internationally. 00:40:10.330 --> 00:40:13.078 [INTERPOSING VOICES] 00:40:13.078 --> 00:40:14.370 DAVID J. MALAN: Does this help? 00:40:14.370 --> 00:40:16.520 It's, roughly, this number here. 00:40:16.520 --> 00:40:17.240 No? 00:40:17.240 --> 00:40:19.080 It's this one here. 00:40:19.080 --> 00:40:23.360 So this is the most popular emoji, by most measures, as of last year. 00:40:23.360 --> 00:40:25.170 But it doesn't always look like this. 00:40:25.170 --> 00:40:28.190 Those of you who have a Mac or an iPhone recognize this symbol, perhaps, 00:40:28.190 --> 00:40:28.690 immediately. 00:40:28.690 --> 00:40:31.100 Those of you with Android devices or other platforms 00:40:31.100 --> 00:40:35.310 might notice that it's the same idea, but it's a little bit different. 00:40:35.310 --> 00:40:38.660 And this is because, too, emojis, at the end of the day, 00:40:38.660 --> 00:40:41.420 just represent character, but those characters can be drawn, 00:40:41.420 --> 00:40:43.070 can be painted in different ways. 00:40:43.070 --> 00:40:46.130 And reasonable people will interpret differently 00:40:46.130 --> 00:40:49.880 this emoji, whose official name is "face with tears of joy." 00:40:49.880 --> 00:40:52.130 And, indeed, Google interprets it a little differently 00:40:52.130 --> 00:40:55.813 from Apple, versus Microsoft, versus Meta, versus other companies, as well. 00:40:55.813 --> 00:40:57.980 So you can almost think of those different companies 00:40:57.980 --> 00:41:00.230 as having different fonts for emoji. 00:41:00.230 --> 00:41:04.160 And that really starts to connect things to the world of text and characters. 00:41:04.160 --> 00:41:05.720 So, just so you've seen it. 00:41:05.720 --> 00:41:07.110 More on this, another time. 00:41:07.110 --> 00:41:10.980 It turns out that emoji and, really, characters, in general, 00:41:10.980 --> 00:41:13.430 we don't use binary 0's and 1's to represent them 00:41:13.430 --> 00:41:15.740 because no one, myself included, is going to recognize what's what. 00:41:15.740 --> 00:41:16.520 It's just too much math. 00:41:16.520 --> 00:41:17.480 It's not interesting. 00:41:17.480 --> 00:41:19.880 And even decimal numbers-- that was 4 billion or some-- 00:41:19.880 --> 00:41:22.260 I don't remember which number is which. 00:41:22.260 --> 00:41:24.410 So we represent things a little more compactly. 00:41:24.410 --> 00:41:26.510 And this, too, admittedly, still looks cryptic, 00:41:26.510 --> 00:41:30.770 but this is a Unicode code point that uses another system, mathematically, 00:41:30.770 --> 00:41:33.470 called base-16 or hexadecimal. 00:41:33.470 --> 00:41:34.670 More on that, another time. 00:41:34.670 --> 00:41:38.090 But it's just a way of representing numbers even more succinctly, 00:41:38.090 --> 00:41:41.270 writing less on the screen, because you're using not just 0 00:41:41.270 --> 00:41:42.590 through 9, as in decimal. 00:41:42.590 --> 00:41:46.010 But you're using A through F, as well, so a few letters 00:41:46.010 --> 00:41:47.670 of the English alphabet come into play. 00:41:47.670 --> 00:41:49.700 But, for now, that's just a little easier 00:41:49.700 --> 00:41:53.750 to remember, too, for people who care, that that is the number that 00:41:53.750 --> 00:41:55.880 represents "face with tears of joy." 00:41:55.880 --> 00:41:58.010 But what if we want a customized emoji? 00:41:58.010 --> 00:41:59.780 And this, increasingly, is the case. 00:41:59.780 --> 00:42:01.910 Here, for instance, are the five skin tones 00:42:01.910 --> 00:42:04.760 that phones, and laptops, and desktops, nowadays, support. 00:42:04.760 --> 00:42:07.802 It's based on something called the "Fitzpatrick scale," which essentially 00:42:07.802 --> 00:42:10.460 categorizes human skin tone into six or, in this case, five 00:42:10.460 --> 00:42:12.560 different categories, from lighter to darker. 00:42:12.560 --> 00:42:17.480 But this suggests that, wow, if we want to represent people 00:42:17.480 --> 00:42:22.040 with five different skin tones, like this, that could significantly 00:42:22.040 --> 00:42:25.130 increase how many unique patterns of 0's and 1's we 00:42:25.130 --> 00:42:26.890 need for every possible face. 00:42:26.890 --> 00:42:29.540 But if we think about it from an engineering perspective, 00:42:29.540 --> 00:42:33.068 we can actually just think of skin tone as modifying some default 00:42:33.068 --> 00:42:34.360 color, for better or for worse. 00:42:34.360 --> 00:42:37.450 And yellow is the de facto default, Simpson style. 00:42:37.450 --> 00:42:41.350 But to modify it to look more genuinely human-like, from lighter to darker, 00:42:41.350 --> 00:42:43.990 well, maybe we just use the same pattern of bits 00:42:43.990 --> 00:42:47.720 to represent a human thumb, for instance, thumbs up or thumbs down. 00:42:47.720 --> 00:42:51.210 And we just, then, modify that character to be displayed with a different skin 00:42:51.210 --> 00:42:51.710 tone. 00:42:51.710 --> 00:42:53.950 So, for instance, here, then, is the "thumbs up" 00:42:53.950 --> 00:42:55.750 that you might use on various platforms. 00:42:55.750 --> 00:42:59.200 And let me just stipulate that this is the Unicode code point. 00:42:59.200 --> 00:43:01.870 That is the number that Macs, PCs, and phones 00:43:01.870 --> 00:43:05.960 use underneath the hood to represent the default yellow "thumbs up." 00:43:05.960 --> 00:43:08.770 But if you want to give it a medium skin tone, 00:43:08.770 --> 00:43:12.370 you still use that same number, that same pattern of 0's and 1's, 00:43:12.370 --> 00:43:13.930 or switches, underneath the hood. 00:43:13.930 --> 00:43:17.620 But you use a few more switches that the computer or phone 00:43:17.620 --> 00:43:20.800 will interpret as, "Oh, you don't want to see the default in yellow 00:43:20.800 --> 00:43:23.890 because of this second number that's in the computer's memory somewhere. 00:43:23.890 --> 00:43:27.170 You want me to adjust it to be the medium skin tone or any 00:43:27.170 --> 00:43:29.010 of the other values, instead." 00:43:29.010 --> 00:43:31.580 So that's the engineering solution to this problem 00:43:31.580 --> 00:43:35.078 of just trying to represent different ranges of emoji here. 00:43:35.078 --> 00:43:36.620 Well, what about something like this? 00:43:36.620 --> 00:43:38.480 There's a lot more combinatorics, nowadays, 00:43:38.480 --> 00:43:40.522 on your keyboard for relationships, for instance. 00:43:40.522 --> 00:43:43.620 So here is a "couple with heart" here. 00:43:43.620 --> 00:43:46.520 So the couple, here, of course, is represented with, apparently, 00:43:46.520 --> 00:43:47.600 this number here. 00:43:47.600 --> 00:43:48.255 But that's it. 00:43:48.255 --> 00:43:50.630 But if you want to be more specific-- like man and woman, 00:43:50.630 --> 00:43:53.240 or man-man, woman-woman-- it's the same idea, 00:43:53.240 --> 00:43:56.630 but we just need to express ourselves a little-- with a little 00:43:56.630 --> 00:43:57.600 more information. 00:43:57.600 --> 00:44:00.560 So, for instance, the way the Unicode folks came up with, 00:44:00.560 --> 00:44:03.830 years ago, to represent, for instance, a woman with a heart and a man, 00:44:03.830 --> 00:44:06.480 from left to right, would be using these values. 00:44:06.480 --> 00:44:08.900 So things just escalated quickly, but only 00:44:08.900 --> 00:44:12.560 in the sense that we're using more bits, more 0's and 1's, to represent, 00:44:12.560 --> 00:44:15.470 more expressively, this particular combination. 00:44:15.470 --> 00:44:18.050 So this happens to be the number in Unicode 00:44:18.050 --> 00:44:19.790 that represents the woman at left. 00:44:19.790 --> 00:44:22.430 This is the number that represents the man at right. 00:44:22.430 --> 00:44:25.160 And this is the pair of numbers that represents 00:44:25.160 --> 00:44:29.550 the heart in the middle, sometimes red, sometimes pink, displayed here as pink. 00:44:29.550 --> 00:44:32.510 But if we want to change the combination, for instance, to be, 00:44:32.510 --> 00:44:33.920 say, woman-- 00:44:33.920 --> 00:44:36.320 if we want to change the combination to be woman-woman, 00:44:36.320 --> 00:44:39.200 notice that, now, the left and the rightmost numbers match. 00:44:39.200 --> 00:44:42.290 Or if we flip it back to man-man, it's just using different numbers 00:44:42.290 --> 00:44:43.790 on the tail end again. 00:44:43.790 --> 00:44:48.380 And meanwhile, if I rewind, there's these two identical values here. 00:44:48.380 --> 00:44:51.680 These are called zero-width joiners or ZWNJ characters. 00:44:51.680 --> 00:44:55.460 It just is a special number that humans reserve to say, 00:44:55.460 --> 00:45:00.210 glue the emoji at the left to the emoji on the right and so forth. 00:45:00.210 --> 00:45:02.370 So it connects ideas in this way. 00:45:02.370 --> 00:45:04.490 So there's actually a lot of emojis, nowadays, 00:45:04.490 --> 00:45:07.370 that are a combination of different things. 00:45:07.370 --> 00:45:09.650 "Heart on fire" is one that's, technically, 00:45:09.650 --> 00:45:13.190 the combination of a heart emoji, the fire emoji, 00:45:13.190 --> 00:45:16.110 joined together, numerically, in this way. 00:45:16.110 --> 00:45:18.290 So computer scientists who come up with these things 00:45:18.290 --> 00:45:21.290 are just reducing things to representations. 00:45:21.290 --> 00:45:23.430 All we have at our disposal are 0's and 1's. 00:45:23.430 --> 00:45:25.250 So we all just need to agree, ultimately-- 00:45:25.250 --> 00:45:28.760 whether we're Google, Microsoft, or the like-- how we're going to standardize 00:45:28.760 --> 00:45:31.030 these kinds of things as information. 00:45:31.030 --> 00:45:36.520 Questions, then, on how characters are represented in a computer, 00:45:36.520 --> 00:45:38.260 be it English or any other language? 00:45:38.260 --> 00:45:38.760 Yeah. 00:45:38.760 --> 00:45:40.630 AUDIENCE: How is the plus a number? 00:45:40.630 --> 00:45:41.963 DAVID J. MALAN: How is the what? 00:45:41.963 --> 00:45:43.930 AUDIENCE: The plus, the U+. 00:45:43.930 --> 00:45:46.600 DAVID J. MALAN: Oh, the U+ is just a convention, really. 00:45:46.600 --> 00:45:49.820 So U+ represents a special Unicode character, 00:45:49.820 --> 00:45:51.790 which is a U with a plus in the middle. 00:45:51.790 --> 00:45:54.590 And this is just the convention for saying, 00:45:54.590 --> 00:45:58.600 hey, everyone, here comes a number that represents a Unicode code point. 00:45:58.600 --> 00:46:00.070 The U and the 1 have no-- sorry. 00:46:00.070 --> 00:46:02.278 The U and the plus have no mathematical significance. 00:46:02.278 --> 00:46:05.470 It's just a visual clue to folks. 00:46:05.470 --> 00:46:10.460 Other questions on representing text in this way? 00:46:10.460 --> 00:46:10.960 All right. 00:46:10.960 --> 00:46:12.240 So what about colors? 00:46:12.240 --> 00:46:13.990 We've already started looking at pictures. 00:46:13.990 --> 00:46:17.032 Well, how are those pictures, be it emojis or anything else, represented? 00:46:17.032 --> 00:46:19.870 One of the most common ways is just with RGB-- 00:46:19.870 --> 00:46:20.950 red, green, and blue. 00:46:20.950 --> 00:46:24.700 It turns out that if we just keep track of how much red should 00:46:24.700 --> 00:46:27.880 be on the screen, and how much green, and how much blue, combined together, 00:46:27.880 --> 00:46:29.890 that gives us every color of the rainbow, 00:46:29.890 --> 00:46:32.120 from white to black and everything in between. 00:46:32.120 --> 00:46:35.620 So how do we represent an amount of red, and green, and blue? 00:46:35.620 --> 00:46:37.690 Well, frankly, just with three different numbers. 00:46:37.690 --> 00:46:40.030 And this is how computers typically represent colors. 00:46:40.030 --> 00:46:43.220 Every one of the dots on your computer screen or your phone screen 00:46:43.220 --> 00:46:44.390 is called a pixel. 00:46:44.390 --> 00:46:50.130 And every single dot underneath the hood has three numbers associated with it, 00:46:50.130 --> 00:46:53.000 so three numbers, three numbers, three numbers for every little dot. 00:46:53.000 --> 00:46:56.420 And those three numbers, together, say how much red, green, and blue 00:46:56.420 --> 00:46:59.040 should the device display at that location. 00:46:59.040 --> 00:47:02.390 So, for instance, if you had a dot on your screen 00:47:02.390 --> 00:47:07.520 that said, "use this much red, this much green this much blue," because each 00:47:07.520 --> 00:47:11.820 of these numbers, I'll tell you, are one byte or eight bits, 00:47:11.820 --> 00:47:15.710 which means the total possible values is 0 to 255-- 00:47:15.710 --> 00:47:19.070 let me just ballpark that the 72, it feels like a medium amount of red 00:47:19.070 --> 00:47:21.350 because it's in between 0 and 255. 00:47:21.350 --> 00:47:25.590 73 is a medium amount of green, and 33 of blue is just a little bit. 00:47:25.590 --> 00:47:29.480 So if you combine a medium amount of red, green, and a little bit of blue, 00:47:29.480 --> 00:47:32.840 anyone want to guess what color of the rainbow this is? 00:47:32.840 --> 00:47:33.507 AUDIENCE: Brown. 00:47:33.507 --> 00:47:34.423 DAVID J. MALAN: Sorry? 00:47:34.423 --> 00:47:35.190 AUDIENCE: Brown. 00:47:35.190 --> 00:47:36.470 DAVID J. MALAN: Brown? 00:47:36.470 --> 00:47:37.228 So, close. 00:47:37.228 --> 00:47:39.020 It's a little more yellow than it is brown. 00:47:39.020 --> 00:47:41.920 But if we combine them, it looks a little something like this. 00:47:41.920 --> 00:47:43.887 This is just CS trivia, not something that even 00:47:43.887 --> 00:47:46.970 I would be able to eyeball, unless I came up with that particular example. 00:47:46.970 --> 00:47:48.607 But wait a minute. 00:47:48.607 --> 00:47:49.940 We've seen these numbers before. 00:47:49.940 --> 00:47:51.768 72, 73, 33 represented what-- 00:47:51.768 --> 00:47:52.310 AUDIENCE: Hi! 00:47:52.310 --> 00:47:53.370 DAVID J. MALAN: --a few minutes ago? 00:47:53.370 --> 00:47:55.787 So it meant "Hi!" but here I am, claiming, no, no, no, no, 00:47:55.787 --> 00:47:57.140 that means yellow. 00:47:57.140 --> 00:47:58.890 How do you reconcile this? 00:47:58.890 --> 00:48:01.550 Well, at the end of the day, this is all we have, 00:48:01.550 --> 00:48:04.310 0's and 1's, whether you think of them as numbers, 00:48:04.310 --> 00:48:06.620 or letters, or even colors now. 00:48:06.620 --> 00:48:08.400 But it depends on the context. 00:48:08.400 --> 00:48:11.390 So if you've received a text message or an email, 00:48:11.390 --> 00:48:13.850 odds are the pattern of 0's and 1's that the computer 00:48:13.850 --> 00:48:17.300 is showing you are going to be interpreted as text because that's 00:48:17.300 --> 00:48:19.940 the whole point of a text message or an email. 00:48:19.940 --> 00:48:23.930 If, though, you opened up MacOS's or iOS's or Windows's or Android's 00:48:23.930 --> 00:48:27.230 calculator app, the same pattern of 0's and 1's might 00:48:27.230 --> 00:48:30.650 be interpreted as numbers for some addition, or subtraction, or whatever. 00:48:30.650 --> 00:48:33.780 If you open the same pattern of 0's and 1's in Photoshop, 00:48:33.780 --> 00:48:37.110 like a graphics program, they're going to be interpreted, in that context, 00:48:37.110 --> 00:48:37.950 as colors. 00:48:37.950 --> 00:48:39.098 So context matters. 00:48:39.098 --> 00:48:40.890 And, indeed, as soon as next week, when you 00:48:40.890 --> 00:48:43.080 start writing code in that language called C, 00:48:43.080 --> 00:48:47.550 the onus will be on you, the programmer, to tell the computer, interpret 00:48:47.550 --> 00:48:52.593 the following sequence of bits as a number, or a letter, or something else. 00:48:52.593 --> 00:48:55.260 And you won't have to even worry about what the 0's and 1's are, 00:48:55.260 --> 00:48:58.380 but you need to give the computer a hint as to what type of file 00:48:58.380 --> 00:49:00.730 or piece of data you're representing. 00:49:00.730 --> 00:49:01.890 So that gives us bits. 00:49:01.890 --> 00:49:05.640 And you can actually see these dots, these pixels on the screen. 00:49:05.640 --> 00:49:07.380 Let me zoom in, zoom in. 00:49:07.380 --> 00:49:10.470 And here we have it, just with this emoji, which, at the end of the day, 00:49:10.470 --> 00:49:13.380 is a picture that someone at Apple, in this case, drew. 00:49:13.380 --> 00:49:16.440 And you can see-- if you really zoom in, or take your phone or TV 00:49:16.440 --> 00:49:19.470 and really put it close to your face, you'll see all of these dots, 00:49:19.470 --> 00:49:20.700 depending on the hardware. 00:49:20.700 --> 00:49:23.250 And each of these dots, these squares, is 00:49:23.250 --> 00:49:28.350 storing 24 bits or three bytes, 24 bits, 24 bits, 24 bits. 00:49:28.350 --> 00:49:31.200 And that's whey, dot, dot, dot, if you've 00:49:31.200 --> 00:49:35.730 got a photograph, for instance, that's three megabytes, which 00:49:35.730 --> 00:49:42.030 is 3 million bytes, well, odds are there's 1 million pixels therein 00:49:42.030 --> 00:49:45.240 because you're using three bytes per pixel 00:49:45.240 --> 00:49:46.708 to represent each of those colors. 00:49:46.708 --> 00:49:48.750 That's a bit of an oversimplification, but that's 00:49:48.750 --> 00:49:51.840 why images and photos are getting bigger and bigger nowadays. 00:49:51.840 --> 00:49:56.110 Because we're throwing even more pixels into the file. 00:49:56.110 --> 00:49:56.610 Music-- 00:49:56.610 --> 00:49:57.330 [MUSIC PLAYING] 00:49:57.330 --> 00:49:59.880 --how could you represent music, digitally, 00:49:59.880 --> 00:50:03.920 using just 0's and 1's, or numbers, really? 00:50:03.920 --> 00:50:07.710 Any instinct, whether a musician or not? 00:50:07.710 --> 00:50:08.743 Yeah. 00:50:08.743 --> 00:50:11.060 AUDIENCE: The notes could be represented by a number. 00:50:11.060 --> 00:50:13.768 DAVID J. MALAN: Yeah, so we can just represent notes by a number. 00:50:13.768 --> 00:50:17.660 So A is some number, and B. And maybe sharp or flat is some other number. 00:50:17.660 --> 00:50:21.610 But note might not be quite enough for some-- 00:50:21.610 --> 00:50:22.110 yeah? 00:50:22.110 --> 00:50:22.876 AUDIENCE: [INAUDIBLE]. 00:50:22.876 --> 00:50:23.834 DAVID J. MALAN: Ah, OK. 00:50:23.834 --> 00:50:26.470 So one note-- one number to represent the note, itself, 00:50:26.470 --> 00:50:30.820 the sound or the pitch; one other number to represent the duration. 00:50:30.820 --> 00:50:35.000 In the context of piano, how long is the human holding the key down? 00:50:35.000 --> 00:50:37.330 And maybe I can think of a third, the loudness. 00:50:37.330 --> 00:50:39.457 How hard has the person played that note? 00:50:39.457 --> 00:50:41.290 So, minimally, with three numbers, you could 00:50:41.290 --> 00:50:43.492 imagine representing music, as well. 00:50:43.492 --> 00:50:45.700 And, indeed, that's very well might be what computers 00:50:45.700 --> 00:50:47.720 are doing when you listen to sound. 00:50:47.720 --> 00:50:48.610 What about video? 00:50:48.610 --> 00:50:51.760 How could you represent videos, as well? 00:50:51.760 --> 00:50:52.270 Yeah? 00:50:52.270 --> 00:50:53.460 AUDIENCE: Through many images. 00:50:53.460 --> 00:50:54.790 DAVID J. MALAN: Yeah, many images. 00:50:54.790 --> 00:50:57.750 So if you've ever produced a film or looked at some of the fine print, 00:50:57.750 --> 00:51:02.070 30 frames per second, FPS, or 29 frames per second 00:51:02.070 --> 00:51:04.560 is just how many pictures are flying across the screen. 00:51:04.560 --> 00:51:06.810 But that's really all a video file is on a computer, 00:51:06.810 --> 00:51:10.470 lots of pictures moving so quickly in front of us that you and I, our brains, 00:51:10.470 --> 00:51:12.930 interpolate that as being actual motion. 00:51:12.930 --> 00:51:15.720 And, in fact, from yesteryear, motion pictures, 00:51:15.720 --> 00:51:18.570 it's like pictures that are giving the illusion of motion, 00:51:18.570 --> 00:51:21.910 even though there's only 30 or so of them flying across the screen. 00:51:21.910 --> 00:51:24.870 So we have a way, now, to represent information, both as 00:51:24.870 --> 00:51:29.250 input and output, whether it's numbers, letters, images, anything else. 00:51:29.250 --> 00:51:32.200 Let's now focus on what's inside of that black box, 00:51:32.200 --> 00:51:36.360 so to speak, wherein we have algorithms, step-by-step instructions 00:51:36.360 --> 00:51:38.430 for solving some problem. 00:51:38.430 --> 00:51:41.760 Now, what do I mean by "algorithms" or "step-by-step instructions"? 00:51:41.760 --> 00:51:44.815 Well, maybe, if we were to turn this into code-- 00:51:44.815 --> 00:51:47.190 and that's how we'll connect the dots, ultimately, today. 00:51:47.190 --> 00:51:51.480 Code is just the implementation, in computers, of algorithms. 00:51:51.480 --> 00:51:53.920 An algorithm can be something we do in the physical world. 00:51:53.920 --> 00:51:58.180 Code is how we implement that exact same idea, in the context of a computer, 00:51:58.180 --> 00:51:58.810 instead. 00:51:58.810 --> 00:52:02.152 And here, for instance, is a very common application inside of a computer, 00:52:02.152 --> 00:52:02.860 for your context. 00:52:02.860 --> 00:52:04.900 This is the iOS version of the icon. 00:52:04.900 --> 00:52:06.963 And, typically, if you click on that icon, 00:52:06.963 --> 00:52:09.130 you'll see something like all of your contacts here, 00:52:09.130 --> 00:52:12.250 typically, alphabetical, by first name or last name. 00:52:12.250 --> 00:52:17.103 And your phone or your computer lets you often search for someone's name 00:52:17.103 --> 00:52:17.770 at the very top. 00:52:17.770 --> 00:52:20.620 And it will autocomplete, and it'll be pretty darn fast. 00:52:20.620 --> 00:52:23.470 But it'll be pretty darn fast because the programmers who 00:52:23.470 --> 00:52:28.210 implemented that application are looking for someone quickly for you. 00:52:28.210 --> 00:52:33.400 Now, I can do this old school style, whereby we have one of these things 00:52:33.400 --> 00:52:35.840 from yesteryear, an actual phone book. 00:52:35.840 --> 00:52:39.160 So, in a physical phone book like this, you might have 1,000 pages. 00:52:39.160 --> 00:52:42.440 And on every page are a bunch of names and a bunch of numbers. 00:52:42.440 --> 00:52:45.490 And as I flip through this, I could look for someone specific. 00:52:45.490 --> 00:52:49.010 So suppose I want to call John Harvard, who's-- the first name, of course, 00:52:49.010 --> 00:52:52.720 starts with a J. Well, I could just turn, page by page, 00:52:52.720 --> 00:52:54.590 looking for John Harvard. 00:52:54.590 --> 00:52:57.000 And if he's not there, I keep turning and turning. 00:52:57.000 --> 00:52:58.000 So this is an algorithm. 00:52:58.000 --> 00:53:00.790 I'm stepping through the phone book, one page at a time. 00:53:00.790 --> 00:53:04.510 Is it correct, this algorithm, assuming I'm looking down? 00:53:04.510 --> 00:53:05.470 So, yeah. 00:53:05.470 --> 00:53:07.930 I mean, it's stupidly slow because why am 00:53:07.930 --> 00:53:10.585 I wasting my time with the A's, and the B's, and the so forth? 00:53:10.585 --> 00:53:12.460 I could probably take bigger bites out of it. 00:53:12.460 --> 00:53:13.210 But it is correct. 00:53:13.210 --> 00:53:16.293 And that's going to be one of the goals of writing code, is to, of course, 00:53:16.293 --> 00:53:18.100 solve the problem you care about correctly. 00:53:18.100 --> 00:53:20.470 So correctness goes without saying, or else what's 00:53:20.470 --> 00:53:23.080 the point of writing the code or solving-- or implementing 00:53:23.080 --> 00:53:23.800 the algorithm? 00:53:23.800 --> 00:53:24.040 All right. 00:53:24.040 --> 00:53:25.623 Well, let me at least speed things up. 00:53:25.623 --> 00:53:29.380 So, instead of one page at a time, so, two, four, six, eight-- 00:53:29.380 --> 00:53:33.420 no, ignore that-- 10, 12, 14, 16, and so forth. 00:53:33.420 --> 00:53:35.920 It's a little hard to do, physically, but it sounded faster. 00:53:35.920 --> 00:53:38.815 It was twice as fast, theoretically, but is it correct? 00:53:38.815 --> 00:53:39.505 AUDIENCE: No. 00:53:39.505 --> 00:53:40.480 DAVID J. MALAN: So, no. 00:53:40.480 --> 00:53:41.210 Why? 00:53:41.210 --> 00:53:41.710 Yeah? 00:53:41.710 --> 00:53:43.180 AUDIENCE: Yeah, you might [INAUDIBLE]. 00:53:43.180 --> 00:53:46.263 DAVID J. MALAN: Yeah, I might miss John Harvard because, just by accident, 00:53:46.263 --> 00:53:48.490 he might get sandwiched between two pages. 00:53:48.490 --> 00:53:52.210 But do I have to throw the algorithm out altogether? 00:53:52.210 --> 00:53:53.260 Probably not. 00:53:53.260 --> 00:53:56.073 Once I reach the K section, which is past the J section, 00:53:56.073 --> 00:53:58.990 I could double back at least one page, at least, [PATS TELEPHONE BOOK] 00:53:58.990 --> 00:54:01.540 and just make sure I didn't blow past him completely. 00:54:01.540 --> 00:54:04.450 So that is twice as fast, because I'm going two pages 00:54:04.450 --> 00:54:05.922 at a time, plus one extra step. 00:54:05.922 --> 00:54:07.130 So it's still an improvement. 00:54:07.130 --> 00:54:08.922 So the first algorithm, worst case, if it's 00:54:08.922 --> 00:54:10.900 not John, but someone whose name starts with Z, 00:54:10.900 --> 00:54:12.860 that might take me a full 1,000 steps. 00:54:12.860 --> 00:54:16.210 The second algorithm is just 500 steps because I'm 00:54:16.210 --> 00:54:19.660 going two pages at a time plus one, in case I have to double back, 00:54:19.660 --> 00:54:20.990 but that's in the worst case. 00:54:20.990 --> 00:54:26.080 But most of us in the-- in yesteryear, and what Apple, and Google, and others 00:54:26.080 --> 00:54:29.440 are actually doing is, in software or here, physically, 00:54:29.440 --> 00:54:31.540 we're typically going, roughly, to the middle. 00:54:31.540 --> 00:54:34.360 Especially if there's no cheat sheet on the side, like A through Z, 00:54:34.360 --> 00:54:36.235 I'm just going to go to, roughly, the middle. 00:54:36.235 --> 00:54:38.920 And, oh, here I am, not surprisingly, in the M section. 00:54:38.920 --> 00:54:40.360 But what do I now know. 00:54:40.360 --> 00:54:43.660 If this is the M section, where is John Harvard? 00:54:43.660 --> 00:54:46.130 So, clearly, to the left, alphabetically. 00:54:46.130 --> 00:54:49.660 And so here is where we can take a much bigger bite out of the problem. 00:54:49.660 --> 00:54:52.480 We can really divide and conquer this problem 00:54:52.480 --> 00:54:56.080 by tearing [TEARS BOOK] the problem in half, throwing half of it 00:54:56.080 --> 00:55:01.240 away, 500 pages away, leaving me with a smaller problem, 00:55:01.240 --> 00:55:04.370 half as big, that I can really just now repeat. 00:55:04.370 --> 00:55:07.150 So I go, roughly, here, and now I'm in the E section. 00:55:07.150 --> 00:55:09.102 So I went a little too far back. 00:55:09.102 --> 00:55:10.060 But what do I now know? 00:55:10.060 --> 00:55:11.680 If this is the E pages, where's John? 00:55:11.680 --> 00:55:12.460 AUDIENCE: To the right. 00:55:12.460 --> 00:55:13.690 DAVID J. MALAN: So now he's to the right. 00:55:13.690 --> 00:55:15.815 So I can-- again, hopefully, he's not on that page. 00:55:15.815 --> 00:55:20.380 I can tear the problem in half again, throw that 250 pages away. 00:55:20.380 --> 00:55:23.530 And now I've gone from 1,000 to 500 to 250 pages. 00:55:23.530 --> 00:55:26.470 Now I'm moving because the first algorithm was one page at a time, 00:55:26.470 --> 00:55:27.280 second was two. 00:55:27.280 --> 00:55:29.373 This is hundreds of pages at a time. 00:55:29.373 --> 00:55:31.540 And if I go, roughly, again, to the middle; roughly, 00:55:31.540 --> 00:55:33.790 to the middle; roughly, to the middle, hopefully, I'll 00:55:33.790 --> 00:55:36.350 find John Harvard on one final page. 00:55:36.350 --> 00:55:39.140 Can only do this once, but one final page. 00:55:39.140 --> 00:55:42.010 So that invites the question, I would think, 00:55:42.010 --> 00:55:44.680 if the phone book does have 1,000 or so pages, 00:55:44.680 --> 00:55:50.210 how many times can I divide the problem in half to get down to one last page? 00:55:50.210 --> 00:55:51.580 So it's, roughly, 10. 00:55:51.580 --> 00:55:56.203 And the quick math is 1,000 goes to 500 to 250 to 125 to 67 something. 00:55:56.203 --> 00:55:58.370 So we have to deal with rounding issues, eventually. 00:55:58.370 --> 00:56:01.640 But assuming we work out the math, it's, roughly, 10 page tears. 00:56:01.640 --> 00:56:07.580 And that's crazy faster than 1,000 pages and still faster than 500 pages. 00:56:07.580 --> 00:56:09.350 So it's fundamentally better. 00:56:09.350 --> 00:56:13.792 And, indeed, if I finally get to that final page, in the software world, 00:56:13.792 --> 00:56:16.250 you'd see something like this, John Harvard and his number, 00:56:16.250 --> 00:56:17.810 which you're welcome to call or text. 00:56:17.810 --> 00:56:22.850 But that's how we now have our answer, much like the single page there. 00:56:22.850 --> 00:56:27.090 But let's consider just how efficient that actually is. 00:56:27.090 --> 00:56:30.560 So here's a very rough, broad-- with broad strokes, a chart. 00:56:30.560 --> 00:56:31.998 So here's an xy plot. 00:56:31.998 --> 00:56:34.790 So here, on the horizontal, is going to be the size of the problem. 00:56:34.790 --> 00:56:38.360 And, by that, I mean, how many pages are we trying to go through? 00:56:38.360 --> 00:56:39.890 This would be zero pages. 00:56:39.890 --> 00:56:41.480 This would be a lot of pages. 00:56:41.480 --> 00:56:43.610 How much time does it take to solve the problem? 00:56:43.610 --> 00:56:46.100 How long does it take to find John Harvard or anyone else? 00:56:46.100 --> 00:56:47.540 This means no time. 00:56:47.540 --> 00:56:48.960 This means a lot of time. 00:56:48.960 --> 00:56:51.110 So what's the relationship among these algorithms? 00:56:51.110 --> 00:56:53.490 Well, the first one is actually just a straight line. 00:56:53.490 --> 00:56:57.330 If there's n pages in the phone book, well, I 00:56:57.330 --> 00:57:00.060 claim that it's a one-to-one relationship. 00:57:00.060 --> 00:57:04.290 Because if Verizon or the phone company adds another page next year, 00:57:04.290 --> 00:57:06.900 that just means I might have one more step next year, 00:57:06.900 --> 00:57:09.990 as well, to find John Harvard or anyone else. 00:57:09.990 --> 00:57:12.930 But the second algorithm, it's also a straight line, 00:57:12.930 --> 00:57:15.295 but it's lower, even though it might not look obvious. 00:57:15.295 --> 00:57:16.420 And what do I mean by that? 00:57:16.420 --> 00:57:18.253 Well, let me just draw some temporary lines. 00:57:18.253 --> 00:57:20.610 If this is how many pages are in the phone book-- dot, 00:57:20.610 --> 00:57:24.090 dot, dot-- notice that it takes this much time, on the red line, 00:57:24.090 --> 00:57:25.360 to solve the problem. 00:57:25.360 --> 00:57:28.530 But if I, instead, use the second algorithm, 00:57:28.530 --> 00:57:30.507 it takes me half as much time. 00:57:30.507 --> 00:57:32.340 So, even though they're both straight lines, 00:57:32.340 --> 00:57:34.923 one is strictly lower than the other, which means it's faster. 00:57:34.923 --> 00:57:35.640 It's better. 00:57:35.640 --> 00:57:38.310 But the third algorithm is a fundamentally different shape, 00:57:38.310 --> 00:57:40.090 and it looks a little something like this. 00:57:40.090 --> 00:57:42.370 And it looks like it's going to flatten, flatten, flatten out, 00:57:42.370 --> 00:57:43.260 but it never does. 00:57:43.260 --> 00:57:46.170 It just rises ever so slowly. 00:57:46.170 --> 00:57:49.290 And what this means is that if maybe Cambridge and Allston, here 00:57:49.290 --> 00:57:53.030 in Massachusetts, merge next year, so we go from 1,000 page phone book 00:57:53.030 --> 00:57:58.100 to a 2,000 page phone book, that means, if we're here this year, 00:57:58.100 --> 00:57:59.660 we're over here next year. 00:57:59.660 --> 00:58:01.190 It's not all that much higher. 00:58:01.190 --> 00:58:04.320 But it would be much higher if we were using the first two algorithms. 00:58:04.320 --> 00:58:04.820 Why? 00:58:04.820 --> 00:58:07.880 It'd be an extra 1,000 steps to find someone in that combined phone 00:58:07.880 --> 00:58:09.890 book or maybe another 500 steps. 00:58:09.890 --> 00:58:13.520 But, to be clear, if we're using my third and final algorithm, 00:58:13.520 --> 00:58:15.620 how many more steps will it take me next year, 00:58:15.620 --> 00:58:20.930 when Cambridge and Allston merge into one 2,000-page phone book? 00:58:20.930 --> 00:58:22.800 Just one more step, no big deal. 00:58:22.800 --> 00:58:24.400 It's one more page tear. 00:58:24.400 --> 00:58:26.900 And this is what we mean, ultimately, about not just writing 00:58:26.900 --> 00:58:29.720 code or implementing algorithms that are correct, but, now, 00:58:29.720 --> 00:58:33.070 that are well-designed or efficient, specifically. 00:58:33.070 --> 00:58:35.570 And this is what makes someone a better programmer, perhaps, 00:58:35.570 --> 00:58:38.237 than someone else, or a better problem-solver than someone else, 00:58:38.237 --> 00:58:40.130 is acquiring, over time, these skills. 00:58:40.130 --> 00:58:43.280 So that, sure, you could solve the problem quickly and dirtily, 00:58:43.280 --> 00:58:45.560 so to speak, but if you're going to have a lot of data 00:58:45.560 --> 00:58:49.492 eventually, be it in your phone book or maybe your Google website index, 00:58:49.492 --> 00:58:51.200 if you're searching the web, you're going 00:58:51.200 --> 00:58:54.540 to want to try to think about how to design algorithms that look like this. 00:58:54.540 --> 00:58:56.370 Mathematically, this is called a logarithm. 00:58:56.370 --> 00:58:59.495 And it's log base 2 because I'm halving, halving, halving, again and again. 00:58:59.495 --> 00:59:03.120 But, for now, just know that it's a fundamentally faster and different 00:59:03.120 --> 00:59:03.820 shape. 00:59:03.820 --> 00:59:06.330 So, among our goals in CS50, ultimately, is not just 00:59:06.330 --> 00:59:12.270 to write and solve problems correctly, but, ultimately, ever more efficiently, 00:59:12.270 --> 00:59:13.600 as well. 00:59:13.600 --> 00:59:20.110 Any questions, then, on these ideas of efficiency and design? 00:59:20.110 --> 00:59:21.480 Any questions here? 00:59:21.480 --> 00:59:22.080 Yeah, in back? 00:59:22.080 --> 00:59:23.480 AUDIENCE: [INAUDIBLE]. 00:59:23.480 --> 00:59:24.170 DAVID J. MALAN: A good question. 00:59:24.170 --> 00:59:27.462 Just to repeat it, can a graph like this capture the accuracy of the algorithm? 00:59:27.462 --> 00:59:28.640 Short answer, no. 00:59:28.640 --> 00:59:31.460 For instance, if I drew a straight line that 00:59:31.460 --> 00:59:35.570 is super low on this graph, which would imply that the algorithm takes 00:59:35.570 --> 00:59:38.240 the same amount of time, no matter how many pages there are, 00:59:38.240 --> 00:59:41.480 my algorithm might actually be to just pull a random page from the phone 00:59:41.480 --> 00:59:43.230 book, one step, and say, here it is. 00:59:43.230 --> 00:59:45.230 But that's not necessarily going to be accurate, 00:59:45.230 --> 00:59:47.010 unless I get really, really lucky. 00:59:47.010 --> 00:59:49.820 So the graph really only speaks to efficiency 00:59:49.820 --> 00:59:52.880 and the design of the algorithm, not the correctness of it. 00:59:52.880 --> 00:59:57.170 To analyze the correctness, you need to use another mechanism altogether, 00:59:57.170 --> 00:59:58.280 namely, logic. 00:59:58.280 --> 01:00:02.120 Other questions now, on efficiency, in this way? 01:00:02.120 --> 01:00:02.940 No? 01:00:02.940 --> 01:00:03.440 All right. 01:00:03.440 --> 01:00:08.090 So, with that in mind, let's begin to translate this to some actual code. 01:00:08.090 --> 01:00:11.630 And, in fact, before we look at, today, one actual programming language, 01:00:11.630 --> 01:00:14.720 albeit a graphical one, let's consider something called pseudocode. 01:00:14.720 --> 01:00:16.497 So pseudocode has no formal meaning. 01:00:16.497 --> 01:00:19.580 Generally, you write it in English or whatever your own human language is. 01:00:19.580 --> 01:00:25.600 But you write your thoughts down tersely, succinct, but precisely. 01:00:25.600 --> 01:00:28.570 You try to really convey your thoughts, not with a wave of the hand, 01:00:28.570 --> 01:00:31.510 metaphorically, but step by step, precisely. 01:00:31.510 --> 01:00:32.650 So what do I mean by this? 01:00:32.650 --> 01:00:35.380 Here might be some representative pseudocode 01:00:35.380 --> 01:00:39.337 via which I describe that third and final algorithm in a way 01:00:39.337 --> 01:00:41.920 that I could hand it to you and you could do the same at home. 01:00:41.920 --> 01:00:45.425 Or I could hand it to someone at Google, and they could implement it in Android. 01:00:45.425 --> 01:00:48.550 Or I could hand it to someone at Apple, and they could implement it in iOS. 01:00:48.550 --> 01:00:51.280 So, step one, I claimed, was "Pick up phone book." 01:00:51.280 --> 01:00:54.430 Step two was "Open to the middle of the phone book." 01:00:54.430 --> 01:00:57.145 Step three, "Look at the page," as I did. 01:00:57.145 --> 01:00:59.020 And now things get a little more interesting. 01:00:59.020 --> 01:01:03.760 Step four, "If person is on page," I have to make a decision. 01:01:03.760 --> 01:01:07.810 Presumably, what should I do if John Harvard is on the page I'm looking at? 01:01:07.810 --> 01:01:12.050 So stop and probably make the call, or email, or whatever the goal might be. 01:01:12.050 --> 01:01:15.430 And so I'm going to actually indent this, visually, by a few spaces, 01:01:15.430 --> 01:01:19.900 just to make clear that you should only do line five if the answer to line four 01:01:19.900 --> 01:01:21.130 is yes. 01:01:21.130 --> 01:01:22.360 Otherwise, you don't bother. 01:01:22.360 --> 01:01:26.640 The next thing I'm going to do, line six, is consider another possibility. 01:01:26.640 --> 01:01:28.980 "If the person I'm looking for is earlier 01:01:28.980 --> 01:01:30.805 in the book," what do I want to do? 01:01:30.805 --> 01:01:31.680 Well, I could write-- 01:01:31.680 --> 01:01:33.388 I could describe this in a bunch of ways. 01:01:33.388 --> 01:01:35.190 I'm going to do this tersely, as follows. 01:01:35.190 --> 01:01:38.910 "Open to the middle of the left half of the book, 01:01:38.910 --> 01:01:41.610 so it's open to the middle of the left half of the book. 01:01:41.610 --> 01:01:43.320 And then, what am I going to do? 01:01:43.320 --> 01:01:46.630 Well, I've just divided the problem into something smaller. 01:01:46.630 --> 01:01:48.580 But it's fundamentally the same problem. 01:01:48.580 --> 01:01:51.100 It's just a fewer number of pages. 01:01:51.100 --> 01:01:54.017 So I'm just going to go back to line three and do it again 01:01:54.017 --> 01:01:56.850 because the problem is just getting smaller and smaller, presumably. 01:01:56.850 --> 01:01:59.520 Else, if the person I'm looking for is later in the book, 01:01:59.520 --> 01:02:02.760 open to the middle of the right half of the book, 01:02:02.760 --> 01:02:04.950 and, also, "Go back to line 3." 01:02:04.950 --> 01:02:08.160 But there's a fourth possibility and its failure 01:02:08.160 --> 01:02:11.580 to realize, sometimes, that there's other possible outcomes that 01:02:11.580 --> 01:02:14.400 make computers crash, or spinning beach balls, or the like, 01:02:14.400 --> 01:02:17.130 if a programmer doesn't anticipate some situation. 01:02:17.130 --> 01:02:20.538 What's the fourth possible situation, when looking for John Harvard? 01:02:20.538 --> 01:02:22.080 AUDIENCE: If they're not in the book. 01:02:22.080 --> 01:02:24.205 DAVID J. MALAN: If they're not in the book, at all. 01:02:24.205 --> 01:02:27.450 And, indeed, I might get to the very last page and John Harvard's not even 01:02:27.450 --> 01:02:28.200 on that one. 01:02:28.200 --> 01:02:33.450 I'd better handle that and say, else, as a catchall, just quit altogether. 01:02:33.450 --> 01:02:35.610 And, truly, often, in your Macs, PCs, phones, 01:02:35.610 --> 01:02:38.310 when they freeze, or, again, spinning beach ball, or the like, 01:02:38.310 --> 01:02:39.660 and just weird things happen, that's just 01:02:39.660 --> 01:02:41.220 because some human made a dumb mistake. 01:02:41.220 --> 01:02:44.430 And they didn't realize that you could somehow get your phone or your laptop 01:02:44.430 --> 01:02:47.070 into a configuration that they didn't anticipate. 01:02:47.070 --> 01:02:48.870 So we're going to try to handle that here. 01:02:48.870 --> 01:02:50.880 Now this is just one way of writing pseudocode. 01:02:50.880 --> 01:02:52.230 There's no one way to do this. 01:02:52.230 --> 01:02:55.500 All of us in this room could come up with slightly different pseudocode. 01:02:55.500 --> 01:02:59.190 But I think you'll find characteristic are certain building 01:02:59.190 --> 01:03:02.130 blocks in all of our answers. 01:03:02.130 --> 01:03:04.590 Here, in yellow, are what, as of today, we're 01:03:04.590 --> 01:03:07.540 going to start calling "functions," technically speaking. 01:03:07.540 --> 01:03:10.800 These are like actions or verbs that literally, in this case, tell me 01:03:10.800 --> 01:03:11.700 what to do. 01:03:11.700 --> 01:03:15.150 Next, we're going to have these things, conditionals, 01:03:15.150 --> 01:03:20.525 forks in the road, so to speak, that take me down this path or another, 01:03:20.525 --> 01:03:21.150 metaphorically. 01:03:21.150 --> 01:03:22.933 So I do this thing or something else. 01:03:22.933 --> 01:03:25.350 But how do I decide if I want to go this way, or this way, 01:03:25.350 --> 01:03:26.620 or this way, or this way? 01:03:26.620 --> 01:03:27.780 I need to ask a question. 01:03:27.780 --> 01:03:30.000 And in programming, we'll typically use what 01:03:30.000 --> 01:03:33.240 are called Boolean expressions, named after a mathematician, Boole. 01:03:33.240 --> 01:03:36.090 And a Boolean expression is essentially just a question 01:03:36.090 --> 01:03:42.300 with a yes/no answer, a true or false answer, a 1 or 0 answer. 01:03:42.300 --> 01:03:45.520 It doesn't matter how you think about it, but it's got two possible answers. 01:03:45.520 --> 01:03:49.228 And so you can think of these as being expressions with question marks, 01:03:49.228 --> 01:03:50.520 even though I didn't draw such. 01:03:50.520 --> 01:03:54.270 Person on page, person earlier in book, person later in book, 01:03:54.270 --> 01:03:55.590 those are Boolean expressions. 01:03:55.590 --> 01:04:00.400 And they tell me whether I should go down one fork in the road or another. 01:04:00.400 --> 01:04:03.600 And, lastly, there's this, "Go back to line 3," in two different places. 01:04:03.600 --> 01:04:06.630 That represents what we call a "loop," some kind of cycle that's 01:04:06.630 --> 01:04:08.370 doing something again and again. 01:04:08.370 --> 01:04:10.733 Now these are just a few building blocks here-- 01:04:10.733 --> 01:04:12.900 functions, conditionals, Boolean expressions, loops. 01:04:12.900 --> 01:04:15.210 But you'll find that they're characteristic of so 01:04:15.210 --> 01:04:18.380 many different languages, one of which we'll look at today, another of which 01:04:18.380 --> 01:04:19.380 we'll look at next week. 01:04:19.380 --> 01:04:23.130 And those include, indeed, C, and Python, and other languages still. 01:04:23.130 --> 01:04:25.350 And so this is why we focus on these basics 01:04:25.350 --> 01:04:27.480 and fundamentals in these early days because we're 01:04:27.480 --> 01:04:29.080 going to see them again and again. 01:04:29.080 --> 01:04:31.800 So even if you feel like that fire hose is hitting you sometime, 01:04:31.800 --> 01:04:34.320 we'll give you, today, ultimately, more visuals 01:04:34.320 --> 01:04:38.730 by which you can hang onto, so as to actually write code, ultimately, 01:04:38.730 --> 01:04:41.490 in different languages and solve all sorts of problems. 01:04:41.490 --> 01:04:46.050 Now, we'd be remiss in not bringing up what's behind characters like Spot, 01:04:46.050 --> 01:04:48.570 and ChatGPT, and other software, artificial intelligence. 01:04:48.570 --> 01:04:51.430 And it turns out, to get to the point of AI, 01:04:51.430 --> 01:04:54.150 we're actually going to need more building blocks than just 01:04:54.150 --> 01:04:56.125 functions, and loops, and conditionals. 01:04:56.125 --> 01:04:57.750 It's not going to be quite that simple. 01:04:57.750 --> 01:04:59.200 But this has been a lot, so far. 01:04:59.200 --> 01:05:01.530 Let's go ahead, here, and take a five-minute break. 01:05:01.530 --> 01:05:03.990 And when we resume, we'll take a look not only at AI, 01:05:03.990 --> 01:05:06.630 but also a specific language called Scratch. 01:05:06.630 --> 01:05:09.870 So no cake, just yet, but we'll come back in five. 01:05:09.870 --> 01:05:14.820 Before we dive back in, I just wanted to call out a special guest 01:05:14.820 --> 01:05:16.980 that we have here today, beyond Spot, someone 01:05:16.980 --> 01:05:19.050 who's come from even farther away. 01:05:19.050 --> 01:05:22.560 And, in fact, if any of you have taken CS50x, the OpenCourseWare version 01:05:22.560 --> 01:05:25.270 of the class, or dabbled in it over the past few years 01:05:25.270 --> 01:05:27.490 in some of CS50's online social communities, 01:05:27.490 --> 01:05:29.710 you might have had your questions answered 01:05:29.710 --> 01:05:31.810 by a certain human from New Zealand. 01:05:31.810 --> 01:05:35.890 And she's come all this way, today, to spend this lecture with us. 01:05:35.890 --> 01:05:38.350 This is CS50's own Brenda Anderson. 01:05:38.350 --> 01:05:41.920 If you might come up for acknowledgment from all of us here. 01:05:41.920 --> 01:05:45.052 [APPLAUSE] 01:05:47.440 --> 01:05:50.170 It's not much, but just a little token of our thanks. 01:05:50.170 --> 01:05:53.830 Brenda has helped, truly, thousands of students online 01:05:53.830 --> 01:05:55.370 for the past many years. 01:05:55.370 --> 01:05:58.780 And, in fact, her own daughter has been the artist 01:05:58.780 --> 01:06:03.615 behind the duck that's about to loom large in CS50 this year and beyond. 01:06:03.615 --> 01:06:04.615 So, thank you to Brenda. 01:06:04.615 --> 01:06:07.936 [APPLAUSE] 01:06:11.410 --> 01:06:12.400 All right. 01:06:12.400 --> 01:06:16.690 So it's hard to escape a discussion of artificial intelligence, nowadays, 01:06:16.690 --> 01:06:18.790 but we thought we'd use this as an opportunity 01:06:18.790 --> 01:06:20.260 to connect some of these dots. 01:06:20.260 --> 01:06:22.460 Because, indeed, over the course of the semester, 01:06:22.460 --> 01:06:25.990 we'll be-- not only be talking about artificial intelligence or AI, 01:06:25.990 --> 01:06:28.900 but really using it all the more constructively 01:06:28.900 --> 01:06:31.570 to help you solve problems, help you get unblocked 01:06:31.570 --> 01:06:35.680 when you hit a wall, cognitively or syntactically, when writing code. 01:06:35.680 --> 01:06:39.430 And, indeed, it's no accident that we have this duck here, 01:06:39.430 --> 01:06:42.370 looming large, which is really the embodiment of the sort of AI 01:06:42.370 --> 01:06:45.200 that you'll experience within CS50, itself, this year. 01:06:45.200 --> 01:06:48.220 So let's talk about the so-called chatbots that 01:06:48.220 --> 01:06:50.500 inspired some of those headlines with which we began 01:06:50.500 --> 01:06:52.780 class, that weren't quite on the nose. 01:06:52.780 --> 01:06:54.880 So the class will still be taught by us humans, 01:06:54.880 --> 01:06:59.170 but helped by this CS50 duck, a chatbot of sorts. 01:06:59.170 --> 01:07:00.590 Now what do I mean by this? 01:07:00.590 --> 01:07:04.090 Well, it turns out that, when it comes to implementing something 01:07:04.090 --> 01:07:06.730 like an artificial intelligence, we don't quite 01:07:06.730 --> 01:07:10.550 have all of the building blocks yet, certainly after just today's week zero, 01:07:10.550 --> 01:07:12.000 to implement something like that. 01:07:12.000 --> 01:07:14.960 But I think we can skate in that direction, intellectually. 01:07:14.960 --> 01:07:19.670 So, for instance, if we were to take a stab at implementing our own chatbot-- 01:07:19.670 --> 01:07:22.883 some interactive text-based program that talks to us, 01:07:22.883 --> 01:07:24.800 and answers questions, and the like-- we could 01:07:24.800 --> 01:07:28.850 try borrowing some of today's ideas already, those functions, conditionals, 01:07:28.850 --> 01:07:30.007 loops, and more. 01:07:30.007 --> 01:07:31.590 And I could write something like this. 01:07:31.590 --> 01:07:34.370 If I am writing code or pseudocode for a chatbot, 01:07:34.370 --> 01:07:37.170 I could program the chatbot to do something like this. 01:07:37.170 --> 01:07:39.230 If the student says, hello, to the chatbot, 01:07:39.230 --> 01:07:41.840 then the chatbot should say, hello, back. 01:07:41.840 --> 01:07:44.900 Else, if the student says, goodbye, well, the chatbot 01:07:44.900 --> 01:07:46.580 should say, goodbye, back. 01:07:46.580 --> 01:07:51.800 Else, if the student asks how you are, the chat bot should say that it's well. 01:07:51.800 --> 01:07:54.020 But things get a little harder when you start 01:07:54.020 --> 01:07:57.470 asking more interesting questions, like, else, 01:07:57.470 --> 01:08:00.990 if the student asks why 111 in binary is 7 in decimal. 01:08:00.990 --> 01:08:03.980 Now, you could imagine that we just have a conditional, 01:08:03.980 --> 01:08:08.570 with this Boolean expression, that programs the chatbot to just give you 01:08:08.570 --> 01:08:12.230 the answer and explain, in an English sentence, why that, in fact, is. 01:08:12.230 --> 01:08:20.750 But what if you, the student, asks why 110 is 6 in decimal or why 010 is 2? 01:08:20.750 --> 01:08:24.229 I mean, you can just imagine the explosion in the amount of code 01:08:24.229 --> 01:08:28.370 that we would have to write to just anticipate every darn question that you 01:08:28.370 --> 01:08:31.375 might ask about today and every other class, not to mention all 01:08:31.375 --> 01:08:32.750 of the knowledge in the universe. 01:08:32.750 --> 01:08:36.649 So, surely, there are other ways to implement algorithms 01:08:36.649 --> 01:08:39.319 that allow something like a chatbot or AI, 01:08:39.319 --> 01:08:43.700 more generally, to be fed input, still, like all of the internet, all 01:08:43.700 --> 01:08:46.760 of the worldwide web, all of the pages and textual content therein, 01:08:46.760 --> 01:08:51.050 but to let it just figure out how to answer our questions based 01:08:51.050 --> 01:08:54.330 on those kinds of inputs, assuming the inputs, themselves, are accurate. 01:08:54.330 --> 01:08:56.510 So "large language models" is a term you might 01:08:56.510 --> 01:08:59.720 have heard bandied about over the past several months, or LLMs. 01:08:59.720 --> 01:09:04.729 And a large language model really is an implementation, in software, 01:09:04.729 --> 01:09:09.630 of code that actually takes, as input, lots and lots of language-- 01:09:09.630 --> 01:09:13.310 like the text of lots and lots of web pages, dictionaries, encyclopedias, 01:09:13.310 --> 01:09:14.779 Wikipedias, and the like-- 01:09:14.779 --> 01:09:19.729 and infers, from the patterns of English words or any human language, 01:09:19.729 --> 01:09:22.907 what a typical human might actually say when asked a question. 01:09:22.907 --> 01:09:24.740 And some of these questions are easy, right? 01:09:24.740 --> 01:09:28.165 Probably, on the internet, alone, not to mention everyday life, if someone 01:09:28.165 --> 01:09:29.540 extends their hand and says, "Hi. 01:09:29.540 --> 01:09:33.000 How are you," odds are, with 90% probability, you're going to say, 01:09:33.000 --> 01:09:33.500 "Good. 01:09:33.500 --> 01:09:34.000 Thanks. 01:09:34.000 --> 01:09:34.700 How are you?" 01:09:34.700 --> 01:09:37.250 So I bet we could write software that just 01:09:37.250 --> 01:09:41.569 infers what it should say, probabilistically, statistically, based 01:09:41.569 --> 01:09:43.069 on those kinds of patterns online. 01:09:43.069 --> 01:09:45.442 And that's, indeed, where the world is starting 01:09:45.442 --> 01:09:47.359 to go, when it comes to the most sophisticated 01:09:47.359 --> 01:09:49.151 of algorithms, where you and I, the humans, 01:09:49.151 --> 01:09:51.979 we don't try to anticipate every possible input. 01:09:51.979 --> 01:09:54.620 Rather, we give it a more general purpose 01:09:54.620 --> 01:09:58.277 input, like all human knowledge, and ideally just let it figure things out. 01:09:58.277 --> 01:09:59.610 Now, we're not quite there, yet. 01:09:59.610 --> 01:10:02.000 And odds are you've heard of hallucinations or just mistakes 01:10:02.000 --> 01:10:03.680 that these large language models make. 01:10:03.680 --> 01:10:05.005 But their inputs are imperfect. 01:10:05.005 --> 01:10:07.130 And sometimes there's a bit of randomness sprinkled 01:10:07.130 --> 01:10:10.088 in because you don't want the thing to always say the exact same thing. 01:10:10.088 --> 01:10:14.250 Even you and I might say, "Eh, I'm not that great today" 10% of the time. 01:10:14.250 --> 01:10:16.730 So you need to perturb the output in some way. 01:10:16.730 --> 01:10:20.630 But within CS50 and within this world of large language models, 01:10:20.630 --> 01:10:24.630 we do have these tools like ChatGPT, and Bing, chat, and others. 01:10:24.630 --> 01:10:26.810 And we'll stipulate that, for CS50's purposes, 01:10:26.810 --> 01:10:28.910 the direction we're going this year is that this 01:10:28.910 --> 01:10:30.710 is what's in the syllabus, dot, dot, dot; 01:10:30.710 --> 01:10:32.840 that it will not be allowed, it will be considered 01:10:32.840 --> 01:10:37.348 not reasonable to use AI-based software, other than CS50's own. 01:10:37.348 --> 01:10:39.140 So the goal here is not to simply take away 01:10:39.140 --> 01:10:43.250 tools that are clearly inevitable, in my view, and clearly helpful 01:10:43.250 --> 01:10:44.720 and productivity savers. 01:10:44.720 --> 01:10:48.440 But we'd like there to be some guardrails, intellectually, on just how 01:10:48.440 --> 01:10:49.653 helpful these chatbots are. 01:10:49.653 --> 01:10:52.070 Because as you've probably seen, if you ask it a question, 01:10:52.070 --> 01:10:54.560 these chatbots are already pretty good at not just helping 01:10:54.560 --> 01:10:56.310 you finish your current thought, but it'll 01:10:56.310 --> 01:10:59.900 hand you your second and your third thought and do the assignment for you. 01:10:59.900 --> 01:11:02.660 But I think, through prompting, so to speak, 01:11:02.660 --> 01:11:04.850 we'll be able to coax some of our own tools, 01:11:04.850 --> 01:11:07.100 being computer scientists, ourself, in a direction 01:11:07.100 --> 01:11:10.200 that you actually find to be the right balance, akin to having 01:11:10.200 --> 01:11:14.310 a good tutor by your side 24/7, who doesn't just hand you answers, 01:11:14.310 --> 01:11:16.223 but, indeed, tries to lead you to the same. 01:11:16.223 --> 01:11:18.390 So you actually get something out of the experience, 01:11:18.390 --> 01:11:20.520 and, ideally, three-plus months from now, 01:11:20.520 --> 01:11:22.380 those training wheels can come off, too. 01:11:22.380 --> 01:11:24.880 And you're able to still stand on your own. 01:11:24.880 --> 01:11:28.770 So it will be reasonable to use CS50's own AI-based software which 01:11:28.770 --> 01:11:33.210 will actually take the form of a CS50 duck, which is actually available now-- 01:11:33.210 --> 01:11:37.170 and we'll use it throughout the term-- at CS50.ai, a web-based application 01:11:37.170 --> 01:11:39.450 that's quite similar to ChatGPT, but that 01:11:39.450 --> 01:11:44.010 has the personality of a CS50 teaching fellow or TF, or teaching assistant, 01:11:44.010 --> 01:11:47.220 TA, that also happens to think of itself as a duck. 01:11:47.220 --> 01:11:49.860 And for reasons we'll get to in a couple of weeks time, 01:11:49.860 --> 01:11:54.105 but rubber ducks, in particular, are a thing in programming. 01:11:54.105 --> 01:11:56.730 But more on that, before long, as you can even see from the one 01:11:56.730 --> 01:11:58.350 there on my desk. 01:11:58.350 --> 01:12:01.110 With that said, too, well, I'm going to call out CS50's own Brenda 01:12:01.110 --> 01:12:05.485 Anderson, whose daughter, Sophie, kindly not only created 01:12:05.485 --> 01:12:07.860 the first incarnation, digitally, of this duck, but also, 01:12:07.860 --> 01:12:11.560 most recently, once it actually did more than quack a random number 01:12:11.560 --> 01:12:13.540 of times in response to questions, has now 01:12:13.540 --> 01:12:15.320 been virtually brought to life, too. 01:12:15.320 --> 01:12:18.760 So, all that and more, over the coming weeks, but you'll find, ultimately, 01:12:18.760 --> 01:12:23.590 that the goal is to really bring to life the availability of an AI-based tutor 01:12:23.590 --> 01:12:26.650 that you have access to, a friend in your-- 01:12:26.650 --> 01:12:30.340 next to you, that will help guide you through a lot of the course's 01:12:30.340 --> 01:12:31.660 challenges along the way. 01:12:31.660 --> 01:12:33.310 And we actually rolled it out this past summer, 01:12:33.310 --> 01:12:35.950 already, with some of your predecessors, through the Harvard Summer School. 01:12:35.950 --> 01:12:38.200 One student wrote, at summer's end, that this duck 01:12:38.200 --> 01:12:40.180 "felt like having a personal tutor-- 01:12:40.180 --> 01:12:44.110 I love how AI bots will answer questions without ego and without judgment 01:12:44.110 --> 01:12:47.110 generally entertaining even the stupidest of questions 01:12:47.110 --> 01:12:49.360 without treating them like they're stupid. 01:12:49.360 --> 01:12:53.140 It has, as one could expect, an inhuman level of patience." 01:12:53.140 --> 01:12:56.410 So there's actually something really there because as many teachers 01:12:56.410 --> 01:12:57.520 as there are in CS50-- 01:12:57.520 --> 01:13:00.730 myself, the course's preceptors, teaching fellows, teaching assistants, 01:13:00.730 --> 01:13:03.490 and course assistants-- there's only so many of us. 01:13:03.490 --> 01:13:05.650 And we're only awake so many hours of the day. 01:13:05.650 --> 01:13:08.620 And I think you'll find, too, that we're on the cusp of something 01:13:08.620 --> 01:13:11.080 pretty remarkable, in the coming years, where 01:13:11.080 --> 01:13:14.380 it's going to get a lot more enabling, now, to learn material 01:13:14.380 --> 01:13:18.110 not only within the confines of a class, but on your own, ultimately, as well. 01:13:18.110 --> 01:13:22.270 And as one other student put it, at summer's end, with respect to the duck, 01:13:22.270 --> 01:13:23.680 "Love love loved the duck. 01:13:23.680 --> 01:13:24.490 We're friends now." 01:13:24.490 --> 01:13:25.720 So that, too, awaits. 01:13:25.720 --> 01:13:28.840 But, first, we're going to need to start with the basics. 01:13:28.840 --> 01:13:30.820 And we started today by talking about binary. 01:13:30.820 --> 01:13:32.710 And darn it, here it is again. 01:13:32.710 --> 01:13:37.060 So we can't actually get to the point of using, or solving, or implementing AI 01:13:37.060 --> 01:13:38.590 until we understand this. 01:13:38.590 --> 01:13:42.590 And odds are most of you won't know, at a glance, what this piece of software 01:13:42.590 --> 01:13:43.090 does. 01:13:43.090 --> 01:13:46.600 But these are the 0's and 1's that represent, perhaps, 01:13:46.600 --> 01:13:50.650 the first program that any programmer writes, which is now a clue to some 01:13:50.650 --> 01:13:52.700 of you who have dabbled in code before. 01:13:52.700 --> 01:13:56.470 What does this pattern of 0's and 1's tell a typical computer to do? 01:13:56.470 --> 01:13:57.348 Might someone guess? 01:13:57.348 --> 01:13:58.390 AUDIENCE: "Hello, world." 01:13:58.390 --> 01:14:00.760 DAVID J. MALAN: It's going to have it say, "hello, world," 01:14:00.760 --> 01:14:02.927 which is one of the very first programmer-- programs 01:14:02.927 --> 01:14:04.420 that most any programmer writes. 01:14:04.420 --> 01:14:06.790 Should you be able to recognize these 0's and 1's? 01:14:06.790 --> 01:14:08.260 Do I recognize these 0's and 1's? 01:14:08.260 --> 01:14:09.040 No, not at all. 01:14:09.040 --> 01:14:11.120 I just happen to know that they are the same. 01:14:11.120 --> 01:14:12.700 And that was a leading question. 01:14:12.700 --> 01:14:17.050 But they are representing data and instructions, ultimately, 01:14:17.050 --> 01:14:22.360 data like H-E-L-L-O, comma, W-O-R-L-D and an instruction like, 01:14:22.360 --> 01:14:24.910 "Print that data to the screen." 01:14:24.910 --> 01:14:26.920 As for what these patterns of 0's and 1's are, 01:14:26.920 --> 01:14:30.250 this is not something that a computer scientist or programmer worries about. 01:14:30.250 --> 01:14:34.240 We just stipulate that, OK, someone, somewhere knows how to do this. 01:14:34.240 --> 01:14:36.400 And it's probably someone like Intel, who makes 01:14:36.400 --> 01:14:37.960 the hardware inside of the computers. 01:14:37.960 --> 01:14:41.470 But you and I, starting now, already, in week zero, 01:14:41.470 --> 01:14:44.950 can start to view binary more abstractly. 01:14:44.950 --> 01:14:46.660 We don't care about the 0's and 1's. 01:14:46.660 --> 01:14:49.090 We only care that you can use 0's and 1's 01:14:49.090 --> 01:14:52.420 to represent more useful quantities, like numbers, 01:14:52.420 --> 01:14:54.260 and letters, colors, and more. 01:14:54.260 --> 01:14:57.040 So this, next week, is going to be the code we actually 01:14:57.040 --> 01:14:58.330 start writing at a keyboard. 01:14:58.330 --> 01:15:01.538 And this is that language called C. It's the same language I, myself, learned 01:15:01.538 --> 01:15:05.140 years ago, when taking CS50, when all we learned at the time was C. 01:15:05.140 --> 01:15:07.770 But this, too, has some crypticness to it. 01:15:07.770 --> 01:15:11.020 And if you've never programmed before, you can probably wrap your mind around, 01:15:11.020 --> 01:15:14.190 OK, I'm guessing the "printf" prints out the "hello, world." 01:15:14.190 --> 01:15:16.920 But what's with the semicolon, the quotes, the backslash, 01:15:16.920 --> 01:15:21.030 and the curly braces, the #include, and all of this stupid syntax? 01:15:21.030 --> 01:15:23.970 A lot of this, in the beginning of any class, is really a distraction. 01:15:23.970 --> 01:15:25.890 It is not intellectually interesting. 01:15:25.890 --> 01:15:29.230 But it's the stupid kind of stuff that's going to trip you up quite often. 01:15:29.230 --> 01:15:31.500 And so, today, what we'll do is focus not 01:15:31.500 --> 01:15:35.100 on syntax, characters on the keyboard, but ideas 01:15:35.100 --> 01:15:37.170 because what really matters in this program 01:15:37.170 --> 01:15:40.900 is that "printf" is a function here for our purposes. 01:15:40.900 --> 01:15:43.980 And that function is to display information on the screen. 01:15:43.980 --> 01:15:46.860 Everything else, as we'll soon see, has value 01:15:46.860 --> 01:15:50.435 and will be understood by you, before long, but for now, it's a distraction. 01:15:50.435 --> 01:15:52.200 Let's focus on those building blocks. 01:15:52.200 --> 01:15:54.940 When it comes time to write code, though, for the curious, 01:15:54.940 --> 01:15:56.815 especially if you've programmed before, we'll 01:15:56.815 --> 01:15:58.890 use a very popular free and open-source tool 01:15:58.890 --> 01:16:01.260 called Visual Studio Code, or VS Code. 01:16:01.260 --> 01:16:06.000 We'll use a cloud-based version of it that we pre-install everything 01:16:06.000 --> 01:16:08.070 you need in the cloud for you so you don't 01:16:08.070 --> 01:16:10.980 have to deal with headaches like getting your Mac or PC to work. 01:16:10.980 --> 01:16:13.920 You'll use instead this URL, cs50.dev, but more 01:16:13.920 --> 01:16:15.870 on that in next week, week one. 01:16:15.870 --> 01:16:19.650 For now, we'll use another cloud-based editor called Scratch. 01:16:19.650 --> 01:16:21.960 And odds are some number of you use this probably 01:16:21.960 --> 01:16:23.970 as early as like middle school or the like. 01:16:23.970 --> 01:16:27.300 And that was fine to create your own animations, games, 01:16:27.300 --> 01:16:28.650 interactive art, or the like. 01:16:28.650 --> 01:16:30.600 But we'll use it today for just a bit. 01:16:30.600 --> 01:16:32.670 And we'll use it in the course's first homework, 01:16:32.670 --> 01:16:36.870 AKA problem set 0, to explore now some of these same ideas. 01:16:36.870 --> 01:16:39.720 And among the goals today for the remainder of today 01:16:39.720 --> 01:16:42.920 is not to focus so much on Scratch and the particulars 01:16:42.920 --> 01:16:44.670 because this is not a language that you're 01:16:44.670 --> 01:16:50.280 going to use often but to give you very visual representations of ideas 01:16:50.280 --> 01:16:53.790 so that when things do escalate next week to C, to the more cryptic, 01:16:53.790 --> 01:16:57.840 it's the same ideas just typed out instead of dragged and dropped. 01:16:57.840 --> 01:16:59.460 So by that, I mean this. 01:16:59.460 --> 01:17:03.270 I'm going to go ahead and share in just a moment the user interface of Scratch. 01:17:03.270 --> 01:17:05.370 But what's nice about Scratch is that this 01:17:05.370 --> 01:17:08.400 is how we're going to implement that same program today. 01:17:08.400 --> 01:17:11.200 These are two blocks or puzzle pieces on the screen, 01:17:11.200 --> 01:17:15.670 and they've been interconnected to tell the computer to say "hello, world" 01:17:15.670 --> 01:17:16.570 on the screen. 01:17:16.570 --> 01:17:18.490 The user interface that we're about to use 01:17:18.490 --> 01:17:20.652 will look generally something like this. 01:17:20.652 --> 01:17:22.360 It's a web-based editor that you can also 01:17:22.360 --> 01:17:24.160 download it locally to use offline. 01:17:24.160 --> 01:17:28.120 And you'll see that at the left here are a whole bunch of puzzle pieces 01:17:28.120 --> 01:17:29.020 or blocks. 01:17:29.020 --> 01:17:31.630 They're categorized by color, and the blue ones 01:17:31.630 --> 01:17:33.980 tend to relate to motion, for instance. 01:17:33.980 --> 01:17:35.950 The purple ones represent looks. 01:17:35.950 --> 01:17:37.750 The pink one represents sounds. 01:17:37.750 --> 01:17:40.180 The yellow one represents events. 01:17:40.180 --> 01:17:41.230 More on that soon. 01:17:41.230 --> 01:17:43.270 The orange ones represent control, and then 01:17:43.270 --> 01:17:46.390 there's operators, variables, my blocks, and even some extensions 01:17:46.390 --> 01:17:47.690 we can install as well. 01:17:47.690 --> 01:17:51.820 So it just categorizes things visually and colorfully so that you 01:17:51.820 --> 01:17:53.470 can find what you're looking for. 01:17:53.470 --> 01:17:56.740 But we're going to use these puzzle pieces to drag and drop them 01:17:56.740 --> 01:17:58.300 onto this area here. 01:17:58.300 --> 01:18:02.030 And we're going to connect them when we want them to do something. 01:18:02.030 --> 01:18:02.890 What can they do? 01:18:02.890 --> 01:18:06.010 Well, by default, Scratch comes with this cat here, otherwise known 01:18:06.010 --> 01:18:09.760 as a sprite, which is a character in a game or in a graphics context. 01:18:09.760 --> 01:18:12.310 And this cat lives in this two-dimensional world in which 01:18:12.310 --> 01:18:14.050 the cat can go up, down, left, right. 01:18:14.050 --> 01:18:16.330 And you can actually change the cat's costume 01:18:16.330 --> 01:18:18.730 to be a dog, a bird, or anything else. 01:18:18.730 --> 01:18:23.260 It really is more of an idea than it is a specific animal in this case. 01:18:23.260 --> 01:18:26.950 But the world that Scratch lives in looks a little something like this. 01:18:26.950 --> 01:18:29.860 It's like a Cartesian plane with x-coordinates and y-coordinates. 01:18:29.860 --> 01:18:32.050 And the numbers don't so much matter fundamentally, 01:18:32.050 --> 01:18:35.390 except that when you want the cat or any character to go up, 01:18:35.390 --> 01:18:37.300 down, left, or right by some amount, it's 01:18:37.300 --> 01:18:42.985 useful to know, for instance, that 0, 0 is the middle, 0 comma 0 for x comma y. 01:18:42.985 --> 01:18:45.550 All the way up is a y value of 180. 01:18:45.550 --> 01:18:47.470 All the way down is -180. 01:18:47.470 --> 01:18:50.170 All the way to the left is -240. 01:18:50.170 --> 01:18:52.030 All the way to the right is 240. 01:18:52.030 --> 01:18:54.655 And generally, you're not going to have to worry about numbers. 01:18:54.655 --> 01:18:58.030 You're going to use these relatively-- go right, go left, go up, or down. 01:18:58.030 --> 01:19:01.970 But that's just the world that Scratch itself lives in here. 01:19:01.970 --> 01:19:03.730 So let's go about using Scratch here. 01:19:03.730 --> 01:19:06.880 I'm going to change over to my cloud-based editor 01:19:06.880 --> 01:19:10.120 here, where I've gone to scratch.mit.edu. 01:19:10.120 --> 01:19:12.520 And I've clicked Create in order to create a project. 01:19:12.520 --> 01:19:14.822 And that gives me this blank canvas here. 01:19:14.822 --> 01:19:17.530 And I'm going to do these somewhat quickly because I kind of know 01:19:17.530 --> 01:19:18.440 what I'm looking for. 01:19:18.440 --> 01:19:21.400 But part of the process with problem set zero is going to be clicking, 01:19:21.400 --> 01:19:23.410 and dragging, and sort of scrolling around 01:19:23.410 --> 01:19:25.390 to see what building blocks exist. 01:19:25.390 --> 01:19:29.020 But I know under Events there's this puzzle piece here 01:19:29.020 --> 01:19:30.910 when green flag clicked. 01:19:30.910 --> 01:19:32.110 Why is that germane? 01:19:32.110 --> 01:19:35.720 Well, if I zoom out, and go back to Scratch's world at the right here, 01:19:35.720 --> 01:19:37.720 notice that above Scratch's world there's 01:19:37.720 --> 01:19:40.870 a green flag, which is going to mean go, and a red stop sign, which, 01:19:40.870 --> 01:19:42.290 of course, is going to mean stop. 01:19:42.290 --> 01:19:46.780 So if I drag this puzzle piece anywhere into the middle, 01:19:46.780 --> 01:19:49.180 it's just going to plop where I put it. 01:19:49.180 --> 01:19:53.380 But what that means semantically is when someone clicks that green flag, 01:19:53.380 --> 01:19:56.440 I want Scratch the cat to do this other thing. 01:19:56.440 --> 01:19:57.970 Well, what do I want it to do? 01:19:57.970 --> 01:19:59.560 Well, let me go under Looks. 01:19:59.560 --> 01:20:04.090 And looks here in purple have some puzzle pieces like this. 01:20:04.090 --> 01:20:06.490 Well, I can say hello for some number of seconds, 01:20:06.490 --> 01:20:08.620 or I can just go ahead and say hello. 01:20:08.620 --> 01:20:09.430 So let's do that. 01:20:09.430 --> 01:20:11.650 I'm going to drag this say block. 01:20:11.650 --> 01:20:13.630 And notice that as soon as I get close enough, 01:20:13.630 --> 01:20:15.562 it's going to magnetically want to connect. 01:20:15.562 --> 01:20:17.770 So I can let go, and they snap together automatically 01:20:17.770 --> 01:20:19.103 because they're the right shape. 01:20:19.103 --> 01:20:21.100 I don't have to say hello, exclamation point. 01:20:21.100 --> 01:20:23.920 I can change it to the more canonical, hello comma world. 01:20:23.920 --> 01:20:26.440 So anything in this white oval is editable 01:20:26.440 --> 01:20:28.870 that you can change as just text there. 01:20:28.870 --> 01:20:31.960 If I now zoom out, let me go ahead and click the green flag. 01:20:31.960 --> 01:20:36.556 And voila-- this should be my first program in Scratch. 01:20:36.556 --> 01:20:37.430 Hello, world. 01:20:37.430 --> 01:20:39.410 Without any of the distractions of syntax 01:20:39.410 --> 01:20:42.420 or weird characters on the screen, it's just done what I want it to do. 01:20:42.420 --> 01:20:42.920 All right. 01:20:42.920 --> 01:20:44.462 Let me go ahead and click Stop there. 01:20:44.462 --> 01:20:47.240 And let me make it a little more connected 01:20:47.240 --> 01:20:48.750 to what we've discussed thus far. 01:20:48.750 --> 01:20:51.380 So this puzzle piece here, say hello, world, 01:20:51.380 --> 01:20:56.370 represents what type of building block using the vocabulary of today? 01:20:56.370 --> 01:20:57.420 So it's a function. 01:20:57.420 --> 01:21:00.048 So these purple blocks are functions, say, hello, world. 01:21:00.048 --> 01:21:02.090 And let me give you another piece of terminology. 01:21:02.090 --> 01:21:04.910 These white ovals that take textual input-- 01:21:04.910 --> 01:21:08.210 in general, those are called parameters or arguments. 01:21:08.210 --> 01:21:11.610 And they customize the behavior of a function. 01:21:11.610 --> 01:21:14.180 So a parameter or an argument customizes, 01:21:14.180 --> 01:21:17.810 modifies the default behavior of a function, like in this case, say. 01:21:17.810 --> 01:21:20.990 Similarly, in the C code from earlier that we'll see next week, 01:21:20.990 --> 01:21:27.350 the printf function took a quoted phrase like, hello, world, similarly as input. 01:21:27.350 --> 01:21:29.970 But more on that in the future. 01:21:29.970 --> 01:21:32.810 So how does this connect to the mental model we presented earlier? 01:21:32.810 --> 01:21:37.140 Well, here's problem-solving as I described it earlier, inputs to outputs 01:21:37.140 --> 01:21:39.090 with algorithms or code in the middle. 01:21:39.090 --> 01:21:43.710 Well, what we've got here really is an input of, hello, world, the white oval. 01:21:43.710 --> 01:21:48.600 The function or algorithm that it's going into as input is the say block. 01:21:48.600 --> 01:21:51.960 And what is the output of using this say block, the say function? 01:21:51.960 --> 01:21:55.650 It's the visual effect of having the cat have the little speech bubble appear 01:21:55.650 --> 01:21:58.060 above its head, saying, hello, world. 01:21:58.060 --> 01:22:00.900 So everything we do, in fact, can be mapped back 01:22:00.900 --> 01:22:04.590 to that very simple idea of inputs and outputs. 01:22:04.590 --> 01:22:06.840 Well, let's make it a little more interesting, though. 01:22:06.840 --> 01:22:09.430 It's a little boring to just say "hello, world" all the time. 01:22:09.430 --> 01:22:11.500 Let me go ahead and drag this away. 01:22:11.500 --> 01:22:15.180 And if you just drag it to the left and let go, it gets automatically deleted. 01:22:15.180 --> 01:22:18.580 Let me go under Sensing in light blue here. 01:22:18.580 --> 01:22:20.850 And you'll see there's a bunch of blocks, one of which 01:22:20.850 --> 01:22:24.180 is an ask block, an ask function, which is going to prompt 01:22:24.180 --> 01:22:26.170 the human, me, for some input. 01:22:26.170 --> 01:22:29.700 So let me go ahead and drag that over here, and it snaps together. 01:22:29.700 --> 01:22:32.190 I could change the question, but I'm OK with that question. 01:22:32.190 --> 01:22:33.670 We'll use what's your name. 01:22:33.670 --> 01:22:36.900 But notice that this block, ask, is a little special. 01:22:36.900 --> 01:22:40.240 It is not just going to display like a speech bubble on the screen. 01:22:40.240 --> 01:22:42.510 It's actually going to return a value, and this is 01:22:42.510 --> 01:22:44.250 another term of art today and onward. 01:22:44.250 --> 01:22:48.492 A return value is some value that can be handed back to you conceptually 01:22:48.492 --> 01:22:50.700 from a function so that you can do something with it. 01:22:50.700 --> 01:22:55.025 It's as though the ask function asks someone for their name, 01:22:55.025 --> 01:22:57.900 writes it down on a piece of paper, and hands you the piece of paper. 01:22:57.900 --> 01:23:00.150 You can do anything now that you want with that name. 01:23:00.150 --> 01:23:03.810 And here is how you access the name in this special block called 01:23:03.810 --> 01:23:07.330 answer, which, again, will start calling a return value. 01:23:07.330 --> 01:23:10.770 So if I want to say "hello" to someone specific, I'm going to do this. 01:23:10.770 --> 01:23:11.800 Let me zoom out. 01:23:11.800 --> 01:23:15.510 Let me go back to Looks, and let me go back to Say. 01:23:15.510 --> 01:23:19.890 And I'm going to change the say block here to "hello, comma." 01:23:19.890 --> 01:23:21.270 Then I'm going to zoom out. 01:23:21.270 --> 01:23:23.700 Well, I need two inputs, I think. 01:23:23.700 --> 01:23:26.640 So I'm going to grab another say block, and I'm going to put it below. 01:23:26.640 --> 01:23:29.160 And I could just type "David," but this is 01:23:29.160 --> 01:23:31.660 going to defeat the whole point of asking me for the name. 01:23:31.660 --> 01:23:32.610 And it will only work for me. 01:23:32.610 --> 01:23:33.790 So I don't want to do that. 01:23:33.790 --> 01:23:38.520 So let me go back to Sensing, and notice the shape is important here. 01:23:38.520 --> 01:23:41.130 Even if it's not quite the same size, the shape matters. 01:23:41.130 --> 01:23:45.690 And I can actually drag this and change the input of this say function 01:23:45.690 --> 01:23:48.870 to be whatever that return value is, that piece of paper 01:23:48.870 --> 01:23:50.430 that has the person's name on it. 01:23:50.430 --> 01:23:53.550 And it grows to fill, but now we have a program 01:23:53.550 --> 01:23:57.240 that I think when I click the green flag-- watch-- is going to prompt me. 01:23:57.240 --> 01:23:58.367 What's your name? 01:23:58.367 --> 01:23:59.950 And now I have room to type down here. 01:23:59.950 --> 01:24:02.370 So I'm going to type D-A-V-I-D. I'm going to hit Enter, 01:24:02.370 --> 01:24:04.000 and it should say "hello, Dave." 01:24:04.000 --> 01:24:04.500 Wait. 01:24:04.500 --> 01:24:06.110 Hmm. 01:24:06.110 --> 01:24:07.050 Huh. 01:24:07.050 --> 01:24:09.990 Maybe it was-- didn't work. 01:24:09.990 --> 01:24:11.160 D-A-V-I-D. Here we go. 01:24:11.160 --> 01:24:12.165 Hello, David. 01:24:12.165 --> 01:24:13.530 Hmm. 01:24:13.530 --> 01:24:19.210 It's missing the hello, but I'm quite sure we have a hello right there. 01:24:19.210 --> 01:24:22.170 So what explains this bug or mistake? 01:24:22.170 --> 01:24:23.117 Yeah. 01:24:23.117 --> 01:24:26.596 AUDIENCE: [INAUDIBLE]. 01:24:26.596 --> 01:24:29.090 So they overlap. 01:24:29.090 --> 01:24:30.800 DAVID J. MALAN: Exactly. 01:24:30.800 --> 01:24:33.710 Put another way, my Mac, my PC, it's just so darn 01:24:33.710 --> 01:24:36.320 fast that it did exactly what it was supposed to. 01:24:36.320 --> 01:24:40.580 But it said "hello, David" so fast that we didn't even see, we being the human, 01:24:40.580 --> 01:24:42.795 the slowest part of the puzzle, see the actual hello. 01:24:42.795 --> 01:24:45.170 So there's a few different ways to fix this, as you know. 01:24:45.170 --> 01:24:48.660 We could have it say "hello" for some number of seconds. 01:24:48.660 --> 01:24:49.910 So I could kind of do that. 01:24:49.910 --> 01:24:50.660 So let me do this. 01:24:50.660 --> 01:24:53.210 I can decouple these by just dragging and letting it go 01:24:53.210 --> 01:24:55.220 so that they're magnetically far apart. 01:24:55.220 --> 01:24:58.610 Let me go ahead and drag this one, say hello for two seconds. 01:24:58.610 --> 01:25:02.030 I'm going to change the grammar here to be hello comma again. 01:25:02.030 --> 01:25:05.120 I'm going to go ahead and disconnect these two. 01:25:05.120 --> 01:25:08.160 I'm going to throw away the old one that I don't want to use. 01:25:08.160 --> 01:25:11.460 And I'm going to reconnect this so that now-- 01:25:11.460 --> 01:25:11.960 OK. 01:25:11.960 --> 01:25:14.960 It's going to say hello for two seconds and then my name, hopefully. 01:25:14.960 --> 01:25:17.180 So let me click Stop and Start. 01:25:17.180 --> 01:25:20.970 D-A-V-I-D. Enter. 01:25:20.970 --> 01:25:21.540 OK. 01:25:21.540 --> 01:25:25.577 So it's better, but it's kind of poorly implemented. 01:25:25.577 --> 01:25:26.160 Like, come on. 01:25:26.160 --> 01:25:27.785 I just wanted to say hello comma David. 01:25:27.785 --> 01:25:28.785 Why is that hard? 01:25:28.785 --> 01:25:31.410 Well, maybe we can actually combine these a little differently. 01:25:31.410 --> 01:25:32.610 And let me propose this. 01:25:32.610 --> 01:25:35.760 Let me actually get rid of these blocks again. 01:25:35.760 --> 01:25:38.430 And let me go ahead and just say one thing. 01:25:38.430 --> 01:25:43.650 But can I somehow combine this to say hello comma David all in one breath? 01:25:43.650 --> 01:25:46.348 Well, it turns out if I go under Operators, 01:25:46.348 --> 01:25:49.140 I know from having played with this before that there's this puzzle 01:25:49.140 --> 01:25:51.030 piece down here called join. 01:25:51.030 --> 01:25:52.463 It's an oval. 01:25:52.463 --> 01:25:54.630 It's a little big, but, again, it will grow to fill. 01:25:54.630 --> 01:25:57.338 And by default, it wants to join two words, "apple" and "banana." 01:25:57.338 --> 01:25:59.830 But those are just placeholders. 01:25:59.830 --> 01:26:03.990 So let me go ahead and drag this over the default hello. 01:26:03.990 --> 01:26:08.700 Let me change "apple" to hello comma space and then banana. 01:26:08.700 --> 01:26:10.230 Let me go back to Sensing. 01:26:10.230 --> 01:26:13.630 Let me grab answer and drag and drop that. 01:26:13.630 --> 01:26:16.740 So now notice that I'm kind of layering my ideas. 01:26:16.740 --> 01:26:20.980 And put another way, the output of this join block 01:26:20.980 --> 01:26:22.980 is presumably going to join two things together, 01:26:22.980 --> 01:26:25.110 apple and banana or hello comma David. 01:26:25.110 --> 01:26:29.460 And then the output of join is going to become the input to say. 01:26:29.460 --> 01:26:31.900 So aesthetically, it just looks better. 01:26:31.900 --> 01:26:33.790 It's still correct, but it's just better. 01:26:33.790 --> 01:26:36.840 So if I type "David," and hit Enter, hello, David. 01:26:36.840 --> 01:26:38.820 This is what a normal program would presumably 01:26:38.820 --> 01:26:42.390 do, not show you part of the phrase and then the rest of the phrase. 01:26:42.390 --> 01:26:44.230 Like, it's just better in this way. 01:26:44.230 --> 01:26:48.480 So let's connect this now to this same puzzle piece and this methodology. 01:26:48.480 --> 01:26:50.610 So here's that same puzzle piece, ask. 01:26:50.610 --> 01:26:53.520 How do we fit it into this input and output flow with algorithms? 01:26:53.520 --> 01:26:57.120 Well, the input to that puzzle piece is something like, what's your name, 01:26:57.120 --> 01:26:58.200 question mark. 01:26:58.200 --> 01:27:01.680 Then the algorithm or the code implementation thereof 01:27:01.680 --> 01:27:06.240 is this ask block and wait so the human has a moment to type their response in. 01:27:06.240 --> 01:27:09.548 The output of that function recall is a return value. 01:27:09.548 --> 01:27:12.840 You don't see anything on the screen by default because we've not used say yet, 01:27:12.840 --> 01:27:14.860 but we get this return value. 01:27:14.860 --> 01:27:18.120 And let me scooch everything over now so that we can now 01:27:18.120 --> 01:27:20.110 join those inputs together. 01:27:20.110 --> 01:27:21.610 So here's this puzzle piece. 01:27:21.610 --> 01:27:24.940 Let me go ahead and propose that the inputs now to the join block 01:27:24.940 --> 01:27:30.100 are two arguments or two parameters, so to speak, hello and answer. 01:27:30.100 --> 01:27:32.740 They go into that join puzzle piece, whose purpose in life 01:27:32.740 --> 01:27:36.040 is to return one joined version thereof. 01:27:36.040 --> 01:27:38.470 Let me slide this all over logically now so 01:27:38.470 --> 01:27:42.850 that now that output becomes the input to the say block 01:27:42.850 --> 01:27:46.330 and now is why the cat has the speech bubble saying all at once, 01:27:46.330 --> 01:27:47.740 hello comma David. 01:27:47.740 --> 01:27:51.620 So what we've done here is kind of composed the output 01:27:51.620 --> 01:27:54.017 and from one function into the input of another. 01:27:54.017 --> 01:27:56.350 And you can think of this in a couple of different ways, 01:27:56.350 --> 01:27:58.150 left to right, as I did there, or kind of 01:27:58.150 --> 01:28:00.740 like stacking these things on top of one another. 01:28:00.740 --> 01:28:02.860 But at the end of the day, even as programming 01:28:02.860 --> 01:28:07.510 gets more and more powerful for us, it's just inputs and outputs, inputs 01:28:07.510 --> 01:28:08.320 and outputs. 01:28:08.320 --> 01:28:11.230 And thankfully, with built-in functionality from our friends 01:28:11.230 --> 01:28:14.680 at MIT who designed Scratch, I can even do something playful like this. 01:28:14.680 --> 01:28:16.990 I can go to that Extensions button at the bottom. 01:28:16.990 --> 01:28:20.260 And there's a lot of fancy things I can add here, like text-to-speech. 01:28:20.260 --> 01:28:22.680 So let me go ahead and choose text-to-speech. 01:28:22.680 --> 01:28:28.050 And let me go ahead here and change the say block in purple. 01:28:28.050 --> 01:28:30.450 Let me get rid of the say block, and let me borrow this. 01:28:30.450 --> 01:28:33.300 Let me get the speak block like this. 01:28:33.300 --> 01:28:35.250 And now let me drag and drop this oval. 01:28:35.250 --> 01:28:36.470 It's going to grow to fill. 01:28:36.470 --> 01:28:38.970 And I think it's just about to be a little more interesting. 01:28:38.970 --> 01:28:42.090 Let me click Play now, and hopefully this isn't too loud. 01:28:42.090 --> 01:28:44.280 D-A-V-I-D. Enter. 01:28:44.280 --> 01:28:45.948 SPEAKER: Hello, David. 01:28:45.948 --> 01:28:46.740 DAVID J. MALAN: OK. 01:28:46.740 --> 01:28:47.730 [APPLAUSE] 01:28:47.730 --> 01:28:50.350 Thank you. 01:28:50.350 --> 01:28:50.980 Thank you. 01:28:50.980 --> 01:28:51.835 That's a low bar. 01:28:51.835 --> 01:28:54.550 [CHUCKLES] Let me go ahead and set the voice too. 01:28:54.550 --> 01:28:56.830 And you might now remember how we began class, where 01:28:56.830 --> 01:28:59.080 we had a robotic, computerized voice. 01:28:59.080 --> 01:29:03.168 Well, we didn't use Scratch at the time, but we could change this in Scratch 01:29:03.168 --> 01:29:04.460 alone to be a little different. 01:29:04.460 --> 01:29:05.890 So D-A-V-I-D. 01:29:05.890 --> 01:29:07.028 SPEAKER: Hello, David. 01:29:07.028 --> 01:29:07.820 DAVID J. MALAN: OK. 01:29:07.820 --> 01:29:10.070 Little creepy, but we can play all day long with that. 01:29:10.070 --> 01:29:12.125 But the point is that these functions are just 01:29:12.125 --> 01:29:13.750 now doing something a little different. 01:29:13.750 --> 01:29:15.792 But it's just these inputs and outputs. 01:29:15.792 --> 01:29:17.500 Well, let's make the cat more like a cat. 01:29:17.500 --> 01:29:20.590 Let me go ahead and throw away all of this asking question stuff. 01:29:20.590 --> 01:29:23.350 Let me go up to Sound, and let me go ahead 01:29:23.350 --> 01:29:26.260 and drag the play sound meow until done. 01:29:26.260 --> 01:29:27.910 And here too it comes with meow. 01:29:27.910 --> 01:29:31.697 You can add your own sounds as well. 01:29:31.697 --> 01:29:34.030 But I'm just going to use the default meow and here too. 01:29:34.030 --> 01:29:35.447 Hopefully, this won't be too loud. 01:29:35.447 --> 01:29:37.360 Let's make the cat meow by clicking Play. 01:29:37.360 --> 01:29:39.530 [MEOWING] 01:29:39.530 --> 01:29:40.030 OK. 01:29:40.030 --> 01:29:42.200 It's a little piercing, but it's cute. 01:29:42.200 --> 01:29:45.490 And if I want the cat to meow twice, I could just play the game twice. 01:29:45.490 --> 01:29:48.410 [MEOWING] 01:29:48.410 --> 01:29:48.910 All right. 01:29:48.910 --> 01:29:52.360 But it would be nice to just get it to meow automatically two, or three, 01:29:52.360 --> 01:29:53.240 or more times. 01:29:53.240 --> 01:29:54.760 So you know what I could do? 01:29:54.760 --> 01:29:56.820 I could just drag a second one of these. 01:29:56.820 --> 01:29:57.820 Actually, you know what? 01:29:57.820 --> 01:30:00.790 I could even just right-click or Control-click and duplicate them. 01:30:00.790 --> 01:30:02.110 But I'll just keep dragging and dropping. 01:30:02.110 --> 01:30:03.670 There's different ways to solve problems. 01:30:03.670 --> 01:30:04.780 And now let me click Play. 01:30:04.780 --> 01:30:07.370 [MEOWING] 01:30:07.370 --> 01:30:07.870 OK. 01:30:07.870 --> 01:30:09.940 Cat does not sound particularly happy. 01:30:09.940 --> 01:30:12.320 So we could go under-- 01:30:12.320 --> 01:30:13.690 how about Control? 01:30:13.690 --> 01:30:15.220 We could wait one second. 01:30:15.220 --> 01:30:18.740 Now, there's no room, but it will sort of expand to give room for me. 01:30:18.740 --> 01:30:19.900 So let me try this. 01:30:19.900 --> 01:30:22.540 And now it's going to wait one second in between meows. 01:30:22.540 --> 01:30:25.828 [MEOWING] 01:30:26.820 --> 01:30:27.330 OK. 01:30:27.330 --> 01:30:28.830 Let me stipulate that is correct. 01:30:28.830 --> 01:30:32.190 If my goal is to get the cat to meow three times, it meowed three times. 01:30:32.190 --> 01:30:36.640 But per our discussion earlier of algorithms and the design thereof, 01:30:36.640 --> 01:30:38.770 this is not the best design. 01:30:38.770 --> 01:30:39.760 [MEOWING] 01:30:39.760 --> 01:30:40.260 OK? 01:30:40.260 --> 01:30:40.500 [LAUGHTER] 01:30:40.500 --> 01:30:42.000 Thank you for playing along at home. 01:30:42.000 --> 01:30:43.350 Yeah. 01:30:43.350 --> 01:30:47.050 In what sense is this arguably not well-designed? 01:30:47.050 --> 01:30:47.550 Yeah. 01:30:47.550 --> 01:30:48.930 AUDIENCE: You repeated yourself. 01:30:48.930 --> 01:30:51.600 DAVID J. MALAN: I repeated myself, which actually in programming 01:30:51.600 --> 01:30:53.440 tends not to be a good thing. 01:30:53.440 --> 01:30:54.510 Now, it was easy. 01:30:54.510 --> 01:30:57.510 I almost resorted to copy-paste, which saves me time upfront. 01:30:57.510 --> 01:30:59.400 But just imagine a contrived scenario. 01:30:59.400 --> 01:31:02.350 Now, what if I want it to wait like two seconds in between? 01:31:02.350 --> 01:31:02.850 All right. 01:31:02.850 --> 01:31:03.940 It's not that big a deal. 01:31:03.940 --> 01:31:05.640 I change it here, and I change it here. 01:31:05.640 --> 01:31:07.913 But what if the program is meant to meow 10 times? 01:31:07.913 --> 01:31:10.830 Then I have to change it here, and here, and here, and here, and here. 01:31:10.830 --> 01:31:12.570 And eventually I'm going to screw up. 01:31:12.570 --> 01:31:13.470 Humans are fallible. 01:31:13.470 --> 01:31:14.970 I'm going to overlook one of them. 01:31:14.970 --> 01:31:16.260 One time, it's going to be one second. 01:31:16.260 --> 01:31:18.910 Another is going to be two, and just stupid things will happen. 01:31:18.910 --> 01:31:23.050 You're setting yourself up for failure if you design things poorly. 01:31:23.050 --> 01:31:26.440 And so I would propose that we use another type of building block 01:31:26.440 --> 01:31:27.730 to solve this instead. 01:31:27.730 --> 01:31:28.555 Yeah. 01:31:28.555 --> 01:31:28.810 AUDIENCE: [INAUDIBLE] 01:31:28.810 --> 01:31:29.685 DAVID J. MALAN: Yeah. 01:31:29.685 --> 01:31:32.950 So we could use a loop and just write minimal code 01:31:32.950 --> 01:31:35.240 but to accomplish even more functionality. 01:31:35.240 --> 01:31:37.390 So let me throw away most of these blocks. 01:31:37.390 --> 01:31:40.990 And let's go and grab this repeat block, which we haven't used yet, 01:31:40.990 --> 01:31:42.042 but it's right there. 01:31:42.042 --> 01:31:43.750 And as the name suggests, this will allow 01:31:43.750 --> 01:31:45.850 me to repeat something some number of times. 01:31:45.850 --> 01:31:48.280 Let me put these two puzzle pieces inside. 01:31:48.280 --> 01:31:49.330 It'll grow to fill. 01:31:49.330 --> 01:31:51.370 Let me reconnect it to the green flag. 01:31:51.370 --> 01:31:53.470 I'll change the default 10 to a 3. 01:31:53.470 --> 01:31:54.265 And now-- 01:31:54.265 --> 01:31:57.526 [MEOWING] 01:31:58.940 --> 01:32:02.290 It's just sort of better because if now you want it to meow more times, 01:32:02.290 --> 01:32:03.290 you change it one place. 01:32:03.290 --> 01:32:05.623 If you want it to slow down, you change it in one place. 01:32:05.623 --> 01:32:07.280 There's no room for error. 01:32:07.280 --> 01:32:09.920 And that's generally a good thing. 01:32:09.920 --> 01:32:10.670 But this is silly. 01:32:10.670 --> 01:32:12.350 Like, Scratch comes with a cat. 01:32:12.350 --> 01:32:15.590 Why didn't MIT give us a puzzle piece called "meow?" 01:32:15.590 --> 01:32:18.460 Like, why am I implementing the idea of meowing myself? 01:32:18.460 --> 01:32:19.460 Like, that took me what? 01:32:19.460 --> 01:32:21.410 1, 2, 3, 4 puzzle pieces. 01:32:21.410 --> 01:32:24.080 Why isn't there just one puzzle piece that meows for me? 01:32:24.080 --> 01:32:28.430 This too we can do in code, be it in Scratch, or C, or other languages too. 01:32:28.430 --> 01:32:32.660 I'm going to go down to these pink my blocks here, 01:32:32.660 --> 01:32:35.027 where I can create my own puzzle piece. 01:32:35.027 --> 01:32:36.860 And I'm going to call this literally "meow." 01:32:36.860 --> 01:32:39.150 And I'm going to go ahead and just click OK. 01:32:39.150 --> 01:32:42.242 And notice that it's given me this new type of start connector. 01:32:42.242 --> 01:32:44.450 It's a start puzzle piece that nothing goes above it. 01:32:44.450 --> 01:32:46.430 But you can put anything you want below it. 01:32:46.430 --> 01:32:48.170 And I'm going to go ahead and cheat here. 01:32:48.170 --> 01:32:51.390 I'm just going to grab my existing code, so to speak. 01:32:51.390 --> 01:32:53.810 This is code I'm writing, even though it's puzzle pieces. 01:32:53.810 --> 01:32:57.380 And now let me just claim, and I'll move this aside. 01:32:57.380 --> 01:33:00.690 Here is now an implementation of my own function, 01:33:00.690 --> 01:33:03.510 my own block called "meow," whose purpose in life 01:33:03.510 --> 01:33:06.180 is to meow until done and then wait one second. 01:33:06.180 --> 01:33:10.350 But what's powerful now is notice at top left, now that I've made the block, 01:33:10.350 --> 01:33:12.910 I can use it any number of times. 01:33:12.910 --> 01:33:17.520 So I can grab this meow block, drag it over here, and you know what? 01:33:17.520 --> 01:33:21.590 Now that "meow" exists as an idea, I can abstract that away. 01:33:21.590 --> 01:33:24.090 And I'm just going to arbitrarily drag it way to the bottom. 01:33:24.090 --> 01:33:24.990 I'm not deleting it. 01:33:24.990 --> 01:33:27.270 I'm just putting it out of sight, out of mind so 01:33:27.270 --> 01:33:28.890 that we can focus now on this idea. 01:33:28.890 --> 01:33:31.410 And I claim that this implementation of meowing 01:33:31.410 --> 01:33:35.010 is sort of better because it's more compact, it does what it says, 01:33:35.010 --> 01:33:38.520 and I don't care about the implementation details of "meow." 01:33:38.520 --> 01:33:41.640 So this idea of abstraction, something we're going to use frequently. 01:33:41.640 --> 01:33:44.767 To abstract something away is to simplify. 01:33:44.767 --> 01:33:47.100 Don't think about the underlying implementation details. 01:33:47.100 --> 01:33:50.130 Just care about what it does or what it's called. 01:33:50.130 --> 01:33:52.590 Someone has to care about the implementation details, 01:33:52.590 --> 01:33:54.330 like me 30 seconds ago. 01:33:54.330 --> 01:33:56.430 But here on out, I don't need to care. 01:33:56.430 --> 01:33:59.690 And so in fact, you and I are using the abstraction 01:33:59.690 --> 01:34:02.940 that is Scratch because I don't know how to put a speech bubble on the screen. 01:34:02.940 --> 01:34:04.773 I don't know how to create that sound, meow. 01:34:04.773 --> 01:34:07.380 MIT did that, and they abstracted those pieces 01:34:07.380 --> 01:34:10.440 of functionality away already for us by just giving us 01:34:10.440 --> 01:34:12.280 these puzzle pieces we see here. 01:34:12.280 --> 01:34:14.445 So the code will work the exact same. 01:34:14.445 --> 01:34:17.330 [MEOWING] 01:34:17.330 --> 01:34:21.640 But it's sort of better designed now because now I've 01:34:21.640 --> 01:34:23.470 abstracted away the idea of meowing. 01:34:23.470 --> 01:34:25.690 But I bet I can improve this further. 01:34:25.690 --> 01:34:28.390 Can I get rid of the repeat block altogether? 01:34:28.390 --> 01:34:31.510 And let me just tell the meow block how many times to meow. 01:34:31.510 --> 01:34:35.170 Well, let me go down to the bottom and look at the implementation details. 01:34:35.170 --> 01:34:37.270 I'm going to right-click or Control-click on this, 01:34:37.270 --> 01:34:38.210 and I'm going to edit it. 01:34:38.210 --> 01:34:39.290 So I'm going to make a change. 01:34:39.290 --> 01:34:42.290 And I didn't do this before, but I'm going to call it "meow," as before. 01:34:42.290 --> 01:34:43.780 I'm going to add an input. 01:34:43.780 --> 01:34:46.240 And just so I know what it says what it does, 01:34:46.240 --> 01:34:49.000 I'm going to add the word "times" here. 01:34:49.000 --> 01:34:52.840 And I'm going to change this placeholder to n. 01:34:52.840 --> 01:34:54.970 n for "number" is the go-to placeholder any time 01:34:54.970 --> 01:34:57.230 we want to talk about a number in CS. 01:34:57.230 --> 01:35:00.020 So now notice the puzzle piece looks a little different. 01:35:00.020 --> 01:35:02.830 It takes an argument or a parameter called 01:35:02.830 --> 01:35:05.680 n, which represents the number of times you want the thing to meow. 01:35:05.680 --> 01:35:07.280 Now, that doesn't do that yet. 01:35:07.280 --> 01:35:10.240 So let me go back to my other code. 01:35:10.240 --> 01:35:13.210 Let me just decouple these temporarily. 01:35:13.210 --> 01:35:17.950 I'm going to move my loop into my implementation of meowing. 01:35:17.950 --> 01:35:21.950 But I don't want to hard code, that is literally write the number 3 anymore. 01:35:21.950 --> 01:35:25.860 I'm going to grab this oval and put it there. 01:35:25.860 --> 01:35:27.900 So now I've generalized the function. 01:35:27.900 --> 01:35:33.860 So now it will meow any number of times, 0 on up, by executing that loop 01:35:33.860 --> 01:35:35.540 and now more powerfully. 01:35:35.540 --> 01:35:36.890 Out of sight, out of mind. 01:35:36.890 --> 01:35:40.850 Notice that my code just became so darn simple. 01:35:40.850 --> 01:35:42.650 Like, my function is called "meow." 01:35:42.650 --> 01:35:44.330 It meows some number of times. 01:35:44.330 --> 01:35:46.910 All I have to do is type a number there, and it just works. 01:35:46.910 --> 01:35:50.660 And I don't care any more about those lower-level, so to speak, 01:35:50.660 --> 01:35:52.230 implementation details. 01:35:52.230 --> 01:35:53.270 So here, no surprise. 01:35:53.270 --> 01:35:55.985 If I type in the number 3, zoom out, and hit Play-- 01:35:55.985 --> 01:36:00.270 [MEOWING] 01:36:00.270 --> 01:36:02.500 --it still works just fine. 01:36:02.500 --> 01:36:05.460 So any questions on what we've just done here? 01:36:05.460 --> 01:36:07.710 It's still just meowing, but that's besides the point. 01:36:07.710 --> 01:36:11.340 It's this creation of our own functions, this modularity, 01:36:11.340 --> 01:36:15.640 this abstraction that's going to be the idea that keeps coming back to us. 01:36:15.640 --> 01:36:16.140 No? 01:36:16.140 --> 01:36:16.640 All right. 01:36:16.640 --> 01:36:19.050 So let's make this a little more cat-like. 01:36:19.050 --> 01:36:20.980 Let me throw away all of this code. 01:36:20.980 --> 01:36:22.000 And let me go ahead-- 01:36:22.000 --> 01:36:25.000 oops-- let me throw away this code first and then the rest of this code. 01:36:25.000 --> 01:36:28.500 And let me go ahead and give myself another green flag block. 01:36:28.500 --> 01:36:32.430 And let me go ahead, and let's create a cat that allows us to pet it 01:36:32.430 --> 01:36:34.530 by moving my cursor over the cat. 01:36:34.530 --> 01:36:36.820 And maybe it likes that, so it'll meow when I do that. 01:36:36.820 --> 01:36:42.270 So let me go under Control, and let me grab this if conditional, 01:36:42.270 --> 01:36:44.910 which we talked about as a building block earlier. 01:36:44.910 --> 01:36:47.310 Let me go to Sensing, and we haven't used this before. 01:36:47.310 --> 01:36:50.130 But here is a weird sort of diagonal shape 01:36:50.130 --> 01:36:52.440 that says touching mouse pointer question mark. 01:36:52.440 --> 01:36:54.027 So that's a Boolean expression. 01:36:54.027 --> 01:36:56.610 So I'm going to drag that, and it's definitely the wrong size. 01:36:56.610 --> 01:36:59.070 But it is the right shape, so it will grow to fill. 01:36:59.070 --> 01:37:03.820 And the question I want to ask is if the cat is touching the mouse pointer, 01:37:03.820 --> 01:37:05.770 then go ahead and meow happily. 01:37:05.770 --> 01:37:08.360 So let me grab the meow sound, put it in there. 01:37:08.360 --> 01:37:12.970 And so I think when I click the green flag to start the program, 01:37:12.970 --> 01:37:19.174 and now I let the mouse pointer touch the cat, we should hear-- 01:37:19.174 --> 01:37:20.110 huh. 01:37:20.110 --> 01:37:21.120 huh. 01:37:21.120 --> 01:37:23.820 Doesn't seem to be working. 01:37:23.820 --> 01:37:27.440 There's a bug or a mistake in this program too. 01:37:27.440 --> 01:37:28.440 What did I do wrong? 01:37:28.440 --> 01:37:28.940 Yeah. 01:37:28.940 --> 01:37:31.090 AUDIENCE: You didn't specify the sprite [INAUDIBLE].. 01:37:31.090 --> 01:37:33.130 DAVID J. MALAN: I don't need to specify the sprite explicitly 01:37:33.130 --> 01:37:35.200 because a detail I didn't discuss earlier. 01:37:35.200 --> 01:37:37.630 In the bottom right of the screen, notice 01:37:37.630 --> 01:37:39.890 that the cat is already selected. 01:37:39.890 --> 01:37:42.100 So this code relates to that cat. 01:37:42.100 --> 01:37:45.340 So your instinct is good if we had two or more sprites, 01:37:45.340 --> 01:37:48.045 but I just have one, so I'm covered there. 01:37:48.045 --> 01:37:48.670 Other thoughts. 01:37:48.670 --> 01:37:49.602 Yeah. 01:37:49.602 --> 01:37:50.870 AUDIENCE: It only checks once. 01:37:50.870 --> 01:37:51.980 DAVID J. MALAN: It only checks? 01:37:51.980 --> 01:37:52.250 AUDIENCE: Once. 01:37:52.250 --> 01:37:53.125 DAVID J. MALAN: Once. 01:37:53.125 --> 01:37:54.960 So I click the green flag. 01:37:54.960 --> 01:37:57.230 The computer did what I told it to do. 01:37:57.230 --> 01:38:00.165 The mouse pointer was not touching the cat at that moment 01:38:00.165 --> 01:38:01.790 because it was touching the green flag. 01:38:01.790 --> 01:38:03.320 So, of course, it didn't meow. 01:38:03.320 --> 01:38:05.360 So what maybe is the fix here? 01:38:05.360 --> 01:38:06.980 What puzzle piece can we add? 01:38:06.980 --> 01:38:09.290 AUDIENCE: After the green flag is [INAUDIBLE].. 01:38:09.290 --> 01:38:10.140 DAVID J. MALAN: OK. 01:38:10.140 --> 01:38:10.640 OK. 01:38:10.640 --> 01:38:12.360 Interesting solution. 01:38:12.360 --> 01:38:17.930 So let me go ahead, and under Control let me grab a-- wait one second. 01:38:17.930 --> 01:38:21.360 I'm going to change the 1 to 5, and now I'm going to click the green flag. 01:38:21.360 --> 01:38:22.370 So here we go. 01:38:22.370 --> 01:38:28.490 1, 2, 3, 4, 5. 01:38:28.490 --> 01:38:29.060 Damn it. 01:38:29.060 --> 01:38:29.630 [MEOWING] 01:38:29.630 --> 01:38:29.810 OK. 01:38:29.810 --> 01:38:30.852 That was yours, not mine. 01:38:30.852 --> 01:38:32.240 [LAUGHTER] 01:38:32.240 --> 01:38:33.915 It didn't work. 01:38:33.915 --> 01:38:34.790 AUDIENCE: [INAUDIBLE] 01:38:34.790 --> 01:38:35.707 DAVID J. MALAN: Sorry? 01:38:35.707 --> 01:38:36.703 AUDIENCE: [INAUDIBLE] 01:38:36.703 --> 01:38:38.120 DAVID J. MALAN: Oh, maybe forever. 01:38:38.120 --> 01:38:42.230 So your approach would work, but it's very much a hack, if you will. 01:38:42.230 --> 01:38:46.700 Like, I would have to time it perfectly so that the mouse pointer is touching 01:38:46.700 --> 01:38:49.040 it, or, conversely, I have to drag it there and just 01:38:49.040 --> 01:38:50.540 leave it there for five seconds. 01:38:50.540 --> 01:38:53.040 And that's a little weird because you don't just touch a cat 01:38:53.040 --> 01:38:54.960 and then five seconds later it meows at you. 01:38:54.960 --> 01:38:57.190 Like, presumably, we want it to be more interactive. 01:38:57.190 --> 01:38:58.660 So I like this idea of a loop. 01:38:58.660 --> 01:38:59.160 Right? 01:38:59.160 --> 01:39:04.440 Why don't we just tell the cat to forever listen for the cursor 01:39:04.440 --> 01:39:07.170 as by using not repeat but forever? 01:39:07.170 --> 01:39:08.240 Let me move this in here. 01:39:08.240 --> 01:39:11.490 So now the cat's going to be told when the green flag is clicked just forever, 01:39:11.490 --> 01:39:13.073 if touching, if touching, if touching. 01:39:13.073 --> 01:39:15.270 Oh, meow when actually touched. 01:39:15.270 --> 01:39:19.368 So now if I zoom out and hit Play, nothing's happening. 01:39:19.368 --> 01:39:21.160 I don't have to wait any number of seconds. 01:39:21.160 --> 01:39:22.545 But when I do touch the cat-- 01:39:22.545 --> 01:39:23.260 [MEOWING] 01:39:23.260 --> 01:39:24.180 [APPLAUSE] 01:39:24.180 --> 01:39:26.765 [CHUCKLES] Fan section here. 01:39:26.765 --> 01:39:27.265 Thank you. 01:39:27.265 --> 01:39:27.765 [MEOWING] 01:39:27.765 --> 01:39:30.890 So now it's actually working quite well. 01:39:30.890 --> 01:39:33.220 So there we have sort of a logical bug. 01:39:33.220 --> 01:39:35.545 But it does make sense if you think about what it was 01:39:35.545 --> 01:39:38.020 you told the computer to actually do. 01:39:38.020 --> 01:39:39.915 Well, let's make things even more interesting 01:39:39.915 --> 01:39:41.290 by using one of these extensions. 01:39:41.290 --> 01:39:44.620 In this bottom left corner, this is how I got to text-to-speech earlier. 01:39:44.620 --> 01:39:47.470 Let me go to Video Sensing over here too. 01:39:47.470 --> 01:39:50.750 And I can actually-- there we go-- literally, the video has come on. 01:39:50.750 --> 01:39:52.060 Let me go ahead and do this. 01:39:52.060 --> 01:39:55.690 Get rid of this code, and let me step out of the frame. 01:39:55.690 --> 01:39:58.540 When video motion is greater than-- 01:39:58.540 --> 01:40:00.490 well, this is CS50, so let's just type in 50, 01:40:00.490 --> 01:40:02.150 which is just a measure of motion. 01:40:02.150 --> 01:40:07.370 Let me go and play sound meow. 01:40:07.370 --> 01:40:07.870 OK. 01:40:07.870 --> 01:40:11.720 And let me stop and restart. 01:40:11.720 --> 01:40:12.220 All right. 01:40:12.220 --> 01:40:13.030 So there's the cat. 01:40:18.270 --> 01:40:19.260 [MEOWING] 01:40:19.260 --> 01:40:20.040 OK. 01:40:20.040 --> 01:40:20.880 It worked. 01:40:20.880 --> 01:40:24.330 The cat's a little insensitive. 01:40:24.330 --> 01:40:25.190 [MEOWING] 01:40:25.190 --> 01:40:25.720 There we go. 01:40:25.720 --> 01:40:26.720 Actually, you know what? 01:40:26.720 --> 01:40:30.300 Maybe it's just-- let me put-- let's change it. 01:40:30.300 --> 01:40:31.130 20. 01:40:31.130 --> 01:40:31.940 Oh, my God. 01:40:31.940 --> 01:40:33.480 Oh, that's OK. 01:40:33.480 --> 01:40:33.980 [MEOWING] 01:40:33.980 --> 01:40:34.790 There we go. 01:40:34.790 --> 01:40:35.690 All right. 01:40:35.690 --> 01:40:36.420 There we go. 01:40:36.420 --> 01:40:40.038 So now it's a little more sensitive to the petting by using the camera. 01:40:40.038 --> 01:40:41.580 Now, this is kind of a weird example. 01:40:41.580 --> 01:40:43.160 And if I just start moving around crazily, like, 01:40:43.160 --> 01:40:45.320 it's just going to meow incessantly, which was what was happening-- 01:40:45.320 --> 01:40:46.430 [MEOWING] 01:40:46.430 --> 01:40:48.222 Stop. 01:40:48.222 --> 01:40:49.020 [LAUGHTER] 01:40:49.020 --> 01:40:49.520 OK. 01:40:49.520 --> 01:40:51.800 When in doubt, this is when you reload the page. 01:40:51.800 --> 01:40:53.010 [LAUGHTER] 01:40:53.010 --> 01:40:53.510 All right. 01:40:53.510 --> 01:40:55.730 So now we're back to where we wanted to be. 01:40:55.730 --> 01:40:58.220 But where can we now use these kinds of building blocks? 01:40:58.220 --> 01:41:00.540 Like, we were doing such tiny little programs. 01:41:00.540 --> 01:41:04.220 But even that we could turn into a whole game, I bet. 01:41:04.220 --> 01:41:07.700 Could we get like one volunteer to come on up? 01:41:07.700 --> 01:41:08.810 One volunteer? 01:41:08.810 --> 01:41:10.170 Everyone's looking down. 01:41:10.170 --> 01:41:10.670 OK. 01:41:10.670 --> 01:41:11.180 On the end here. 01:41:11.180 --> 01:41:11.722 Come on down. 01:41:11.722 --> 01:41:12.290 Yeah. 01:41:12.290 --> 01:41:12.790 All right. 01:41:12.790 --> 01:41:14.930 Round of applause for our one brave volunteer here. 01:41:14.930 --> 01:41:18.150 [APPLAUSE] 01:41:18.650 --> 01:41:19.400 All right. 01:41:19.400 --> 01:41:22.020 These Super Mario cookies are on the line for you, 01:41:22.020 --> 01:41:23.160 depending on how this goes. 01:41:23.160 --> 01:41:24.827 So I'm going to have you come over here. 01:41:24.827 --> 01:41:28.940 And in advance on Scratch's website, we have some pre-made games, 01:41:28.940 --> 01:41:32.930 one of them written by one of your predecessors, a former student, 01:41:32.930 --> 01:41:36.050 that they implemented this sort of "Whac-A-Mole" game. 01:41:36.050 --> 01:41:39.290 So what you're about to see is the camera turn on on you. 01:41:39.290 --> 01:41:43.160 And you're going to see four moles above, below, left, and right. 01:41:43.160 --> 01:41:44.720 And using only your head-- 01:41:44.720 --> 01:41:47.930 up, down, left, right-- the goal is to whack a mole 01:41:47.930 --> 01:41:51.860 to get a point every time your head touches one of these sprites. 01:41:51.860 --> 01:41:54.740 So you're about to see things get very interesting very quickly. 01:41:54.740 --> 01:41:57.050 But using these building blocks, just those simple 01:41:57.050 --> 01:42:01.350 blocks but have four sprites, not four cats but four moles in this case. 01:42:01.350 --> 01:42:03.955 We can actually turn these into actual games. 01:42:03.955 --> 01:42:04.580 [MUSIC PLAYING] 01:42:04.580 --> 01:42:06.950 So here we go. 01:42:06.950 --> 01:42:08.360 Click Beginner. 01:42:08.360 --> 01:42:08.900 OK. 01:42:08.900 --> 01:42:10.858 And we just need you to center your head first. 01:42:10.858 --> 01:42:13.808 [MUSIC PLAYING] 01:42:20.152 --> 01:42:23.080 [INDISTINCT CHATTER] 01:42:24.056 --> 01:42:25.032 [CHUCKLES] 01:42:25.032 --> 01:42:28.448 [MUSIC PLAYING] 01:42:30.920 --> 01:42:31.820 Nice. 01:42:31.820 --> 01:42:32.630 Ten seconds. 01:42:32.630 --> 01:42:36.004 [MUSIC PLAYING] 01:42:39.390 --> 01:42:40.470 Nice. 01:42:40.470 --> 01:42:42.486 Two seconds. 01:42:42.486 --> 01:42:43.290 AUDIENCE: [LAUGHS] 01:42:43.290 --> 01:42:43.770 DAVID J. MALAN: All right. 01:42:43.770 --> 01:42:44.610 A round of applause. 01:42:44.610 --> 01:42:45.906 [APPLAUSE] 01:42:45.906 --> 01:42:48.100 Thank you. 01:42:48.100 --> 01:42:50.380 You want to introduce yourself? 01:42:50.380 --> 01:42:51.380 AUDIENCE: Hi, everybody. 01:42:51.380 --> 01:42:53.890 My name is [? Vanilla. ?] I'm a first year, 01:42:53.890 --> 01:42:56.955 and I'm going to be majoring in computer science and economics. 01:42:56.955 --> 01:42:58.330 DAVID J. MALAN: Nice to meet you. 01:42:58.330 --> 01:42:58.830 Here we go. 01:42:58.830 --> 01:42:59.435 Thank you. 01:42:59.435 --> 01:43:00.560 AUDIENCE: Nice to meet you. 01:43:00.560 --> 01:43:01.393 [APPLAUSE, CHEERING] 01:43:01.393 --> 01:43:04.210 DAVID J. MALAN: So we won't look at the code for that actual game. 01:43:04.210 --> 01:43:05.740 It was written by one of your predecessors. 01:43:05.740 --> 01:43:07.573 And you can see it online if you're curious. 01:43:07.573 --> 01:43:10.780 But you can think about now with our functions, conditionals, 01:43:10.780 --> 01:43:15.010 Boolean expressions, loops how you could kind of compose that kind of program. 01:43:15.010 --> 01:43:17.770 So odds are there was a loop that was just constantly listening 01:43:17.770 --> 01:43:19.690 for that kind of connectivity, or it was one 01:43:19.690 --> 01:43:22.900 of those extensions that was waiting for motion to go 01:43:22.900 --> 01:43:24.180 touch one of those sprites. 01:43:24.180 --> 01:43:25.930 Notice that there's these numbers up here. 01:43:25.930 --> 01:43:27.040 And we haven't talked about this yet. 01:43:27.040 --> 01:43:30.040 But just like in math, where you can have variables x, y, and z, 01:43:30.040 --> 01:43:33.220 in programming, you can create variables using other puzzle pieces 01:43:33.220 --> 01:43:36.460 in Scratch that just keep track of how many seconds are left, 01:43:36.460 --> 01:43:40.310 that keeps track of how many times her head hit one of the moles. 01:43:40.310 --> 01:43:43.760 And so you can implement the mechanics of games using very simple building 01:43:43.760 --> 01:43:44.260 blocks. 01:43:44.260 --> 01:43:46.240 But how do you go about building something 01:43:46.240 --> 01:43:48.600 that's more interesting and interactive like that? 01:43:48.600 --> 01:43:50.760 Let me go ahead and bring up, for instance, 01:43:50.760 --> 01:43:52.930 one of the very first things I wrote years ago 01:43:52.930 --> 01:43:55.430 when I was in graduate school and Scratch had just come out. 01:43:55.430 --> 01:43:57.380 I was taking a class at MIT's Media Lab. 01:43:57.380 --> 01:44:01.520 And they invited us to beta test-- that is, try out the software before it then 01:44:01.520 --> 01:44:03.140 became part of the world. 01:44:03.140 --> 01:44:07.200 And the game I made was this one here. 01:44:07.200 --> 01:44:10.640 Let me find the right version of "Oscartime." 01:44:10.640 --> 01:44:15.673 So "Oscartime" is a game that took me tens of hours probably in the end. 01:44:15.673 --> 01:44:18.590 It was a little bit addictive, but let me go ahead and full-screen it. 01:44:18.590 --> 01:44:20.270 And I won't play the whole game. 01:44:20.270 --> 01:44:23.995 But it looked a little something like this. 01:44:23.995 --> 01:44:25.120 OSCAR: (SINGS) Oh, I love-- 01:44:25.120 --> 01:44:28.220 DAVID J. MALAN: Where trash is falling from the sky. 01:44:28.220 --> 01:44:30.070 And I can click on it and drag it. 01:44:30.070 --> 01:44:33.430 And notice as I get close, the lid opens up like this. 01:44:33.430 --> 01:44:37.450 And if I let it keep falling, it goes in, and Oscar gives me a point. 01:44:37.450 --> 01:44:39.585 And I can do this again. 01:44:39.585 --> 01:44:41.710 OSCAR: If you really want to see something trashy-- 01:44:41.710 --> 01:44:41.860 DAVID J. MALAN: All right. 01:44:41.860 --> 01:44:42.943 Here comes something else. 01:44:42.943 --> 01:44:44.565 OSCAR: I have here a sneaker that-- 01:44:44.565 --> 01:44:46.690 DAVID J. MALAN: So now there's two pieces of trash. 01:44:46.690 --> 01:44:49.787 OSCAR: It's all full of holes, and the laces are-- 01:44:49.787 --> 01:44:52.370 DAVID J. MALAN: And it just keeps going, and going, and going. 01:44:52.370 --> 01:44:55.537 And if we can lower the volume for just a moment, we'll let more trash fall. 01:44:55.537 --> 01:44:58.300 But to your comment earlier about one sprite or more 01:44:58.300 --> 01:45:00.040 sprites, that's what we're seeing here. 01:45:00.040 --> 01:45:03.190 Even though our examples thus far are just one cat, one or two puzzle 01:45:03.190 --> 01:45:04.660 pieces, or a few puzzle pieces. 01:45:04.660 --> 01:45:06.670 Here is, I claim, a sprite. 01:45:06.670 --> 01:45:08.110 Here is another sprite. 01:45:08.110 --> 01:45:09.340 Here is another sprite. 01:45:09.340 --> 01:45:11.950 And by toggling among them in that bottom right-hand corner, 01:45:11.950 --> 01:45:15.460 I can just associate different puzzle pieces with each of these sprites. 01:45:15.460 --> 01:45:17.987 Now, I didn't start off by implementing this whole game. 01:45:17.987 --> 01:45:20.570 And in just a moment, if we can raise the volume a little bit, 01:45:20.570 --> 01:45:22.400 we'll see even more trash is falling. 01:45:22.400 --> 01:45:23.300 So this is what-- 01:45:23.300 --> 01:45:26.930 I hate this song now, like 10 plus hours listening to this song on loop 01:45:26.930 --> 01:45:28.160 just to get the timing right. 01:45:28.160 --> 01:45:31.040 But it brings to life all of these different sprites. 01:45:31.040 --> 01:45:34.130 And if you play it again and again, it's always a little bit different 01:45:34.130 --> 01:45:35.600 because I'm using some randomness. 01:45:35.600 --> 01:45:38.510 So this newspaper doesn't always fall from there. 01:45:38.510 --> 01:45:39.650 Sometimes it's here. 01:45:39.650 --> 01:45:40.680 Sometimes it's here. 01:45:40.680 --> 01:45:42.920 And so here, again, we have mechanics of a game where 01:45:42.920 --> 01:45:45.770 things are being perturbed a little, randomized a little bit 01:45:45.770 --> 01:45:47.772 to keep things more interesting. 01:45:47.772 --> 01:45:48.605 And let me go ahead. 01:45:48.605 --> 01:45:50.240 OSCAR: (SINGS) I love trash. 01:45:50.240 --> 01:45:51.407 DAVID J. MALAN: There we go. 01:45:51.407 --> 01:45:53.420 How. about raise a little volume? 01:45:53.420 --> 01:45:54.890 One more piece of trash. 01:45:54.890 --> 01:45:55.490 So a clock. 01:45:55.490 --> 01:45:57.600 It just goes on forever, this kind of game. 01:45:57.600 --> 01:45:59.010 But let's go ahead and consider. 01:45:59.010 --> 01:46:00.060 Let me close that. 01:46:00.060 --> 01:46:01.610 Let me go ahead and consider. 01:46:01.610 --> 01:46:04.770 How I went about implementing that from the get-go. 01:46:04.770 --> 01:46:06.920 So I will stipulate-- 01:46:06.920 --> 01:46:11.090 let me open a few of these versions here-- that the very first thing I did 01:46:11.090 --> 01:46:13.070 was pretty much just implement the stage. 01:46:13.070 --> 01:46:13.220 Right? 01:46:13.220 --> 01:46:15.262 I was kind of procrastinating, so I poked around. 01:46:15.262 --> 01:46:18.390 I found the Sesame Street lamppost, and I dragged it into the world. 01:46:18.390 --> 01:46:18.890 And done. 01:46:18.890 --> 01:46:19.820 Version one is done. 01:46:19.820 --> 01:46:23.510 It didn't do anything, but at least I had the world sort of laid out. 01:46:23.510 --> 01:46:25.820 That is to say I took a baby step toward my goal. 01:46:25.820 --> 01:46:27.528 Then I started thinking about, all right, 01:46:27.528 --> 01:46:31.620 how do I bring the trash to life, even if it doesn't do much of anything else? 01:46:31.620 --> 01:46:34.370 And so I created another sprite by clicking the appropriate button 01:46:34.370 --> 01:46:35.840 in that bottom right-hand corner. 01:46:35.840 --> 01:46:38.420 And I thought about, well, what do I want this trash to do? 01:46:38.420 --> 01:46:40.860 I want it to just kind of fall from the sky. 01:46:40.860 --> 01:46:44.690 And so what I did here was the following. 01:46:44.690 --> 01:46:48.020 If I go to this trash piece here or-- actually, sorry. 01:46:48.020 --> 01:46:48.780 Out of order. 01:46:48.780 --> 01:46:51.860 What I actually did first was I didn't even have the trash fall. 01:46:51.860 --> 01:46:55.280 If I play this game, the trash just stays there in the air. 01:46:55.280 --> 01:46:57.480 But I can do this. 01:46:57.480 --> 01:47:01.370 I can drag it, and as before, as I touch the other sprite, 01:47:01.370 --> 01:47:03.090 I get the trash can lid to go up. 01:47:03.090 --> 01:47:04.080 So how do I do that? 01:47:04.080 --> 01:47:07.130 Well, let me click on Oscar down there, my first sprite. 01:47:07.130 --> 01:47:10.580 And here are the puzzle pieces via which I implemented this idea. 01:47:10.580 --> 01:47:13.340 I changed Oscar's costume, his appearance, 01:47:13.340 --> 01:47:17.960 to be just number one, which was one of the images I imported into the program. 01:47:17.960 --> 01:47:19.820 And then I forever did this. 01:47:19.820 --> 01:47:22.460 If Oscar is touching the mouse pointer, then 01:47:22.460 --> 01:47:26.390 change Oscar's costume to number two, otherwise change it back to one. 01:47:26.390 --> 01:47:28.190 So it's super simple animation. 01:47:28.190 --> 01:47:31.970 I'm just toggling between lid up, lid down, lid up, lid down, 01:47:31.970 --> 01:47:33.710 but it kind of feels interactive. 01:47:33.710 --> 01:47:35.810 And if I wanted to really make this pretty, 01:47:35.810 --> 01:47:40.580 I could have 30 different costumes where the lid is ever so slightly higher. 01:47:40.580 --> 01:47:44.120 Then it would look even more like a movie or fluid motion. 01:47:44.120 --> 01:47:46.640 But this was enough to get the job done, which 01:47:46.640 --> 01:47:49.700 is to say I didn't try to implement all of "Oscartime" together. 01:47:49.700 --> 01:47:52.490 I just took a second baby step toward my goal. 01:47:52.490 --> 01:47:54.950 And then my next version of "Oscartime" might 01:47:54.950 --> 01:47:57.675 have looked a little something like this, where now the trash-- 01:47:57.675 --> 01:47:58.800 there's more going on here. 01:47:58.800 --> 01:48:02.300 Let's look at two of these blocks of code. 01:48:02.300 --> 01:48:04.972 The first thing I did was I enabled drag mode to draggable, 01:48:04.972 --> 01:48:07.430 and I had to Google to figure this out because otherwise it 01:48:07.430 --> 01:48:09.890 didn't let me drag the trash while playing the game. 01:48:09.890 --> 01:48:12.800 But once I figured that out, I tell the trash 01:48:12.800 --> 01:48:16.700 to go to a random x-coordinate between 0 and 240 01:48:16.700 --> 01:48:19.670 from left to right and then the y location 180 01:48:19.670 --> 01:48:22.070 because I always want the trash falling from the sky. 01:48:22.070 --> 01:48:23.270 And then what do I do? 01:48:23.270 --> 01:48:27.530 I told the trash to forever change its y-coordinate, its vertical coordinate, 01:48:27.530 --> 01:48:30.740 by negative 1, negative 1, negative 1, one pixel 01:48:30.740 --> 01:48:34.190 at a time, which creates the illusion of it falling from the sky. 01:48:34.190 --> 01:48:36.950 But I needed to do one other thing, and let me scroll up. 01:48:36.950 --> 01:48:41.360 Each of your sprites can have multiple programs, multiple scripts, 01:48:41.360 --> 01:48:44.070 so to speak, that are not attached to one another. 01:48:44.070 --> 01:48:46.050 They will happen in parallel for you. 01:48:46.050 --> 01:48:47.750 The second one is saying this. 01:48:47.750 --> 01:48:52.850 Forever if the trash is touching Oscar, what should it do? 01:48:52.850 --> 01:48:57.440 Go to a completely different x and y location at the very top. 01:48:57.440 --> 01:48:57.950 Why? 01:48:57.950 --> 01:49:01.010 Well, as soon as I drag the trash over the Oscar, 01:49:01.010 --> 01:49:03.530 I want it to disappear as though it's going into the can. 01:49:03.530 --> 01:49:06.830 And I then want it to reappear at the top so more trash falls. 01:49:06.830 --> 01:49:08.840 So I just thought about what would it mean 01:49:08.840 --> 01:49:10.910 for the trash to go into the trash can. 01:49:10.910 --> 01:49:11.870 Well, who cares? 01:49:11.870 --> 01:49:14.210 What really matters to the human user is that it just 01:49:14.210 --> 01:49:16.980 disappears and teleports elsewhere. 01:49:16.980 --> 01:49:19.620 And so that's the idea I implemented here. 01:49:19.620 --> 01:49:24.960 So if you can reduce intuitive ideas to just basic building blocks like this, 01:49:24.960 --> 01:49:27.440 you can start to make things much more interactive. 01:49:27.440 --> 01:49:29.970 And lastly, if I look at this version here, 01:49:29.970 --> 01:49:33.050 you'll see that we've combined these. 01:49:33.050 --> 01:49:35.930 And so indeed, if I actually go ahead and play this now, 01:49:35.930 --> 01:49:37.790 not only is it falling. 01:49:37.790 --> 01:49:41.090 I can let it fall right on top of Oscar and watch it disappear. 01:49:41.090 --> 01:49:44.795 But notice Oscar doesn't pop out yet because that was the fourth version 01:49:44.795 --> 01:49:45.920 and then the fifth version. 01:49:45.920 --> 01:49:48.650 And then I added the annoying music and so forth but sort of 01:49:48.650 --> 01:49:51.600 composed this program step by step by step 01:49:51.600 --> 01:49:53.193 so as to accomplish my larger goal. 01:49:53.193 --> 01:49:55.610 And this is going to be true of all of the code you write, 01:49:55.610 --> 01:50:00.980 be it in Scratch, or C, or Python, or in the like, trying to come up with-- 01:50:00.980 --> 01:50:05.360 or rather, trying to reduce your ideas, your grand vision to just baby steps, 01:50:05.360 --> 01:50:07.670 building blocks so that you start with version one, 01:50:07.670 --> 01:50:10.010 and maybe you submit version 10 or 20. 01:50:10.010 --> 01:50:13.190 But you don't try to implement version 10 or 20 at the get-go. 01:50:13.190 --> 01:50:15.780 You take those incremental steps. 01:50:15.780 --> 01:50:16.280 All right. 01:50:16.280 --> 01:50:17.270 How about one other? 01:50:17.270 --> 01:50:19.550 Well, let me propose this. 01:50:19.550 --> 01:50:23.370 Let me go ahead and open three games that represent one 01:50:23.370 --> 01:50:27.570 that your predecessors also implemented, which looks a little something 01:50:27.570 --> 01:50:29.220 like this in version zero. 01:50:29.220 --> 01:50:32.220 Suppose I wanted to implement a game that 01:50:32.220 --> 01:50:33.900 simply has these kinds of mechanics. 01:50:33.900 --> 01:50:37.050 I'm touching my arrow keys on my keyboard-- up, down, left, and right. 01:50:37.050 --> 01:50:38.460 And I'm moving the Harvard logo. 01:50:38.460 --> 01:50:39.610 Let me zoom in a bit. 01:50:39.610 --> 01:50:42.210 So if I hit the up arrow, the Harvard shield goes up. 01:50:42.210 --> 01:50:44.760 If I hit the down arrow, the shield goes down. 01:50:44.760 --> 01:50:47.070 If I go all the way to the left, it goes left 01:50:47.070 --> 01:50:49.548 until it hits the wall and same thing on the right. 01:50:49.548 --> 01:50:51.840 So this is like the beginnings of a game or a beginning 01:50:51.840 --> 01:50:53.370 of a maze, something like that. 01:50:53.370 --> 01:50:55.150 Well, how might I implement this? 01:50:55.150 --> 01:50:57.210 Well, let me look inside this one. 01:50:57.210 --> 01:51:01.020 And there's a lot going on, but, again, I sort of took simple steps. 01:51:01.020 --> 01:51:02.730 Notice that I've got three sprites-- 01:51:02.730 --> 01:51:04.920 a left wall, which is just a straight line, 01:51:04.920 --> 01:51:07.140 the right wall, which is a straight line. 01:51:07.140 --> 01:51:10.710 And just intuitively, why did I implement those as sprites? 01:51:10.710 --> 01:51:15.240 Why do they need to exist as entities themselves? 01:51:15.240 --> 01:51:16.230 Yeah, in front. 01:51:16.230 --> 01:51:16.530 AUDIENCE: [INAUDIBLE] 01:51:16.530 --> 01:51:17.405 DAVID J. MALAN: Yeah. 01:51:17.405 --> 01:51:19.033 I want it to interact with the shield. 01:51:19.033 --> 01:51:21.450 So I need to be able to ask that Boolean expression, like, 01:51:21.450 --> 01:51:24.940 not touching mouse pointer but touching shield, for instance, 01:51:24.940 --> 01:51:26.560 a different type of yes/no question. 01:51:26.560 --> 01:51:29.130 And so what is the code for the shield actually doing? 01:51:29.130 --> 01:51:32.850 Well, there's a lot of duplication, and let me focus on the abstraction first. 01:51:32.850 --> 01:51:35.680 Notice that I've got this one green flag clicked. 01:51:35.680 --> 01:51:38.430 I want the shield to go dead center, 0 comma 0, 01:51:38.430 --> 01:51:42.600 and then forever listen for the human's keyboard, and feel for the wall. 01:51:42.600 --> 01:51:45.815 So I decided to implement my own pink puzzle pieces that 01:51:45.815 --> 01:51:47.190 implement the two separate ideas. 01:51:47.190 --> 01:51:49.450 One, just listen for the keyboard-- up, down, left, 01:51:49.450 --> 01:51:50.700 right-- and then do something. 01:51:50.700 --> 01:51:53.770 And then feel for walls is this idea of whenever I go up, 01:51:53.770 --> 01:51:56.550 down, left, or right, if I touch a wall, I 01:51:56.550 --> 01:52:00.600 need to stop doing whatever the keystrokes are telling me to do. 01:52:00.600 --> 01:52:05.580 So now if we dive into those implementation details, listen and feel 01:52:05.580 --> 01:52:07.930 are abstractions, custom puzzle pieces. 01:52:07.930 --> 01:52:09.870 Let's look at the implementation details. 01:52:09.870 --> 01:52:11.220 Well, here's the keyboard. 01:52:11.220 --> 01:52:15.840 If the key arrow up is pressed, change y by 1. 01:52:15.840 --> 01:52:19.495 If key down arrow is pressed, change y by negative 1. 01:52:19.495 --> 01:52:21.370 And you can probably see where this is going. 01:52:21.370 --> 01:52:23.310 Right arrow is x by 1. 01:52:23.310 --> 01:52:25.920 Left arrow is x by negative 1, and that's 01:52:25.920 --> 01:52:29.050 sort of all that's involved with up, down, left, right. 01:52:29.050 --> 01:52:30.030 But wait a minute. 01:52:30.030 --> 01:52:35.910 Why is there no loop in this listen for keyboard puzzle piece? 01:52:35.910 --> 01:52:39.240 I needed a loop last time so it constantly works. 01:52:39.240 --> 01:52:40.770 AUDIENCE: [INAUDIBLE] 01:52:40.770 --> 01:52:41.770 DAVID J. MALAN: Exactly. 01:52:41.770 --> 01:52:44.350 I put the loop in my main part of my program 01:52:44.350 --> 01:52:48.020 up top so something is telling me to keep listening again and again. 01:52:48.020 --> 01:52:50.020 So it's got to be somewhere, but it doesn't have 01:52:50.020 --> 01:52:51.760 to be in the actual implementation. 01:52:51.760 --> 01:52:53.950 And lastly, how about this feel for walls? 01:52:53.950 --> 01:52:58.420 Well, if touching left wall, which is just another sprite, change x by 1. 01:52:58.420 --> 01:53:00.880 So that is to say if I'm touching the left wall, 01:53:00.880 --> 01:53:03.465 I want to bounce it back the other direction 01:53:03.465 --> 01:53:04.840 and same thing on the right wall. 01:53:04.840 --> 01:53:06.580 If I'm touching the right wall, I want to bounce it 01:53:06.580 --> 01:53:09.670 back to the left, which effectively means that even if the human's hitting 01:53:09.670 --> 01:53:11.680 the key, it's like fighting with this code, 01:53:11.680 --> 01:53:14.890 but it's never going to go through the wall based on that math. 01:53:14.890 --> 01:53:16.760 It's going to stop it right there. 01:53:16.760 --> 01:53:17.260 All right. 01:53:17.260 --> 01:53:18.760 Let's add something else to the mix. 01:53:18.760 --> 01:53:21.640 Suppose I want the game to change to be a little something like this, 01:53:21.640 --> 01:53:25.300 where Yale is some kind of block in between me and the exit. 01:53:25.300 --> 01:53:27.140 So some dramatic race here. 01:53:27.140 --> 01:53:27.640 OK. 01:53:27.640 --> 01:53:30.100 I just got by, but the Yale logo doesn't seem 01:53:30.100 --> 01:53:32.200 to be doing all that much except bouncing. 01:53:32.200 --> 01:53:35.660 So I'm guessing there's a loop, maybe a conditional checking for those walls 01:53:35.660 --> 01:53:36.160 too. 01:53:36.160 --> 01:53:38.823 So let's go ahead and zoom out, see inside. 01:53:38.823 --> 01:53:41.490 Let's not worry about Harvard because it's pretty much the same. 01:53:41.490 --> 01:53:43.520 Let's look at the Yale puzzle pieces. 01:53:43.520 --> 01:53:45.710 And sure enough, go to the middle. 01:53:45.710 --> 01:53:46.880 0 comma zero. 01:53:46.880 --> 01:53:50.930 Point in direction 90, so point horizontally on the grid. 01:53:50.930 --> 01:53:53.870 And then if touching left wall or touching right wall-- 01:53:53.870 --> 01:53:58.470 I'm kind of cheating this time, but cleverly, just spin around and do 180 01:53:58.470 --> 01:54:00.198 so you effectively bounce off the wall. 01:54:00.198 --> 01:54:01.490 This just tightened up my code. 01:54:01.490 --> 01:54:03.490 I don't need to do the negative 1 or the plus 1. 01:54:03.490 --> 01:54:07.010 I just say bounce in this form of code, otherwise just 01:54:07.010 --> 01:54:08.420 constantly move one step. 01:54:08.420 --> 01:54:12.080 Now, if this is a game where Yale is supposed to be better and faster, 01:54:12.080 --> 01:54:13.520 well, let's change the 1 to 5. 01:54:13.520 --> 01:54:15.300 Move 5 pixels at a time. 01:54:15.300 --> 01:54:17.610 Let's move it 10 back and forth. 01:54:17.610 --> 01:54:19.040 Let's maybe 100. 01:54:19.040 --> 01:54:20.150 Uh-oh. 01:54:20.150 --> 01:54:21.380 So what just happened? 01:54:21.380 --> 01:54:25.310 That is a bug, which we can avoid by just not doing that. 01:54:25.310 --> 01:54:27.310 But why did it break out of the wall? 01:54:27.310 --> 01:54:28.310 Yeah. 01:54:28.310 --> 01:54:29.600 AUDIENCE: [INAUDIBLE]. 01:54:29.600 --> 01:54:30.890 At first it was [INAUDIBLE]. 01:54:30.890 --> 01:54:31.890 DAVID J. MALAN: Exactly. 01:54:31.890 --> 01:54:34.010 Because I'm doing 100 steps at a time, I'm 01:54:34.010 --> 01:54:36.200 never actually touching the other sprite because I'm 01:54:36.200 --> 01:54:38.060 sort of stepping way over it. 01:54:38.060 --> 01:54:40.610 So there's never a moment where they're actually touching. 01:54:40.610 --> 01:54:44.630 So previously, I was just getting lucky by doing fewer steps because it's 01:54:44.630 --> 01:54:46.700 gradually going over the wall, which gives me 01:54:46.700 --> 01:54:48.557 just enough time to detect as much. 01:54:48.557 --> 01:54:50.390 So I would have to kind of tinker, and he'll 01:54:50.390 --> 01:54:51.932 handle this a little bit differently. 01:54:51.932 --> 01:54:53.790 So it's a bug if it's too fast. 01:54:53.790 --> 01:54:55.730 But at least if I keep it slow and reasonable 01:54:55.730 --> 01:54:59.870 the math actually does work out, so long as it starts again in the middle. 01:54:59.870 --> 01:55:04.200 Well, let's do one final flourish here, whereby let's bring MIT into the mix. 01:55:04.200 --> 01:55:04.700 Right? 01:55:04.700 --> 01:55:06.770 They're super smart, so maybe they can kind of 01:55:06.770 --> 01:55:11.390 track us and follow wherever I'm going. 01:55:11.390 --> 01:55:13.140 So how might this work? 01:55:13.140 --> 01:55:13.640 All right. 01:55:13.640 --> 01:55:16.580 So nothing happens yet because we haven't finished composing the game. 01:55:16.580 --> 01:55:17.490 And notice here-- 01:55:17.490 --> 01:55:17.990 OK. 01:55:17.990 --> 01:55:19.063 Now MIT is struggling. 01:55:19.063 --> 01:55:21.980 It's kind of twitching there because it's going just above, and below, 01:55:21.980 --> 01:55:23.100 and then above, and below. 01:55:23.100 --> 01:55:24.840 So we could fix that too if we want. 01:55:24.840 --> 01:55:28.080 But that's just a function of my math, one pixel at a time. 01:55:28.080 --> 01:55:33.570 Let me open up this one, see inside, and click on MIT. 01:55:33.570 --> 01:55:37.110 And it doesn't take much to implement MIT, it seems. 01:55:37.110 --> 01:55:43.200 So go to random position, forever point towards the Harvard logo outline, 01:55:43.200 --> 01:55:46.330 AKA the shield, and then move one step. 01:55:46.330 --> 01:55:50.705 So if I wanted to make MIT even smarter, even faster, what do I change here? 01:55:50.705 --> 01:55:51.580 AUDIENCE: [INAUDIBLE] 01:55:51.580 --> 01:55:52.455 DAVID J. MALAN: Yeah. 01:55:52.455 --> 01:55:56.070 Change one step to two steps to double their speed or five steps, 10 steps, 01:55:56.070 --> 01:55:56.612 or 100 steps. 01:55:56.612 --> 01:55:58.403 And the game is going to be over like that. 01:55:58.403 --> 01:56:00.940 But that's all it takes to now make these kinds of elements. 01:56:00.940 --> 01:56:03.330 So if you are a game player on your phone, or consoles, 01:56:03.330 --> 01:56:06.120 or computer, or whatever, if you think about almost any game, 01:56:06.120 --> 01:56:08.910 you can probably now start to think about how they implemented 01:56:08.910 --> 01:56:12.210 those mechanics because it's just being reduced to functions, conditionals, 01:56:12.210 --> 01:56:15.640 loops, variables, and the like in this case. 01:56:15.640 --> 01:56:20.560 So let's go ahead here and have maybe one final volunteer. 01:56:20.560 --> 01:56:22.480 We've got one more bag of Oreos here. 01:56:22.480 --> 01:56:22.980 OK. 01:56:22.980 --> 01:56:23.640 That was super fast. 01:56:23.640 --> 01:56:24.750 Do you want to come on up? 01:56:24.750 --> 01:56:25.250 All right. 01:56:25.250 --> 01:56:25.920 Brave volunteer. 01:56:25.920 --> 01:56:26.820 Come on up. 01:56:26.820 --> 01:56:30.271 [APPLAUSE] 01:56:31.180 --> 01:56:31.680 All right. 01:56:31.680 --> 01:56:36.720 Let me find the full-fledged version of this that one of your predecessors 01:56:36.720 --> 01:56:37.650 made. 01:56:37.650 --> 01:56:40.490 And let me get the right one. 01:56:40.490 --> 01:56:40.990 OK. 01:56:40.990 --> 01:56:41.740 Here we go. 01:56:41.740 --> 01:56:45.030 We'll see some instructions on the screen in just a moment. 01:56:45.030 --> 01:56:50.460 And when we hit Play, you'll see that the mechanics are all combined now 01:56:50.460 --> 01:56:51.630 into one full-fledged game. 01:56:51.630 --> 01:56:54.740 But first, an introduction. 01:56:54.740 --> 01:56:55.890 It's on. 01:56:55.890 --> 01:56:56.750 SAM: Hi, everyone. 01:56:56.750 --> 01:56:57.780 I'm Sam. 01:56:57.780 --> 01:56:58.830 I live in [INAUDIBLE]. 01:56:58.830 --> 01:57:00.657 I'm a freshman, and I'm from Nepal. 01:57:00.657 --> 01:57:01.740 DAVID J. MALAN: All right. 01:57:01.740 --> 01:57:02.655 Welcome to the stage. 01:57:02.655 --> 01:57:03.155 [APPLAUSE] 01:57:03.155 --> 01:57:04.287 SAM: Thank you. 01:57:04.287 --> 01:57:05.370 DAVID J. MALAN: All right. 01:57:05.370 --> 01:57:05.980 So here we go. 01:57:05.980 --> 01:57:06.480 Yep. 01:57:06.480 --> 01:57:08.010 Go ahead and click the green flag. 01:57:08.010 --> 01:57:10.435 [MUSIC PLAYING] 01:57:10.435 --> 01:57:14.010 [MC HAMMER, "U CAN'T TOUCH THIS"] You can't touch this. 01:57:14.010 --> 01:57:17.982 DAVID J. MALAN: You see the grid is just bigger this time. 01:57:17.982 --> 01:57:19.395 (SINGING) You can't touch this. 01:57:19.395 --> 01:57:20.270 DAVID J. MALAN: Nice. 01:57:20.270 --> 01:57:22.227 Now there's that Yale element. 01:57:22.227 --> 01:57:24.080 (SINGING) You can't touch this. 01:57:24.080 --> 01:57:27.770 My, my, my my music hits me so hard. 01:57:27.770 --> 01:57:29.570 Makes me say, oh, my Lord. 01:57:29.570 --> 01:57:32.518 Thank you for blessing me with a mind to rhyme and two hyped feet. 01:57:32.518 --> 01:57:35.810 It feels good when you know you're down, a super dope homeboy from the Oaktown. 01:57:35.810 --> 01:57:36.602 DAVID J. MALAN: OK. 01:57:36.602 --> 01:57:38.810 Third Yale. 01:57:38.810 --> 01:57:41.480 All started at slightly different positions. 01:57:41.480 --> 01:57:43.035 (SINGING) I told you, homeboy. 01:57:43.035 --> 01:57:43.910 You can't touch this. 01:57:43.910 --> 01:57:44.660 DAVID J. MALAN: Nice. 01:57:44.660 --> 01:57:45.160 All right. 01:57:45.160 --> 01:57:45.830 There's MIT. 01:57:45.830 --> 01:57:47.780 (SINGING) And ya know you can't touch this. 01:57:47.780 --> 01:57:49.139 [CHEERING, APPLAUSE] 01:57:49.139 --> 01:57:50.500 DAVID J. MALAN: Oh. 01:57:50.500 --> 01:57:52.520 (SINGING) You can't touch this. 01:57:52.520 --> 01:57:53.020 Yo. 01:57:53.020 --> 01:57:54.245 Let me bust the funky lyrics. 01:57:54.245 --> 01:57:55.495 DAVID J. MALAN: Gotta go fast. 01:57:58.000 --> 01:57:59.930 Oh. 01:57:59.930 --> 01:58:00.430 No. 01:58:05.030 --> 01:58:07.090 Oh. 01:58:07.090 --> 01:58:07.590 [CHUCKLES] 01:58:07.590 --> 01:58:09.465 (SINGING) Cold on a mission, so fall on back. 01:58:09.465 --> 01:58:13.690 Let them know that you're too much, and this is a beat they can't touch. 01:58:13.690 --> 01:58:14.590 DAVID J. MALAN: Nice. 01:58:14.590 --> 01:58:15.970 [EXCLAIMING] 01:58:15.970 --> 01:58:18.270 (SINGING) You can't touch this. 01:58:18.270 --> 01:58:20.482 DAVID J. MALAN: No more walls but two MITs. 01:58:20.482 --> 01:58:21.690 (SINGING) Yo, sound the bell. 01:58:21.690 --> 01:58:22.765 School's in, sucker. 01:58:22.765 --> 01:58:24.015 DAVID J. MALAN: Princeton now. 01:58:24.015 --> 01:58:25.612 (SINGING) Give me a song or rhythm. 01:58:25.612 --> 01:58:27.570 Making them sweat, that's what I'm giving them. 01:58:27.570 --> 01:58:31.140 Now, they know you talk about the Hammer when you're talkin' 01:58:31.140 --> 01:58:32.670 'bout a show that's hyped and tight. 01:58:32.670 --> 01:58:36.463 Singers are sweatin', so pass them a wipe or a tape to learn. 01:58:36.463 --> 01:58:38.005 DAVID J. MALAN: Second to last level. 01:58:38.005 --> 01:58:39.800 (SINGING) The chart's legit. 01:58:39.800 --> 01:58:41.980 Either work hard, or you might as well quit. 01:58:41.980 --> 01:58:45.540 That's word because you know you can't touch this. 01:58:48.265 --> 01:58:49.140 You can't touch this. 01:58:51.870 --> 01:58:52.600 Break it down. 01:58:52.600 --> 01:58:53.683 DAVID J. MALAN: All right. 01:58:53.683 --> 01:58:54.660 Clear. 01:58:54.660 --> 01:58:55.160 There we go. 01:58:55.160 --> 01:58:55.660 Nice. 01:58:55.660 --> 01:58:56.310 Oh, oh. 01:58:59.565 --> 01:59:00.960 Oh. 01:59:00.960 --> 01:59:02.814 Few more lives. 01:59:02.814 --> 01:59:05.590 Oh. 01:59:05.590 --> 01:59:06.280 (SINGING) Stop. 01:59:06.280 --> 01:59:06.700 Hammer time. 01:59:06.700 --> 01:59:07.130 DAVID J. MALAN: Here we go. 01:59:07.130 --> 01:59:08.110 There we go. 01:59:08.110 --> 01:59:09.690 [EXCLAIMING] 01:59:09.690 --> 01:59:10.190 All right. 01:59:10.190 --> 01:59:12.220 Couple more tries. 01:59:12.220 --> 01:59:13.260 Yes! 01:59:13.260 --> 01:59:14.345 Oh, no. 01:59:14.345 --> 01:59:15.997 (SINGING) This is it for a winner. 01:59:15.997 --> 01:59:17.830 Dance to this, and you're gonna get thinner. 01:59:17.830 --> 01:59:19.480 Now move, slide your rump. 01:59:19.480 --> 01:59:20.080 DAVID J. MALAN: Starts getting stressful. 01:59:20.080 --> 01:59:22.210 (SINGING) Just for a minute, let's all do the bump. 01:59:22.210 --> 01:59:23.140 Bump, bump, bump. 01:59:23.140 --> 01:59:24.310 Yeah. 01:59:24.310 --> 01:59:27.020 You can't touch this. 01:59:27.020 --> 01:59:27.890 Look, man. 01:59:27.890 --> 01:59:29.358 You can't touch this. 01:59:29.358 --> 01:59:30.650 You'll probably get hyped, boy. 01:59:30.650 --> 01:59:31.620 DAVID J. MALAN: One more try. 01:59:31.620 --> 01:59:33.087 (SINGING) You can't touch this. 01:59:33.087 --> 01:59:33.670 Ring the bell. 01:59:33.670 --> 01:59:34.840 School's back in. 01:59:34.840 --> 01:59:35.140 DAVID J. MALAN: All right. 01:59:35.140 --> 01:59:36.515 A round of applause, nonetheless. 01:59:36.515 --> 01:59:37.420 [APPLAUSE] 01:59:37.420 --> 01:59:38.360 Nicely done. 01:59:38.360 --> 01:59:39.280 Thank you. 01:59:42.500 --> 01:59:46.660 So as you might have noticed if your eyes started 01:59:46.660 --> 01:59:48.410 to wander to the light bulbs here, there's 01:59:48.410 --> 01:59:50.570 actually 64 of these light bulbs. 01:59:50.570 --> 01:59:55.062 And I'm wondering if you divide 64 by 8, that's 8 bytes of light bulbs. 01:59:55.062 --> 01:59:57.020 And we now have some Unicode in our vocabulary. 01:59:57.020 --> 01:59:58.970 So might very well be the case that we've 01:59:58.970 --> 02:00:03.360 been spelling something out on the stage here for you all of this time. 02:00:03.360 --> 02:00:07.190 But before we adjourn for cake to be served in the Transept, 02:00:07.190 --> 02:00:11.930 allow me to introduce some of CS50's human friends, 02:00:11.930 --> 02:00:17.210 the Harvard Krokodiloes and the Radcliffe Pitches, 02:00:17.210 --> 02:00:23.148 to give us this exciting ending, "This is CS50." 02:00:23.148 --> 02:00:26.606 [APPLAUSE, CHEERING] 02:00:36.980 --> 02:00:38.462 [HARMONICA NOTE] 02:00:41.420 --> 02:00:41.920 [VOCALIZING] 02:00:41.920 --> 02:00:50.820 SPEAKER: (SINGING) There's a certain someone who I'm indebted to. 02:00:50.820 --> 02:01:00.080 And since the old [? BNC ?] has 50, I have this friend for you. 02:01:03.090 --> 02:01:04.300 A two, three, four. 02:01:04.300 --> 02:01:08.845 [VOCALIZING] 02:01:08.845 --> 02:01:12.130 Oh, rubber ducky, you're the one. 02:01:12.130 --> 02:01:14.770 You make [INAUDIBLE] so much fun. 02:01:14.770 --> 02:01:18.400 Rubber ducky, I'm awfully fond of you. 02:01:18.400 --> 02:01:25.030 [SCATTING] Rubber ducky, you make me smile, and you help my code compile. 02:01:25.030 --> 02:01:27.100 Rubber ducky, you're my very best friend. 02:01:27.100 --> 02:01:29.230 It's true. 02:01:29.230 --> 02:01:33.760 When I'm at a standstill, your debugging abilities stun me. 02:01:33.760 --> 02:01:40.330 When I'm at the end of my rope, you just snap, and my code's up and running. 02:01:40.330 --> 02:01:45.160 Rubber ducky, you're so fine, and I'm lucky that you're mine. 02:01:45.160 --> 02:01:47.320 Rubber ducky, you're my very best friend. 02:01:47.320 --> 02:01:48.610 It's true. 02:01:48.610 --> 02:01:49.910 You're my best friend. 02:01:49.910 --> 02:01:52.750 It's true. 02:01:52.750 --> 02:01:56.785 Rubber ducky, I'm awfully fond of you. 02:02:02.128 --> 02:02:05.607 [CHEERING, APPLAUSE] 02:02:08.600 --> 02:02:10.140 SPEAKER: Good afternoon, CS50. 02:02:10.140 --> 02:02:12.890 We are the Harvard Krokodiloes, Harvard's oldest a cappella group, 02:02:12.890 --> 02:02:16.160 founded way back in 1946 at the historic Hasty Pudding Club. 02:02:16.160 --> 02:02:18.462 We'd love to make a big thank you to CS50 staff 02:02:18.462 --> 02:02:21.170 and to David Malan for having us perform here at Sanders Theater. 02:02:21.170 --> 02:02:24.800 And you enjoyed this performance, please come audition for us 02:02:24.800 --> 02:02:26.856 this weekend at Farkas Hall. 02:02:26.856 --> 02:02:30.328 [CHEERING, APPLAUSE] 02:02:36.280 --> 02:02:37.730 SPEAKER: Hello, everyone. 02:02:37.730 --> 02:02:40.000 We are some of the Radcliffe Pitches, and we are also 02:02:40.000 --> 02:02:41.920 hosting auditions this weekend. 02:02:41.920 --> 02:02:45.280 You can find more information at our Instagram, @radcliffepitches. 02:02:45.280 --> 02:02:48.700 Now, let me tell you a little bit about just about a year ago 02:02:48.700 --> 02:02:52.840 today, when I was sitting in your very seats on my first day of CS50 lecture. 02:02:52.840 --> 02:02:55.700 And this is just about how I was feeling. 02:02:55.700 --> 02:02:57.170 [HARMONICA NOTE] 02:03:00.110 --> 02:03:03.540 [VOCALIZING] 02:03:07.410 --> 02:03:11.700 (SINGING) It's the first day of class, and I'm brand new to code. 02:03:11.700 --> 02:03:14.190 Is this for me? 02:03:14.190 --> 02:03:16.200 So many people around. 02:03:16.200 --> 02:03:18.750 Can I get through the workload? 02:03:18.750 --> 02:03:21.660 But it's my dream. 02:03:21.660 --> 02:03:25.260 I tend to stick to English, not science. 02:03:25.260 --> 02:03:29.167 But my [INAUDIBLE] friends told me to try this. 02:03:29.167 --> 02:03:32.250 Hey, dancing robot dog, you kind of look like you have your life together, 02:03:32.250 --> 02:03:33.180 I guess. 02:03:33.180 --> 02:03:35.697 I really need some advice. 02:03:35.697 --> 02:03:37.530 (ALL SINGING) We know you're feeling unsure, 02:03:37.530 --> 02:03:40.590 but this is really the right call. 02:03:40.590 --> 02:03:44.760 In CS50, you'll meet new friends, get free food. 02:03:44.760 --> 02:03:50.160 You'll be all set for this fall in CS50. 02:03:50.160 --> 02:03:54.150 You have a thousand TAs who will help you. 02:03:54.150 --> 02:03:58.190 You'll get cupcakes, duckies, Chinese food. 02:03:58.190 --> 02:04:03.870 And you can always take this class and set aside. 02:04:06.880 --> 02:04:08.680 SPEAKER: This is CS50. 02:04:08.680 --> 02:04:10.828 Fist bump. 02:04:10.828 --> 02:04:11.800 [LAUGHTER] 02:04:11.800 --> 02:04:15.202 [APPLAUSE, CHEERING] 02:04:16.645 --> 02:04:18.270 DAVID J. MALAN: Thank you to the Kroks. 02:04:18.270 --> 02:04:19.380 Thank you to the Pitches. 02:04:19.380 --> 02:04:20.370 Cake is now served. 02:04:20.370 --> 02:04:23.490 Come on up to say hi if you'd like or meet Spot. 02:04:23.490 --> 02:04:24.330 See you next time. 02:04:24.330 --> 02:04:25.163 [APPLAUSE, CHEERING] 02:04:25.163 --> 02:04:28.280 [MUSIC PLAYING]