CS50 Video Player
    • 🧁

    • 🍦

    • 🍉

    • 🍿
    • 0:00:00Intro
    • 0:01:17Problem Solving
    • 0:05:36Representation
    • 0:06:14Binary
    • 0:13:25Letters
    • 0:16:40ASCII
    • 0:20:12Unicode
    • 0:25:38Color
    • 0:28:00Images
    • 0:29:28Videos
    • 0:30:43Music
    • 0:31:50Questions on representation
    • 0:35:41Algorithms
    • 0:40:33Time to solve
    • 0:43:57Pseudocode
    • 0:50:35Scratch
    • 0:52:39Scratch blocks
    • 0:57:22Hello world
    • 1:03:30Inputs to outputs, outputs to inputs
    • 1:05:50Text to speech
    • 1:07:41Meow
    • 1:08:35Loops
    • 1:10:42Custom blocks
    • 1:15:50Conditionals
    • 1:17:25Forever
    • 1:18:42Video sensing
    • 1:20:25Whack-a-mole
    • 1:21:57One piece at a time
    • 1:22:48Oscartime
    • 1:24:24Oscartime stage
    • 1:25:30Oscartime trash can
    • 1:27:18Oscartime falling trash
    • 1:29:42Oscartime dragging trash
    • 1:31:30Putting Oscartime together
    • 1:33:42Movement
    • 1:35:32Movement's abstractions
    • 1:37:30Bouncing
    • 1:39:05Following
    • 1:41:00Ivy's Hardest Game
    • 0:00:00[MUSIC PLAYING]
    • 0:01:18DAVID MALAN: All right, this is CS50, Harvard University's introduction
    • 0:01:22to the intellectual enterprises of computer science
    • 0:01:25and the art of programming, back here on campus in beautiful Sanders Theatre
    • 0:01:29for the first time in quite a while.
    • 0:01:31So welcome to the class.
    • 0:01:33My name is David--
    • 0:01:35OK.
    • 0:01:35[CHEERING AND APPLAUSE]
    • 0:01:44So my name is David Malan.
    • 0:01:46And I took this class myself some time ago, but almost didn't.
    • 0:01:49It was sophomore fall and I was sitting in on the class.
    • 0:01:52And I was a little curious but, eh, it didn't really
    • 0:01:55feel like the field for me.
    • 0:01:57I was definitely a computer person, but computer science
    • 0:01:59felt like something altogether.
    • 0:02:01And I only got up the nerve to take the class,
    • 0:02:02ultimately, because the professor at the time, Brian Kernighan,
    • 0:02:05allowed me to take the class pass/fail, initially.
    • 0:02:08And that is what made all the difference.
    • 0:02:10I quickly found that computer science is not just
    • 0:02:12about programming and working in isolation on your computer.
    • 0:02:15It's really about problem solving more generally.
    • 0:02:18And there was something about homework, frankly,
    • 0:02:20that was, like, actually fun for perhaps the first time in, what, 19 years.
    • 0:02:24And there was something about this ability
    • 0:02:26that I discovered, along with all of my classmates,
    • 0:02:28to actually create something and bring a computer to life to solve a problem,
    • 0:02:33and sort of bring to bear something that I'd been using every day
    • 0:02:35but didn't really know how to harness, that's been gratifying ever since,
    • 0:02:38and definitely challenging and frustrating.
    • 0:02:40Like, to this day, all these years later,
    • 0:02:43you're going to run up against mistakes, otherwise known as bugs,
    • 0:02:46in programming, that just drive you nuts.
    • 0:02:47And you feel like you've hit a wall.
    • 0:02:49But the trick really is to give it enough time,
    • 0:02:51to take a step back, take a break when you need to.
    • 0:02:53And there's nothing better, I daresay, than that sense of gratification
    • 0:02:57and pride, really, when you get something
    • 0:02:58to work, and in a class like this, present, ultimately,
    • 0:03:01at term's end, something like your very own final project.
    • 0:03:04Now, this isn't to say that I took to it 100% perfectly.
    • 0:03:08In fact, just this past week, I looked in my old CS50 binder, which I still
    • 0:03:13have from some 25 years ago, and took a photo
    • 0:03:15of what was apparently the very first program that I wrote and submitted,
    • 0:03:20and quickly received minus 2 points on.
    • 0:03:22But this is a program that we'll soon see in the coming days that
    • 0:03:26does something quite simply like print "Hello, CS50," in this case,
    • 0:03:30to the screen.
    • 0:03:31And to be fair, I technically hadn't really
    • 0:03:32followed the directions, which is why I lost those couple of points.
    • 0:03:35But if you just look at this, especially if you've never programmed before,
    • 0:03:38you might have heard about programming language
    • 0:03:40but you've never typed something like this out,
    • 0:03:42undoubtedly it's going to look cryptic.
    • 0:03:44But unlike human languages, frankly, which
    • 0:03:46were a lot more sophisticated, a lot more vocabulary, a lot more
    • 0:03:50grammatical rules, programming, once you start to wrap your mind around what
    • 0:03:54it is and how it works and what these various languages are, it's so easy,
    • 0:03:57you'll see, after a few months of a class like this,
    • 0:03:59to start teaching yourself, subsequently,
    • 0:04:01other languages, as they may come, in the coming years as well.
    • 0:04:05So what ultimately matters in this particular course
    • 0:04:08is not so much where you end up relative to your classmates
    • 0:04:11but where you end up relative to yourself when you began.
    • 0:04:14And indeed, you'll begin today.
    • 0:04:16And the only experience that matters ultimately in this class is your own.
    • 0:04:19And so, consider where you are today.
    • 0:04:21Consider, perhaps, just how cryptic something like that
    • 0:04:24looked a few seconds ago.
    • 0:04:25And take comfort in knowing just some months from now all of that
    • 0:04:28will be within your own grasp.
    • 0:04:31And if you're thinking that, OK, surely the person in front of me, to the left,
    • 0:04:34to the right, behind me, knows more than me, that's statistically not the case.
    • 0:04:382/3 of CS50 students have never taken a CS course before, which is to say,
    • 0:04:42you're in very good company throughout this whole term.
    • 0:04:47So then, what is computer science?
    • 0:04:49I claim that it's problem solving.
    • 0:04:51And the upside of that is that problem solving is
    • 0:04:53something we sort of do all the time.
    • 0:04:55But a computer science class, learning to program,
    • 0:04:58I think kind of cleans up your thoughts.
    • 0:05:00It helps you learn how to think more methodically, more carefully, more
    • 0:05:03correctly, more precisely.
    • 0:05:05Because, honestly, the computer is not going
    • 0:05:07to do what you want unless you are correct and precise and methodical.
    • 0:05:10And so, as such, there's these fringe benefits
    • 0:05:12of just learning to think like a computer scientist and a programmer.
    • 0:05:15And it doesn't take all that much to start doing so.
    • 0:05:18This, for instance, is perhaps the simplest picture of computer science,
    • 0:05:21sure, but really problem solving in general.
    • 0:05:23Problems are all about taking input, like the problem you want to solve.
    • 0:05:27You want to get the solution, a.k.a.
    • 0:05:29output.
    • 0:05:29And so, something interesting has got to be happening in here,
    • 0:05:32in here, when you're trying to get from those inputs to outputs.
    • 0:05:35Now, in the world of computers specifically,
    • 0:05:38we need to decide in advance how we represent these inputs and outputs.
    • 0:05:42We all just need to decide, whether it's Macs or PCs or phones or something
    • 0:05:46else, that we're all going to speak some common language, irrespective
    • 0:05:49of our human languages as well.
    • 0:05:51And you may very well know that computers tend to speak only
    • 0:05:55what language, so to speak?
    • 0:05:59Assembly, one, but binary, two, might be your go-to.
    • 0:06:01And binary, by implying two, means that the world of computers
    • 0:06:05has just two digits at its disposal, 0 and 1.
    • 0:06:08And indeed, we humans have many more than that, certainly not just zeros
    • 0:06:12and ones alone.
    • 0:06:13But a computer indeed only has zeros and ones.
    • 0:06:15And yet, somehow they can do so much.
    • 0:06:18They can crunch numbers in Excel, send text messages,
    • 0:06:20create images and artwork and movies and more.
    • 0:06:23And so, how do you get from something as simple as a few zeros, a few ones,
    • 0:06:27to all of the stuff that we're doing today
    • 0:06:29in our pockets and laptops and desktops?
    • 0:06:31Well, it turns out that we can start quite simply.
    • 0:06:34If a computer were to want to do something as simple as count, well,
    • 0:06:38what could it do?
    • 0:06:38Well, in our human world, we might count doing this,
    • 0:06:41like 1, 2, 3, 4, 5, using so-called unitary notation, literally the digits
    • 0:06:46on your fingers where one finger represents one person in the room,
    • 0:06:49if I'm, for instance, taking attendance.
    • 0:06:51Now, we humans would typically actually count 1, 2, 3, 4, 5, 6.
    • 0:06:55And we'd go past just those five digits and count much higher,
    • 0:06:58using zeros through nines.
    • 0:06:59But computers, somehow, only have these zeros and ones.
    • 0:07:02So if a computer only somehow speaks binary, zeros and ones,
    • 0:07:05how does it even count past the number 1?
    • 0:07:08Well, here are 3 zeros, of course.
    • 0:07:11And if you translate this number in binary, 000,
    • 0:07:14to a more familiar number in decimal, we would just call this zero.
    • 0:07:18Enough said.
    • 0:07:19If we were to represent, with a computer, the number 1,
    • 0:07:22it would actually be 001, which, not surprisingly,
    • 0:07:25is exactly the same as we might do in our human world,
    • 0:07:28but we might not bother writing out the two zeros at the beginning.
    • 0:07:32But a computer, now, if it wants to count as high as two,
    • 0:07:34it doesn't have the digit 2.
    • 0:07:36And so it has to use a different pattern of zeros and ones.
    • 0:07:39And that happens to be 010.
    • 0:07:41So this is not 10 with a zero in front of it.
    • 0:07:43It's indeed zero one zero in the context of binary.
    • 0:07:45And if we want to count higher now than two,
    • 0:07:48we're going to have to tweak these zeros and ones further to get 3.
    • 0:07:52And then if we want 4 or 5 or 6 or 7, we're
    • 0:07:56just kind of toggling these zeros and ones, a.k.a.
    • 0:07:59bits, for binary digits that represent, via these different patterns,
    • 0:08:04different numbers that you and I, as humans, know,
    • 0:08:06of course, as the so-called decimal system, 0 through 9,
    • 0:08:09dec implying 10, 10 digits, those zeros through nine.
    • 0:08:13So why that particular pattern?
    • 0:08:15And why these particular zeros and ones?
    • 0:08:17Well, it turns out that representing one thing or the other
    • 0:08:20is just really simple for a computer.
    • 0:08:23Why?
    • 0:08:23At the end of the day, they're powered by electricity.
    • 0:08:25And it's a really simple thing to just either store some electricity
    • 0:08:28or don't store some electricity.
    • 0:08:30Like, that's as simple as the world can get, on or off.
    • 0:08:331 or 0, so to speak.
    • 0:08:35So, in fact, inside of a computer, a phone, anything
    • 0:08:38these days that's electronic, pretty much,
    • 0:08:40is some number of switches, otherwise known as transistors.
    • 0:08:43And they're tiny.
    • 0:08:44You've got thousands, millions of them in your Mac or PC or phone these days.
    • 0:08:47And these are just tiny little switches that can get turned on and off.
    • 0:08:50And by turning those things on and off in patterns,
    • 0:08:53a computer can count from 0 on up to 7, and even higher than that.
    • 0:08:56And so these switches, really, you can think of being as like switches
    • 0:08:59like this.
    • 0:09:00Let me just borrow one of our little stage lights here.
    • 0:09:02Here's a light bulb.
    • 0:09:03It's currently off.
    • 0:09:04And so, I could just think of this as representing,
    • 0:09:07in my laptop, a transistor, a switch, representing 0.
    • 0:09:10But if I allow some electricity to flow, now I, in fact, have a 1.
    • 0:09:15Well, how do I count higher than 1?
    • 0:09:17I, of course, need another light bulb.
    • 0:09:19So let me grab another one here.
    • 0:09:21And if I put it in that same kind of pattern, I don't want to just do this.
    • 0:09:26That's sort of the old finger counting way of unary, just 1, 2.
    • 0:09:29I want to actually take into account the pattern
    • 0:09:31of these things being on and off.
    • 0:09:33So if this was one a moment ago, what I think I did earlier was I turned it off
    • 0:09:39and let the next one over be on, a.k.a.
    • 0:09:43010.
    • 0:09:44And let me get us a third bit, if you will.
    • 0:09:47And that feels like enough.
    • 0:09:49Here is that same pattern now, starting at the beginning with 3.
    • 0:09:53So here is 000.
    • 0:09:55Here is 001.
    • 0:09:58Here is 010, a.k.a., in our human world of decimal, 2.
    • 0:10:05And then we could, of course, keep counting further.
    • 0:10:07This now would be 3 and dot dot dot.
    • 0:10:10If this other bulb now goes on, and that switch is turned
    • 0:10:13and all three stay on-- this, again, was what number?
    • 0:10:16AUDIENCE: Seven.
    • 0:10:16DAVID MALAN: OK, so, seven.
    • 0:10:18So it's just as simple, relatively, as that, if you will.
    • 0:10:22But how is it that these patterns came to be?
    • 0:10:26Well, these patterns actually follow something very familiar.
    • 0:10:29You and I don't really think about it at this level
    • 0:10:31anymore because we've probably been doing math and numbers since grade
    • 0:10:35school or whatnot.
    • 0:10:36But if we consider something in decimal, like the number 123,
    • 0:10:41I immediately jump to that.
    • 0:10:43This looks like 123 in decimal.
    • 0:10:45But why?
    • 0:10:46It's really just three symbols, a 1, a 2 with a bit of curve, a 3
    • 0:10:50with a couple of curves, that you and I now instinctively
    • 0:10:52just assign meaning to.
    • 0:10:54But if we do rewind a few years, that is one hundred twenty-three
    • 0:10:59because you're assigning meaning to each of these columns.
    • 0:11:03The 3 is in the so-called ones place.
    • 0:11:06The 2 is in the so-called tens place.
    • 0:11:09And the 1 is in the so-called hundreds place.
    • 0:11:12And then the math ensues quickly in your head.
    • 0:11:14This is technically 100 times 1, plus 10 times 2, plus 1 times 3, a.k.a.
    • 0:11:20100 plus 20 plus 3.
    • 0:11:21And there we get the sort of mathematical notion we know as 123.
    • 0:11:27Well, nicely enough, in binary, it's actually the same thing.
    • 0:11:31It's just these columns mean a little something different.
    • 0:11:33If you use three digits in decimal, and you have the ones place,
    • 0:11:37the tens place, and the hundreds place, well, why was that 1, 10, and 100?
    • 0:11:42They're technically just powers of 10.
    • 0:11:43So 10 to the 0, 10 to the 1, 10 to the 2.
    • 0:11:46Why 10?
    • 0:11:47Decimal system, "dec" meaning 10.
    • 0:11:49You have 8 and 10 digits, 0 through 9.
    • 0:11:51In the binary system, if you're going to use three digits,
    • 0:11:54just change the bases if you're using only zeros and ones.
    • 0:11:57So now it's powers of 2, 2 to the 0, 2 to the 1, 2 to the 2, a.k.a.
    • 0:12:011 and 2 and 4, respectively.
    • 0:12:04And if you keep going, it's going to be 8s column, 16s column, 32, 64,
    • 0:12:08and so forth.
    • 0:12:10So, why did we get these patterns that we did?
    • 0:12:12Here's your 000 because it's 4 times 0, 2 times 0, 1 times 0, obviously 0.
    • 0:12:18This is why we got the decimal number 1 in binary.
    • 0:12:22This is why we got the number 2 in binary, because it's 4 times
    • 0:12:260, plus 2 times 1, plus 1 times 0, and now 3, and now 4, and now 5, and now 6,
    • 0:12:33and now 7.
    • 0:12:34And, of course, if you wanted to count as high as 8, to be clear,
    • 0:12:38what do you have to do?
    • 0:12:39What does a computer need to do to count even higher than 7?
    • 0:12:41AUDIENCE: Add a bit.
    • 0:12:42DAVID MALAN: Add a bit.
    • 0:12:43Add another light bulb, another switch.
    • 0:12:45And, indeed, computers have standardized just how
    • 0:12:47many zeros and ones, or bits or switches,
    • 0:12:49they throw at these kinds of problems.
    • 0:12:51And, in fact, most computers would typically use at least eight at a time.
    • 0:12:56And even if you're only counting as high as three or seven,
    • 0:12:58you would still use eight and have a whole bunch of zeros.
    • 0:13:01But that's OK, because the computers these days certainly
    • 0:13:04have so many more, thousands, millions of transistors and switches
    • 0:13:08that that's quite OK.
    • 0:13:10All right, so, with that said, if we can now count as high as seven
    • 0:13:14or, frankly, as high as we want, that only
    • 0:13:16seems to make computers useful for things like Excel,
    • 0:13:19like number crunching.
    • 0:13:20But computers, of course, let you send text messages,
    • 0:13:22write documents, and so much more.
    • 0:13:24So how would a computer represent something like a letter,
    • 0:13:28like the letter A of the English alphabet, if, at the end of the day,
    • 0:13:32all they have is switches?
    • 0:13:35Any thoughts?
    • 0:13:35Yeah.
    • 0:13:36AUDIENCE: You can represent letters in numbers.
    • 0:13:38DAVID MALAN: OK, so we could represent letters using numbers.
    • 0:13:41OK, so what's a proposal?
    • 0:13:42What number should represent what?
    • 0:13:44AUDIENCE: Say if you were starting at the beginning of the alphabet,
    • 0:13:47you could say 1 is A, 2 is B, 3 is C.
    • 0:13:51DAVID MALAN: Perfect.
    • 0:13:52Yeah, we just all have to agree somehow that one number is
    • 0:13:55going to represent one letter.
    • 0:13:56So 1 is A, 2 is B, 3 is C, Z is 26, and so forth.
    • 0:14:01Maybe we can even take into account uppercase and lowercase.
    • 0:14:03We just have to agree and sort of write it down in some global standard.
    • 0:14:06And humans, indeed, did just that.
    • 0:14:09They didn't use 1, 2, 3.
    • 0:14:10It turns out they started a little higher up.
    • 0:14:12Capital A has been standardized as the number 65.
    • 0:14:16And capital B has been standardized as the number 66.
    • 0:14:20And you can kind of imagine how it goes up from there.
    • 0:14:23And that's because whatever you're representing,
    • 0:14:25ultimately, can only be stored, at the end of the day, as zeros and ones.
    • 0:14:30And so, some humans in a room before, decided that capital A shall be 65,
    • 0:14:33or, really, this pattern of zeros and ones inside of every computer
    • 0:14:37in the world, 01000001.
    • 0:14:41So if that pattern of zeros and ones ever appears in a computer,
    • 0:14:45it might be interpreted then as indeed a capital letter A, eight of those bits
    • 0:14:50at a time.
    • 0:14:51But I worry, just to be clear, we might have now created a problem.
    • 0:14:55It might seem, if I play this naively, that, OK,
    • 0:14:58how do I now actually do math with the number 65?
    • 0:15:01If now Excel displays 65 is an A, let alone Bs and Cs.
    • 0:15:06So how might a computer do as you've proposed,
    • 0:15:09have this mapping from numbers to letters, but still support numbers?
    • 0:15:14It feels like we've given something up.
    • 0:15:16Yeah?
    • 0:15:16AUDIENCE: By having a prefix for letters?
    • 0:15:18DAVID MALAN: By having a prefix?
    • 0:15:20AUDIENCE: You could have prefixes and suffixes.
    • 0:15:21DAVID MALAN: OK, so we could perhaps have some kind of prefix,
    • 0:15:24like some pattern of zeros and ones--
    • 0:15:25I like this-- that indicates to the computer
    • 0:15:28here comes another pattern that represents a letter.
    • 0:15:31Here comes another pattern that represents a number or a letter.
    • 0:15:35So, not bad.
    • 0:15:36I like that.
    • 0:15:37Other thoughts?
    • 0:15:38How might a computer distinguish these two?
    • 0:15:41Yeah.
    • 0:15:41AUDIENCE: Have a different file format, so,
    • 0:15:44like, odd text or just check the graphic or--
    • 0:15:48DAVID MALAN: Indeed, and that's spot-on.
    • 0:15:50Nothing wrong with what you suggested, but the world generally does just that.
    • 0:15:53The reason we have all of these different file formats in the world,
    • 0:15:56like JPEG and GIF and PNGs and Word documents, .docx,
    • 0:16:01and Excel files and so forth, is because a bunch of humans got in a room
    • 0:16:04and decided, well, in the context of this type of file, or really,
    • 0:16:08more specifically, in the context of this type of program,
    • 0:16:11Excel versus Photoshop versus Google Docs or the like,
    • 0:16:14we shall interpret any patterns of zeros and ones as being maybe numbers
    • 0:16:19for Excel, maybe letters in, like, a text messaging program or Google Docs,
    • 0:16:23or maybe even colors of the rainbow in something like Photoshop and more.
    • 0:16:27So it's context dependent.
    • 0:16:28And we'll see, when we ourselves start programming,
    • 0:16:31you the programmer will ultimately provide
    • 0:16:33some hints to the computer that tells the computer, interpret it as follows.
    • 0:16:38So, similar in spirit to that, but not quite a standardized with these
    • 0:16:41prefixes.
    • 0:16:41So this system here actually has a name ASCII, the American Standard
    • 0:16:45Code for Information Interchange.
    • 0:16:47And indeed, it began here in the US, and that's
    • 0:16:49why it's actually a little biased toward A's through Z's
    • 0:16:52and a bit of punctuation as well.
    • 0:16:54And that quickly became a problem.
    • 0:16:55But if we start simply now, in English, the mapping
    • 0:16:59itself is fairly straightforward.
    • 0:17:01So if A is 65, B it 66, and dot dot dot, suppose
    • 0:17:05that you received a text message, an email, from a friend,
    • 0:17:08and underneath the hood, so to speak, if you kind of
    • 0:17:11looked inside the computer, what you technically received in this text
    • 0:17:15or this email happened to be the numbers 72, 73, 33,
    • 0:17:20or, really, the underlying pattern of zeros and ones.
    • 0:17:23What might your friend have sent you as a message, if it's 72, 73, 33?
    • 0:17:29AUDIENCE: Hey.
    • 0:17:30DAVID MALAN: Hey?
    • 0:17:31Close.
    • 0:17:31AUDIENCE: Hi.
    • 0:17:32DAVID MALAN: Hi.
    • 0:17:33It's, indeed, hi.
    • 0:17:34Why?
    • 0:17:34Well, apparently, according to this little cheat sheet, H is 72, I is 73.
    • 0:17:39It's not obvious from this chart what the 33 is,
    • 0:17:42but indeed, this pattern represents "hi."
    • 0:17:44And anyone want to guess, or if you know, what 33 is?
    • 0:17:46AUDIENCE: Exclamation point.
    • 0:17:47DAVID MALAN: Exclamation point.
    • 0:17:48And this is, frankly, not the kind of thing most people know.
    • 0:17:51But it's easily accessible by a nice user-friendly chart like this.
    • 0:17:54So this is an ASCII chart.
    • 0:17:56When I said that we just need to write down this mapping earlier,
    • 0:17:59this is what people did.
    • 0:18:00They wrote it down in a book or in a chart.
    • 0:18:02And, for instance, here is our 72 for H, here is our 73 for I,
    • 0:18:06and here is our 33 for exclamation point.
    • 0:18:10And computers, Macs, PCs, iPhones, Android devices,
    • 0:18:13just know this mapping by heart, if you will.
    • 0:18:16They've been designed to understand those letters.
    • 0:18:19So here, I might have received "hi."
    • 0:18:20Technically, what I've received is these patterns of zeros and ones.
    • 0:18:24But it's important to note that when you get these patterns of zeros and ones
    • 0:18:27in any format, be it email or text or a file,
    • 0:18:30they do tend to come in standard lengths,
    • 0:18:33with a certain number of zeros and ones altogether.
    • 0:18:37And this happens to be 8 plus 8, plus 8.
    • 0:18:39So just to get the message "hi, exclamation point,"
    • 0:18:43you would have received at least, it would seem, some 24 bits.
    • 0:18:47But frankly, bits are so tiny, literally and mathematically,
    • 0:18:51that we don't tend to think or talk, generally, in terms of bits.
    • 0:18:53You're probably more familiar with bytes.
    • 0:18:56B-Y-T-E-S is a byte, is a byte, is a byte.
    • 0:19:00A byte is just 8 bits.
    • 0:19:02And even those, frankly, aren't that useful if we do out the math.
    • 0:19:05How high can you count if you have eight bits?
    • 0:19:06Anyone know?
    • 0:19:08Say it again?
    • 0:19:10Higher than that.
    • 0:19:11Unless you want to go negative, that's fine.
    • 0:19:15256, technically 255.
    • 0:19:17Long story short, if we actually got into the weeds of all of these zeros
    • 0:19:21and ones, and we figured out what 11111111 mathematically adds up
    • 0:19:26to in decimal, it would indeed be 255, or less
    • 0:19:30if you want to represent negative numbers as well.
    • 0:19:32So this is useful because now we can speak, not just in terms of bytes
    • 0:19:36but, if the files are bigger, kilobytes is thousands of bytes,
    • 0:19:39megabytes is millions of bytes, gigabytes is billions of bytes,
    • 0:19:43terabytes are trillions of bytes, and so forth.
    • 0:19:46We have a vocabulary for these increasingly large quantities of data.
    • 0:19:53The problem is that, if you're using ASCII and, therefore, eight bits or one
    • 0:19:57byte per character, and originally, only seven, you
    • 0:20:00can only represent 255 characters.
    • 0:20:03And that's actually 256 total characters, including zero.
    • 0:20:06And that's fine if you're using literally English, in this case,
    • 0:20:10plus a bunch of punctuation.
    • 0:20:11But there's many human languages in the world
    • 0:20:14that need many more symbols and, therefore, many more bits.
    • 0:20:18So, thankfully, the world decided that we'll indeed
    • 0:20:20support not just the US English keyboard, but all
    • 0:20:24of the accented characters that you might want for some languages.
    • 0:20:27And heck, if we use enough bits, zeros and ones,
    • 0:20:30not only can we represent all human languages in written form,
    • 0:20:34as well as some emotions along the way, we
    • 0:20:36can capture the latter with these things called emojis.
    • 0:20:39And indeed, these are very much in vogue these days.
    • 0:20:41You probably send and/or receive many of these things any given day.
    • 0:20:45These are just characters, like letters of an alphabet, patterns
    • 0:20:49of zeros and ones that you're receiving, that the world has also standardized.
    • 0:20:53For instance, there are certain emojis that
    • 0:20:55are represented with certain patterns of bits.
    • 0:20:57And when you receive them, your phone, your laptop, your desktop,
    • 0:21:00displays them as such.
    • 0:21:02And this newer standard is called Unicode.
    • 0:21:05So it's a superset of what we called ASCII.
    • 0:21:07And Unicode is just a mapping of many more numbers to many more letters
    • 0:21:12or characters, more generally, that might
    • 0:21:15use eight bits for backwards compatibility
    • 0:21:17with the old way of doing things with ASCII, but they might also use 16 bits.
    • 0:21:22And if you have 16 bits, you can actually
    • 0:21:24represent more than 65,000 possible letters.
    • 0:21:27And that's getting up there.
    • 0:21:28And heck, Unicode might even use 32 bits to represent letters and numbers
    • 0:21:34and punctuation symbols and emojis.
    • 0:21:35And that would give you up to 4 billion possibilities.
    • 0:21:39And, I daresay, one of the reasons we see so many emojis these days is we
    • 0:21:42have so much room.
    • 0:21:43I mean, we've got room for billions more, literally.
    • 0:21:46So, in fact, just as a little bit of trivia,
    • 0:21:48has anyone ever received this decimal number, or if you prefer binary now,
    • 0:21:54has anyone ever received this pattern of zeros and ones on your phone,
    • 0:21:58in a text or an email, perhaps this past year?
    • 0:22:01Well, if you actually look this up, this esoteric sequence of zeros and ones
    • 0:22:06happens to represent face with medical mask.
    • 0:22:09And notice that if you've got an iPhone or an Android device,
    • 0:22:13you might be seeing different things.
    • 0:22:15In fact, this is the Android version of this, most recently.
    • 0:22:19This is the iOS version of it, most recently.
    • 0:22:21And there's bunches of other interpretations by other companies
    • 0:22:24as well.
    • 0:22:25So Unicode, as a consortium, if you will,
    • 0:22:28has standardized the descriptions of what these things are.
    • 0:22:31But the companies themselves, manufacturers out there,
    • 0:22:35have generally interpreted it as you see fit.
    • 0:22:37And this can lead to some human miscommunications.
    • 0:22:41In fact, for like, literally, embarrassingly, like a year or two,
    • 0:22:44I started being in the habit of using the emoji that kind of looks
    • 0:22:47like this because I thought it was like woo, happy face, or whatever.
    • 0:22:50I didn't realize this is the emoji for hug
    • 0:22:52because whatever device I was using sort of looks like this, not like this.
    • 0:22:57And that's because of their interpretation of the data.
    • 0:22:59This has happened too when what was a gun became a water
    • 0:23:04pistol in some manufacturers' eyes.
    • 0:23:06And so it's an interesting dichotomy between what information we all
    • 0:23:10want to represent and how we choose, ultimately, to represent it.
    • 0:23:15Questions, then, on these representations of formats,
    • 0:23:18be it numbers or letters, or soon more.
    • 0:23:21Yeah?
    • 0:23:22AUDIENCE: Why is decimal popular for a computer
    • 0:23:24if binary is the basis for everything?
    • 0:23:27DAVID MALAN: Sorry, why is what so popular?
    • 0:23:29AUDIENCE: Why is the decimal popular if binary is the fundamental--
    • 0:23:32DAVID MALAN: Yeah, so we'll come back to this in a few weeks, in fact.
    • 0:23:34There are other ways to represent numbers.
    • 0:23:36Binary is one.
    • 0:23:37Decimal is another.
    • 0:23:38Unary is another.
    • 0:23:39And hexadecimal is yet a fourth that uses 16 total digits, literally 0
    • 0:23:45through 9 plus A, B, C, D, E, F. And somehow,
    • 0:23:48you can similarly count even higher with those.
    • 0:23:51We'll see in a few weeks why this is compelling.
    • 0:23:53But hexadecimal, long story short, uses four bits per digit.
    • 0:23:57And so, four bits, if you have two digits in hex, that gives you eight.
    • 0:24:00And it's just a very convenient unit of measure.
    • 0:24:03And it's also human convention in the world of files and other things.
    • 0:24:06But we'll come back to that soon.
    • 0:24:08Other questions?
    • 0:24:09AUDIENCE: Do the lights on the stage supposedly say that--
    • 0:24:12DAVID MALAN: Do the lights on the stage supposedly say anything?
    • 0:24:15Well, if we had thought in advance to use maybe 64 light bulbs,
    • 0:24:19that would seem to give us 8 total bytes on stage, 8 times 8,
    • 0:24:24giving us just that.
    • 0:24:25Maybe.
    • 0:24:26Good question.
    • 0:24:27Other questions on 0's and 1's?
    • 0:24:31It's a little bright in here.
    • 0:24:33No?
    • 0:24:34Oh, yes?
    • 0:24:37Where everyone's pointing somewhere specific.
    • 0:24:41There we go.
    • 0:24:42Sorry.
    • 0:24:42Very bright in this corner.
    • 0:24:44AUDIENCE: I was just going to ask about the 255 bits,
    • 0:24:47like with the maximum characters.
    • 0:24:49[INAUDIBLE]
    • 0:24:49DAVID MALAN: Ah, sure, and we'll come back to this, in some form,
    • 0:24:52in the coming days too, at a slower pace too,
    • 0:24:54we have, with eight bits, two possible values for the first
    • 0:24:58and then two for the next, two for the next, and so forth.
    • 0:25:01So that's 2 times 2 times 2.
    • 0:25:02That's 2 to the eighth power total, which
    • 0:25:04means you can have 256 total possible patterns of zeros and ones.
    • 0:25:09But as we'll see soon computer scientists, programmers,
    • 0:25:13software often starts counting at 0 by convention and if you use one of those
    • 0:25:17patterns, 00000000 to represent the decimal number we know is zero,
    • 0:25:23you only have 255 other patterns left to count as high as therefore 255.
    • 0:25:29That's all.
    • 0:25:30Good question.
    • 0:25:32All right, so what then might we have besides these emojis and letters
    • 0:25:37and numbers?
    • 0:25:37Well, we of course have things like colors and programs
    • 0:25:40like Photoshop and pictures and photos.
    • 0:25:42Well let me ask the question again.
    • 0:25:43How might a computer, do you think, knowing what you know now, represents
    • 0:25:47something like a color?
    • 0:25:48Like what are our options if all we've got are zeros and ones and switches?
    • 0:25:52Yeah?
    • 0:25:53AUDIENCE: RGB
    • 0:25:54DAVID MALAN: RGB.
    • 0:25:56RGB indeed is this acronym that represents some amount of red
    • 0:25:59and some amount of green and blue and indeed computers
    • 0:26:02can represent colors by just doing that.
    • 0:26:05Remembering, for instance, this dot.
    • 0:26:06This yellow dot on the screen that might be part of any of those emojis
    • 0:26:09these days, well that's some amount of red, some amount of green,
    • 0:26:12some amount of blue.
    • 0:26:13And if you sort of mix those colors together,
    • 0:26:15you can indeed get a very specific one.
    • 0:26:16And we'll see you in just a moment just that.
    • 0:26:19So indeed earlier on, humans only used seven bits total.
    • 0:26:24And it was only once they decided, well, let's add an eighth bit that they
    • 0:26:27got extended ASCII and that was initially in part
    • 0:26:29a solution to the same problem of not having enough room, if you will,
    • 0:26:33in those patterns of zeros and ones to represent all of the characters
    • 0:26:37that you might want.
    • 0:26:38But even that wasn't enough and that's why we've now gone up to 16 and 32
    • 0:26:43and long past 7.
    • 0:26:44So if we come back now to this one particular color.
    • 0:26:49RGB was proposed as a scheme, but how might this work?
    • 0:26:52Well, consider for instance this.
    • 0:26:53If we do indeed decide as a group to represent any color of the rainbow
    • 0:26:58with some mixture of some red, some green, and some blue,
    • 0:27:01we have to decide how to represent the amount of red and green and blue.
    • 0:27:06Well, it turns out if all we have are zeros and ones, ergo numbers,
    • 0:27:09let's do just that.
    • 0:27:11For instance, suppose a computer we're using, these three numbers 72, 73, 33,
    • 0:27:17no longer in the context of an email or a text message,
    • 0:27:20but now in the context of something like Photoshop, a program for editing
    • 0:27:24and creating graphical files, maybe this first number
    • 0:27:28could be interpreted as representing some amount of red, green, and blue,
    • 0:27:32respectively.
    • 0:27:33And that's exactly what happens.
    • 0:27:34You can think of the first digit as red, second as green, third as blue.
    • 0:27:38And so ultimately when you combine that amount of red, that amount of green,
    • 0:27:42that amount of blue, it turns out it's going to resemble the shade of yellow.
    • 0:27:46And indeed, you can come up with a numbers between 0 and 255
    • 0:27:50for each of those colors to mix any other color that you might want.
    • 0:27:54And you can actually see this in practice.
    • 0:27:56Even though our screens, admittedly, are getting really good
    • 0:27:59on our phones and laptops such that you barely see the dots, they are there.
    • 0:28:03You might have heard the term pixel before.
    • 0:28:05Pixel's just a dot on the screen and you've
    • 0:28:07got thousands, millions of them these days horizontally and vertically.
    • 0:28:10If I take even this emoji, which again happens
    • 0:28:13to be one company's interpretation of a face with medical mask
    • 0:28:18and zoom in a bit, maybe zoom in a bit more,
    • 0:28:21you can actually start to see these pixels.
    • 0:28:23Things get pixelated because what you're seeing
    • 0:28:26is each of the individual dots that compose this particular image.
    • 0:28:29And apparently each of these individual dots
    • 0:28:32are probably using 24 bits, eight bits for red, eight bits for green, eight
    • 0:28:37bits for blue, in some pattern.
    • 0:28:40This program or some other like Photoshop is interpreting one pattern
    • 0:28:43and it's white or yellow or black or some brown in between.
    • 0:28:48So if you look sort of awkwardly, but up close to your phone or your laptop
    • 0:28:52or maybe your TV, you can see exactly this, too.
    • 0:28:56All right, well, what about things that we also
    • 0:28:58watch every day on YouTube or the like?
    • 0:28:59Things like videos.
    • 0:29:01How would a computer, knowing what we know now,
    • 0:29:03represent something like a video?
    • 0:29:07How might you represent a video using only zeros and ones?
    • 0:29:10Yeah?
    • 0:29:11AUDIENCE: As we can see here, they represent images, right?
    • 0:29:15[INAUDIBLE] sounds of the 0 and 1s as well.
    • 0:29:20[INAUDIBLE]
    • 0:29:23DAVID MALAN: Yeah, exactly.
    • 0:29:24To summarize, what video really adds is just some notion of time.
    • 0:29:28It's not just one image, it's not just one letter or a number,
    • 0:29:30it's presumably some kind of sequence because time is passing.
    • 0:29:34So with a whole bunch of images, maybe 24 maybe 30 per second,
    • 0:29:38if you fly them by the human's eyes, we can
    • 0:29:41interpret them using our eyes and brain that there is now
    • 0:29:43movement and therefore video.
    • 0:29:46Similarly with audio or music.
    • 0:29:48If we just came up with some convention for representing those same notes
    • 0:29:52on a musical instrument, could we have the computer synthesize them, too?
    • 0:29:56And this might be actually pretty familiar.
    • 0:29:57Let me pull up a quick video here, which happens to be an old school
    • 0:30:02version of the same idea.
    • 0:30:04You might remember from childhood.
    • 0:30:05[MUSIC PLAYING]
    • 0:30:13[CLICKING]
    • 0:30:27So granted that particular video is an actual video
    • 0:30:31of a paper-based animation, but indeed, that's really all you need,
    • 0:30:35is some sequence of these images, which themselves of course
    • 0:30:38are just zeros and ones because they're just this grid of these pixels or dots.
    • 0:30:43Now something like musical notes like these, those of you who are musicians
    • 0:30:46might just naturally play these on physical devices,
    • 0:30:48but computers can certainly represent those sounds, too.
    • 0:30:51For instance, a popular format for audio is
    • 0:30:54called MIDI and MIDI might just represent
    • 0:30:57each note that you saw a moment ago essentially as a sequence of numbers.
    • 0:31:01But more generally, you might think about music as having notes,
    • 0:31:05for instance, A through G, maybe some flats and some sharps,
    • 0:31:08you might have the duration like how long is the note being heard or played
    • 0:31:11on a piano or some other device, and then
    • 0:31:13just the volume like how hard does a human in the real world
    • 0:31:16press down on that key and therefore how loud is that sound?
    • 0:31:19It would seem that just remembering little details like that quantitatively
    • 0:31:24we can then represent really all of these otherwise analog human realities.
    • 0:31:29So that then is really a laundry list of ways
    • 0:31:33that we can just represent information.
    • 0:31:35Again, computers or digital have all of these different formats,
    • 0:31:38but at the end of the day and as fancy as those devices in years
    • 0:31:40are, it's just zeros and ones, tiny little switches or light bulbs,
    • 0:31:44if you will, represented in some way and it's up to the software
    • 0:31:47that you and I and others write to use those zeros
    • 0:31:50and ones in ways we want to get the computers to do something
    • 0:31:54more powerfully.
    • 0:31:55Questions, then, on this representation of information, which I daresay
    • 0:31:59is ultimately what problem solving is all about, taking in information
    • 0:32:03and producing new via some process in between.
    • 0:32:08Any questions there?
    • 0:32:11Yeah, in back.
    • 0:32:12AUDIENCE: Yeah, so we talked about how different file formats kind of allow
    • 0:32:16you to interpret information.
    • 0:32:18How does a file format like .mp4 discriminate between audio and video
    • 0:32:23within itself as a value?
    • 0:32:25DAVID MALAN: So a really good question.
    • 0:32:26There are many other file formats out there.
    • 0:32:28You allude to MP4 for video and more generally the use
    • 0:32:31are these things called codecs and containers.
    • 0:32:34It's not quite as simple when using larger files, for instance,
    • 0:32:37in more modern formats that a video is just a sequence of images,
    • 0:32:41for instance.
    • 0:32:41Why?
    • 0:32:42If you stored that many images for like a Hollywood movie,
    • 0:32:45like 24 or 30 of them per second, that's a huge number of images.
    • 0:32:50And if you've ever taken photos on your phone,
    • 0:32:52you might know how many megabytes or larger even individual photographs
    • 0:32:56might be.
    • 0:32:57So humans have developed over the years a fancier software
    • 0:33:00that uses much more math to represent the same information more minimally
    • 0:33:05just using somehow shorter patterns of zeros and ones
    • 0:33:07than are most simplistic representation here.
    • 0:33:10And they use what might be called compression.
    • 0:33:12If you've ever used a zip file or something else,
    • 0:33:15somehow your computer is using fewer zeros and ones
    • 0:33:17to represent the same amount of information,
    • 0:33:19ideally without losing any information.
    • 0:33:22In the world of multimedia, which we'll touch on a little bit in a few weeks,
    • 0:33:25there are both lossy and lossless formats out there.
    • 0:33:29Lossless means you lose no information whatsoever.
    • 0:33:32But more commonly as you're alluding to one is lossy compression, L-O-S-S-Y,
    • 0:33:38where you're actually throwing away some amount of quality.
    • 0:33:40You're getting some amount of pixelation that might not
    • 0:33:43look perfect to the human, but heck it's a lot cheaper and a lot easier
    • 0:33:46to distribute.
    • 0:33:47And in the world of multimedia, you have containers like QuickTime
    • 0:33:50and other MPEG containers that can combine
    • 0:33:53different formats of video, different formats of audio in one file,
    • 0:33:57but there, too, do designers have discretion.
    • 0:33:59So more in a few weeks, too.
    • 0:34:01Other questions, then, on information here as well?
    • 0:34:05Yeah?
    • 0:34:05AUDIENCE: So I know computers used to be very big
    • 0:34:08and taking up like a whole room and stuff.
    • 0:34:10Is the reason they've gotten smaller because we can store
    • 0:34:14this information piecemeal or what?
    • 0:34:16DAVID MALAN: Exactly.
    • 0:34:17I mean, back in the day you might have heard of the expression a vacuum
    • 0:34:20tube, which is like some physically large device that
    • 0:34:23might have only stored some 0 or 1.
    • 0:34:25Yes, it is the miniaturization of hardware these days
    • 0:34:28that has allowed us to store as many and many more zeros and ones much more
    • 0:34:33closely together.
    • 0:34:34And as we've built more fancy machines that
    • 0:34:36can sort of design this hardware at an even smaller scale,
    • 0:34:38we're just packing more and more into these devices.
    • 0:34:41But there, too, is a trade off.
    • 0:34:42For instance, you might know by using your phone or your laptop
    • 0:34:45for quite a while, maybe on your lap, starts to get warm.
    • 0:34:48So there are these literal physical side effects
    • 0:34:50of this where now some of our devices run hot.
    • 0:34:53This is why like a data center in the real world
    • 0:34:55might need more air conditioning than a typical place,
    • 0:34:58because there are these physical artifacts as well.
    • 0:35:01In fact, if you'd like to see one of the earliest computers from decades ago,
    • 0:35:04across the river here in now Allston in the new engineering building
    • 0:35:08is the Harvard Mark 1 computer that will give you a much better mental model
    • 0:35:13of just that.
    • 0:35:14Well if we come back now to this first picture
    • 0:35:17being computer science or really problem solving,
    • 0:35:19I daresay we have more than enough ways now
    • 0:35:21to represent information, input and output, so long as we all just agree
    • 0:35:25on something and thankfully all of those before us have given us
    • 0:35:28things like ASCII and Unicode.
    • 0:35:30Not to mention MP4s, word documents, and the like.
    • 0:35:33But what's inside of this proverbial black box into which these inputs are
    • 0:35:37going in the outputs are coming?
    • 0:35:39Well that's where we get this term you might have heard, too.
    • 0:35:42An algorithm, which is just step-by-step instructions for solving some problem
    • 0:35:47incarnated in the world of computers by software.
    • 0:35:50When you write software aka programs, you
    • 0:35:53are implementing one or more algorithms, one or more sets of instructions
    • 0:35:58for solving some problem, and maybe you're using this language or that,
    • 0:36:02but at the end of the day, no matter the language
    • 0:36:04you use the computer is going to represent what you type
    • 0:36:07using just zeros and ones.
    • 0:36:10So what might be a representative algorithm?
    • 0:36:12Nowadays you might use your phone quite a bit
    • 0:36:15to make calls or send texts or emails and therefore you
    • 0:36:18have a whole bunch of contacts in your address book.
    • 0:36:20Nowadays, of course, this is very digital,
    • 0:36:22but whether on iOS or Android or the like,
    • 0:36:26you might have a whole bunch of names, first name
    • 0:36:28and/or last, as well as numbers and emails and the like.
    • 0:36:31You might be in the habit of like scrolling through on your phone
    • 0:36:34all of those names to find the person you want to call.
    • 0:36:37It's probably sorted alphabetically by first name or last name, A through Z,
    • 0:36:41or some other symbol.
    • 0:36:44This is frankly quite the same as we used to do back in my day, CS50,
    • 0:36:49when we just used a physical book.
    • 0:36:50In this physical book might be a whole bunch
    • 0:36:52of names alphabetically sorted from left to right
    • 0:36:54corresponding to a whole bunch of numbers.
    • 0:36:57So suppose that in this old Harvard phone book
    • 0:36:59we want to search for John Harvard.
    • 0:37:01We might of course start quite simply at the beginning
    • 0:37:04here, looking at one page at a time, and this is an algorithm.
    • 0:37:08This is like literally step-by-step looking for the solution
    • 0:37:13to this problem.
    • 0:37:14In that sense, if John Harvard's in the phone book,
    • 0:37:16is this algorithm page-by-page correct, would you say?
    • 0:37:20AUDIENCE: Yes.
    • 0:37:21DAVID MALAN: Yes.
    • 0:37:21Like if John Harvard's in the phone book,
    • 0:37:23obviously I'm eventually going to get to him, so that's what we mean by correct.
    • 0:37:27Is it efficient?
    • 0:37:28Is it well designed, would you say?
    • 0:37:31No.
    • 0:37:31I mean this is going to take forever even just to get to the Js or the Hs,
    • 0:37:34depending how this thing's sorted.
    • 0:37:35All right, well let me go a little faster.
    • 0:37:37I'll start like two pages at a time.
    • 0:37:392, 4, 6, 8, 10, 12, and so forth.
    • 0:37:43Sounds faster, is faster, is it correct?
    • 0:37:46AUDIENCE: No.
    • 0:37:47DAVID MALAN: OK, why is it not correct?
    • 0:37:49Yeah?
    • 0:37:49AUDIENCE: So if you're starting on page 1,
    • 0:37:51you're only going odd number of pages, so if it's on an even number page,
    • 0:37:54you'll miss it.
    • 0:37:55DAVID MALAN: Exactly.
    • 0:37:56If I start on an odd number of pages and I'm going two at a time
    • 0:37:58I might miss pages in between.
    • 0:38:00And if I therefore conclude when I get to the back
    • 0:38:03of the book there was no John Harvard, I might have just errored.
    • 0:38:05This would be again one of these bugs.
    • 0:38:08But if I try a little harder, I feel like there's a solution.
    • 0:38:11We don't have to completely throw out this algorithm.
    • 0:38:14I think we can probably go roughly twice as fast still.
    • 0:38:16But what should we do instead to fix this?
    • 0:38:19Yeah, in back.
    • 0:38:21AUDIENCE: [INAUDIBLE]
    • 0:38:31DAVID MALAN: Nice.
    • 0:38:32So I think what many of us, most of us, if we even use this technology any more
    • 0:38:35these days, we might go roughly to the middle of the phone book
    • 0:38:38just to kind of get us started.
    • 0:38:39And now I'm looking down, I'm looking for J, assuming first name, J Harvard,
    • 0:38:43and it looks like I'm in the M section.
    • 0:38:45So just to be clear, what should I do next?
    • 0:38:48AUDIENCE: [INAUDIBLE]
    • 0:38:54DAVID MALAN: OK, and presumably it is John Harvard
    • 0:38:56would be to the left of this.
    • 0:38:57So here's an opportunity to figuratively and literally tear
    • 0:39:00this particular problem in half, throw half of the problem away.
    • 0:39:05It's actually pretty easy if you just do it that way.
    • 0:39:07The hard way is this way.
    • 0:39:09But I've now just decreased the size of this problem really in half.
    • 0:39:13So if I started with 1,000 pages of phone numbers and names, now
    • 0:39:17I'm down to 500.
    • 0:39:19And already we haven't found John Harvard,
    • 0:39:21but that's a big bite out of this problem.
    • 0:39:23I do think it's correct because if J is to the left of M, of course,
    • 0:39:27he's definitely not going to be over there.
    • 0:39:28I think if I repeat this again dividing and conquering, if you will,
    • 0:39:32here I might have gone a little too far.
    • 0:39:34Now I'm in like the E section.
    • 0:39:36So let me tear the problem in half again, throw another 250 pages away,
    • 0:39:40and again repeat, dividing and dividing and conquering
    • 0:39:43until finally, presumably, I end up with just one page of a phone
    • 0:39:47book on which John Harvard's name either is or is not,
    • 0:39:50but because of the algorithm you proposed, step by step,
    • 0:39:54I know that he's not in anything I discarded.
    • 0:39:57So traumatic is that might have been made out
    • 0:40:00to be, it's actually just harnessing pretty good human intuition.
    • 0:40:04Indeed, this is what programming is all about, too.
    • 0:40:06It's not about learning a completely new world,
    • 0:40:09but really just how to harness intuition and ideas that you might already
    • 0:40:13have and take naturally but learning how to express
    • 0:40:16them now more succinctly, more precisely,
    • 0:40:18using things called programming languages.
    • 0:40:21Why is an algorithm like that if I found John Harvard better than, ultimately,
    • 0:40:27just doing the first one or even the second
    • 0:40:29and maybe doubling back to check those even pages?
    • 0:40:32Well let's just look at little charts here.
    • 0:40:34Again, we don't have to get into the nuances of numbers,
    • 0:40:36but if we've got like a chart here, xy plot, on the x-axis
    • 0:40:40here I claim as the size of the problem.
    • 0:40:42So measured in the numbers of pages in the phone book.
    • 0:40:45So the farther you go out here, the more pages are in the phone book.
    • 0:40:48And here we have time to solve on the y-axis.
    • 0:40:51So the higher you go up, the more time it's
    • 0:40:54going to be taking to solve that particular problem.
    • 0:40:56So let's just arbitrarily say that the first algorithm, involving
    • 0:41:00like n pages, might be represented graphically like this.
    • 0:41:04No matter the slope, it's a straight line
    • 0:41:06because there's presumably a one to one relationship
    • 0:41:09between numbers of pages and number of seconds or number of page turns.
    • 0:41:13Why?
    • 0:41:13If the phone company adds another page next year
    • 0:41:15because some new people move to town, that's
    • 0:41:18going to require one additional page for me.
    • 0:41:19One to one.
    • 0:41:21If, though, we use the second algorithm, flawed though it was,
    • 0:41:25unless we double back a little bit to fix someone being in between,
    • 0:41:29that's too going to be a straight line, but it's
    • 0:41:31going to be a different slope because now there's a 2 to 1 or a 1 to 2
    • 0:41:35relationship because I'm going to pages at a time.
    • 0:41:38So if the phone company adds another page or another two pages,
    • 0:41:43that still only just one more step.
    • 0:41:45You can see the difference if I kind of draw this.
    • 0:41:47If this is the phone book in question, this number of pages,
    • 0:41:50it might take this many seconds on the yellow line
    • 0:41:53to represent or to find someone like John Harvard.
    • 0:41:57But of course on the first algorithm, the red line,
    • 0:42:00it's literally going to take twice as many steps.
    • 0:42:02And what do the n here mean? n is the go-to variable
    • 0:42:05for computer scientist or programmer just generically representing a number.
    • 0:42:08So if the number of pages in the phone book is n,
    • 0:42:11the number of steps the second algorithm would have taken
    • 0:42:14would be in the worst case n over 2.
    • 0:42:16Half as many because you're going twice as fast.
    • 0:42:18But the third algorithm, actually if you recall your logarithms,
    • 0:42:23looks a little something like this.
    • 0:42:25There's a fundamentally different relationship
    • 0:42:27between the size of the problem and the amount of time required to solve it
    • 0:42:30that technically is log-based, too, again,
    • 0:42:32but it's really the shape that's different.
    • 0:42:35The implication there is that if, for instance, Cambridge and Allston,
    • 0:42:39two different towns here in Massachusetts, merge next year
    • 0:42:41and there's just one phone book that's twice as big,
    • 0:42:44no big deal for that third and final algorithm.
    • 0:42:47Why?
    • 0:42:47You just tear the problem one more time in half,
    • 0:42:50taking one more byte, that's it, not another 1,000
    • 0:42:54bytes just to get to the solution.
    • 0:42:56Put another way, you can walk it way, way,
    • 0:42:59way out here to a much bigger phone book and ultimately
    • 0:43:02that green line is barely going to have budged.
    • 0:43:05So this then is just a way of now formalizing and thinking
    • 0:43:08about what the performance or quality of these algorithms might be.
    • 0:43:15Before we now make one more formalization of the algorithm itself,
    • 0:43:18any questions then on this notion of efficiency or now performance of ideas?
    • 0:43:24Yeah.
    • 0:43:25AUDIENCE: How many phone books have you got?
    • 0:43:28DAVID MALAN: (LAUGHING) A lot of phone books over the years
    • 0:43:31and if you or your parents have any more still somewhere we could definitely
    • 0:43:34use them because they're hard to find.
    • 0:43:37Other questions?
    • 0:43:38But thanks.
    • 0:43:40Other questions here, too?
    • 0:43:42No.
    • 0:43:42Oh, was that a murmur?
    • 0:43:44Yes, over here.
    • 0:43:45AUDIENCE: You could get Harry Potter as a guest speaker.
    • 0:43:48DAVID MALAN: Sorry, say again.
    • 0:43:49AUDIENCE: You could get Harry Potter as a guest speaker.
    • 0:43:51DAVID MALAN: (LAUGHING) Oh, yeah.
    • 0:43:52Hopefully.
    • 0:43:53Then we'd have a little something more to use here.
    • 0:43:56So now if we want to formalize further what it is we just did,
    • 0:44:01we can go ahead and introduce this.
    • 0:44:03A form of code aka pseudocode.
    • 0:44:06Pseudocode is not a specific language, it's not like something
    • 0:44:08we're about to start coding in, it's just a way of expressing yourself
    • 0:44:11in English or any human language succinctly
    • 0:44:14correctly toward an end of getting your idea for an algorithm across.
    • 0:44:18So for instance, here might be how we could
    • 0:44:20formalize the code, the pseudocode for that same algorithm.
    • 0:44:24Step one was pick up the phone book, as I did.
    • 0:44:26Step two might be open to the middle of the phone book,
    • 0:44:29as you proposed that we do first.
    • 0:44:31Step three was probably to look down at the pages, I did.
    • 0:44:34And step four gets a little more interesting
    • 0:44:37because I had to quickly make a decision and ask myself a question.
    • 0:44:40If person is on page, then I should probably just
    • 0:44:44go ahead and call that person.
    • 0:44:45But that probably wasn't the case at least for John Harvard,
    • 0:44:48and I opened the M section.
    • 0:44:50So there's this other question I should now
    • 0:44:52ask else if the person is earlier in the book,
    • 0:44:55then I should tear the problem in half as I did but go left, so
    • 0:44:58to speak, and then not just open to the middle of the left half of the book,
    • 0:45:02but really just go back to step three, repeat myself.
    • 0:45:06Why?
    • 0:45:06Because I can just repeat what I just did, but with a smaller problem
    • 0:45:10having taken this big bite.
    • 0:45:11But, if the person was later in the book,
    • 0:45:14as might have happened with a different person than John Harvard,
    • 0:45:17then I should open to the middle of the right half of the book,
    • 0:45:19again go back to line three, but again, I'm
    • 0:45:21not going to get sucked doing something forever like this
    • 0:45:24because I keep shrinking the size of the problem.
    • 0:45:27Lastly, the only possible scenario that's
    • 0:45:29left, if John Harvard is not on the page and he's not to the left
    • 0:45:32and he's not to the right, what should our conclusion be?
    • 0:45:34AUDIENCE: He's not there.
    • 0:45:36DAVID MALAN: He's not there.
    • 0:45:37He's not listed.
    • 0:45:38So we need to quit in some other form.
    • 0:45:40Now as an aside, it's kind of deliberate that I buried that last question
    • 0:45:44at the end because this is what happens all too often in programming,
    • 0:45:48whether you're new at it or professional,
    • 0:45:50just not considering all possible cases, corner cases if you will,
    • 0:45:54that might not happen that often, but if you don't anticipate them
    • 0:45:58in your own code, pseudocode or otherwise,
    • 0:46:01this is when and why programs might crash
    • 0:46:03or you might say stupid little spinning beach balls or hourglasses
    • 0:46:06or your computer might reboot.
    • 0:46:08Why?
    • 0:46:08It's doing something sort of unpredictable
    • 0:46:11if a human, maybe myself, didn't anticipate this.
    • 0:46:15Like what does this program do if John Harvard is not in the phone book
    • 0:46:18if I had omitted lines 12 and 13?
    • 0:46:20I don't know.
    • 0:46:21Maybe it would behave differently on a Mac or PC
    • 0:46:23because it's sort of undefined behavior.
    • 0:46:26These are the kinds of omissions that frankly you're invariably
    • 0:46:29going to make, bugs you're going to introduce,
    • 0:46:31mistakes you're going to make early on, and me, too, 25 years later.
    • 0:46:35But you'll get better at thinking about those corner cases
    • 0:46:38and handling anything that can possibly go wrong and as a result,
    • 0:46:41your code will be all the better for it.
    • 0:46:44Now the problem ultimately with learning how to program,
    • 0:46:47especially if you've never had experience
    • 0:46:49or even if you do but you learned one language only,
    • 0:46:54is that they all look a little cryptic at first glance.
    • 0:46:57But they do share certain commonalities.
    • 0:46:59In fact, we'll use this pseudocode to define those first.
    • 0:47:02Highlighted in yellow here are what henceforth
    • 0:47:05we're going to start calling functions.
    • 0:47:07Lots of different programming languages exist, but most of them
    • 0:47:10have what we might call functions, which are actions
    • 0:47:13or verbs that solve some smaller problem.
    • 0:47:16That is to say, you might use a whole bunch of functions
    • 0:47:18to solve a bigger problem because each function tends to do
    • 0:47:22something very specific or precise.
    • 0:47:25These then in English might be translated in code, actual computer
    • 0:47:29code, to these things called functions.
    • 0:47:32Highlighted in yellow now are what we might call conditionals.
    • 0:47:35Conditionals are things that you do conditionally
    • 0:47:38based on the answer to some question.
    • 0:47:40You can think of them kind of like forks in the road.
    • 0:47:42Do you go left or go right or some other direction
    • 0:47:45based on the answer to some question?
    • 0:47:47Well, what are those questions?
    • 0:47:48Highlighted now in yellow or what we would call Boolean expressions, named
    • 0:47:52after a mathematician last name Bool, that simply have yes no answers.
    • 0:47:57Or, if you prefer, true or false answers or, heck, if you prefer 1 or 0 answers.
    • 0:48:03We just need to distinguish one scenario from another.
    • 0:48:07The last thing manifests in this pseudocode
    • 0:48:09is what I might highlight now and call loops.
    • 0:48:12Some kind of cycle, some kind of directive that tells us to do something
    • 0:48:16again and again so that I don't need a 1,000-line program to search
    • 0:48:21a 1,000-page phone book, I can get away with a 13-line program but sort
    • 0:48:25of repeat myself inherently in order to solve some problem until I get to that
    • 0:48:30last step.
    • 0:48:31So this then is what we might call pseudocode
    • 0:48:34and indeed there are other characteristics
    • 0:48:37of programs that we'll touch on before long, things like arguments and return
    • 0:48:41values, variables, and more, but unfortunately in most languages,
    • 0:48:46including some we will very deliberately use in this class
    • 0:48:49and that everyone in the real world these days still uses,
    • 0:48:52its programs tend to look like this.
    • 0:48:54This for instance, is a distillation of that very first program
    • 0:48:57I wrote in 1996 in CS50 itself just to print something on the screen.
    • 0:49:02In fact, this version here just tries to print quote unquote, "Hello, world."
    • 0:49:07Which is, dare say, the most canonical first thing that most
    • 0:49:10any programmer ever gets a computer to say just because,
    • 0:49:14but look at this mess.
    • 0:49:15I mean, there's a hash symbol, these angled brackets, parentheses,
    • 0:49:18words like int, curly braces, quotes, parentheses, semicolons, and back
    • 0:49:22slashes.
    • 0:49:23I mean there's more overhead and more syntax and clutter
    • 0:49:26than there is an actual idea.
    • 0:49:28Now that's not to say that you won't be able to understand this before long,
    • 0:49:32because honestly there's not that many patterns, indeed programming languages
    • 0:49:36have typically a much smaller vocabulary than any actual human language,
    • 0:49:40but at first it might indeed look quite cryptic.
    • 0:49:43But you can perhaps infer I have no idea what these other lines do yet,
    • 0:49:47but "Hello, world." is presumably quote unquote what
    • 0:49:50will be printed on the screen.
    • 0:49:52But what we'll do today, after a short break,
    • 0:49:55and set the stage for next week is introduce
    • 0:49:57these exact same ideas in just a bit using Scratch,
    • 0:50:00something that you yourselves might have used
    • 0:50:01when you're quite younger but without the same vocabulary applied
    • 0:50:05to those ideas.
    • 0:50:06The upside of what we'll soon do using Scratch, this graphical programming
    • 0:50:11language from our friends down the road at MIT, it'll let us today
    • 0:50:14start to drag and drop things that look like puzzle pieces that interlock
    • 0:50:19together if it makes logical sense to do so,
    • 0:50:21but without the distraction of hashes, parentheses,
    • 0:50:24curly braces, angle brackets, semicolons, and things
    • 0:50:27that are quite beside the point.
    • 0:50:29But for now, let's go ahead and take a 10 minute break here
    • 0:50:31and when we resume, we will start programming.
    • 0:50:33So this on the screen is a language called
    • 0:50:37C something that will dive into next week and thankfully
    • 0:50:40this now on the screen is another language called Python
    • 0:50:43that we'll also take a look at in a few weeks before long along
    • 0:50:46with other languages along the way.
    • 0:50:48Today though, and for this first week, week zero, so to speak,
    • 0:50:52we use Scratch because again it will allow
    • 0:50:54us to explore some of those programming fundamentals
    • 0:50:56that will be in C and in Python and in JavaScript and other languages, too,
    • 0:51:01but in a way where we don't have to worry about the distractions of syntax.
    • 0:51:05So the world of Scratch looks like this.
    • 0:51:08It's a web-based or downloadable programming environment
    • 0:51:10that has this layout here by default. On the left here
    • 0:51:13we'll soon see is a palette of puzzle pieces, programming blocks that
    • 0:51:17represent all of those ideas we just discussed.
    • 0:51:20And by dragging and dropping these puzzle pieces
    • 0:51:22or blocks over this big area and connecting them together,
    • 0:51:26if it makes logical sense to do so, we'll
    • 0:51:28start programming in this environment.
    • 0:51:31The environment allows you to have multiple sprites, so to speak.
    • 0:51:34Multiple characters, things like a cat or anything
    • 0:51:36else, and those sprites exist in this rectangular world
    • 0:51:41up here that you can full screen to make bigger and this here by default
    • 0:51:44is Scratch, who can move up, down, left, right and do many more things, too.
    • 0:51:48Within its Scratch's world you can think of it
    • 0:51:51as perhaps a familiar coordinate system with Xs and Ys
    • 0:51:55which is helpful only when it comes time to position things on the screen.
    • 0:51:59Right now Scratch is at the default, 0,0, where x equals 0 and y equals 0.
    • 0:52:04If you were to move the cat way up to the top, x would stay zero,
    • 0:52:08y would be positive 180.
    • 0:52:10If you move the cat all the way to the bottom, x would stay zero,
    • 0:52:13but y would now be negative 180.
    • 0:52:15And if you went left, x would become negative 240 but y would stay 0,
    • 0:52:20or to the right x would be 240 and y would stay zero.
    • 0:52:24So those numbers generally don't so much matter because you can just
    • 0:52:28move relatively in this world up, down, left, right,
    • 0:52:30but when it comes time to precisely position
    • 0:52:33some of these sprites or other imagery, it'll
    • 0:52:36be helpful just to have that mental model off up, down, left, and right.
    • 0:52:39Well let's go ahead and make perhaps the simplest of programs here.
    • 0:52:42I'm going to switch over to the same programming environment
    • 0:52:45now for a tour of the left hand side.
    • 0:52:48So by default selected here are the category in blue motion,
    • 0:52:53which has a whole bunch of puzzle pieces or blocks that relate to motion.
    • 0:52:57And whereas Scratch as a graphical language
    • 0:52:59categorizes things by the type of things that these pieces do,
    • 0:53:03we'll see that throughout this whole palette
    • 0:53:05we'll have functions and variables and conditionals
    • 0:53:08and Boolean expressions and more each in a different color and shape.
    • 0:53:11So for instance, moving 10 steps or turning one way or the other
    • 0:53:14would be functions categorized here as things like motion.
    • 0:53:18Under looks in purple, you might have speech bubbles
    • 0:53:21that you can create by dragging and dropping
    • 0:53:23these that might say "hello" or whatever for some number of seconds.
    • 0:53:27Or you could switch costumes, change the cat to look like a dog or a bird
    • 0:53:31or anything else in between.
    • 0:53:33Sounds, too.
    • 0:53:34You can play sounds like "meow" or anything you might import or record,
    • 0:53:37yourself.
    • 0:53:38Then there's these things Scratch calls events and the most important of these
    • 0:53:41is the first, when green flag clicked.
    • 0:53:43Because if we look over to the right of Scratch's world here,
    • 0:53:46this rectangular region has this green flag and red stop
    • 0:53:50sign up above, one of which is for Play one of which is for Stop
    • 0:53:53and so that's going to allow us to start and stop our actual programs
    • 0:53:57when that green flag is initially clicked.
    • 0:54:00But you can listen for other types of events when the spacebar is pressed
    • 0:54:04or something else, when this sprite is clicked or something else.
    • 0:54:08Here you already see like a programmer's incarnation of things
    • 0:54:12you and I take for granted like every day now on our phones.
    • 0:54:15Any time you tap an icon or drag your finger or hit a button on the side.
    • 0:54:19These are what a programmer would call events,
    • 0:54:21things that happen and are often triggered by us
    • 0:54:24humans and things that a program be it in Scratch or Python
    • 0:54:28or C or anything else can listen for and respond to.
    • 0:54:31Indeed, that's why when you tap the phone icon on your phone,
    • 0:54:34the phone application starts up because someone
    • 0:54:37wrote software that's listening for a finger press on that particular icon.
    • 0:54:41So Scratch has these same things, too.
    • 0:54:43Under Control in orange, you can see that we
    • 0:54:46can wait for one second or repeat something
    • 0:54:48some number of times, 10 by default, but we
    • 0:54:50can change anything in these white circles to anything else.
    • 0:54:53There's another puzzle piece here forever,
    • 0:54:55which implies some kind of loop where we can do something again and again.
    • 0:54:58Even though it seems a little tight, there's
    • 0:54:59not much room to fit something there, Scratch
    • 0:55:01is going to have these things grow and shrink
    • 0:55:03however we want to fill similarly shaped pieces.
    • 0:55:06Here are those conditionals.
    • 0:55:07If something is true or false, then do this next thing.
    • 0:55:12And that's how we can put in this little trapezoid-like shape.
    • 0:55:15Some form of Boolean expression, a question with a yes/no, true/false,
    • 0:55:19or one/zero answer and decide whether to do something or not.
    • 0:55:22You can combine these things, too.
    • 0:55:25If something is true, do this, else do this other thing.
    • 0:55:28And you can even tuck one inside of the other
    • 0:55:30if you want to ask three or four or more questions.
    • 0:55:34Sensing, too, is going to be a thing.
    • 0:55:35You can ask questions aka Boolean expressions like is the sprite touching
    • 0:55:40the mouse pointer, the arrow on the screen?
    • 0:55:42So that you can start to interact with these programs.
    • 0:55:45What is the distance between a sprite and a mouse pointer?
    • 0:55:47You can do simple calculations just to figure out maybe
    • 0:55:50if the enemy is getting close to the cat.
    • 0:55:52Under Operator some lower level stuff like math, but also the ability
    • 0:55:56to pick random numbers, which for a game is great
    • 0:55:58because then you can kind of vary the difficulty
    • 0:56:00or what's happening in a game without the same game playing
    • 0:56:03the same way every time.
    • 0:56:04And you can combine ideas.
    • 0:56:06Something and something must be true in order to make that kind of decision
    • 0:56:10before.
    • 0:56:10Or we can even join two words together.
    • 0:56:12Says apple and banana by default, but you can type in or drag and drop
    • 0:56:15whatever you want there to combine multiple words into full,
    • 0:56:19larger sentences.
    • 0:56:21Then lastly down here, there's in orange things called variables.
    • 0:56:25In math we've obviously got x and y and whatnot.
    • 0:56:27In programming we'll have the same ability
    • 0:56:29to store in these named symbols, x or y, values that we care about.
    • 0:56:35Numbers or letters or words or colors or anything, ultimately.
    • 0:56:39But in programming you'll see that it's much more conventional not to just
    • 0:56:42use simple letters like x and y and z, but to actually
    • 0:56:46give variables full singular or plural words to describe what they are.
    • 0:56:52Then lastly, if this isn't enough color blocks for you,
    • 0:56:56you can create your own blocks.
    • 0:56:58Indeed, this is going to be a programming principle we'll apply today
    • 0:57:01and with the first problem set whereby once you start to assemble these puzzle
    • 0:57:05pieces and you realize, oh, would have been nice if those several pieces could
    • 0:57:10have just been replaced by one had MIT thought to give me that
    • 0:57:13one puzzle piece, you yourself can make your own blocks
    • 0:57:16by connecting these all together, giving them a name, and boom,
    • 0:57:19a new puzzle piece will exist.
    • 0:57:21So let's do the simplest, most canonical programs
    • 0:57:24here, starting up with control, and I'm going
    • 0:57:26to click and drag and drop this thing here when green flag clicked.
    • 0:57:30Then I'm going to grab one more, for instance under Looks,
    • 0:57:33and under Looks I'm going to go ahead and just say
    • 0:57:35something like initially not just Hello but the more canonical
    • 0:57:40Hello comma world.
    • 0:57:42Now you might guess that in this programming environment,
    • 0:57:44I can go over here now and click the green flag and voila,
    • 0:57:48Hello comma world.
    • 0:57:49So that's my first program and obviously much
    • 0:57:51more user friendly than typing out the much more cryptic text that we
    • 0:57:54saw on the screen that you, too, will type out next week.
    • 0:57:57But for now, we'll just focus on these ideas, in this case, a function.
    • 0:58:00So what it is that just happened?
    • 0:58:02This purple block here is Say, that's the function,
    • 0:58:05and it seems to take some form of input in the white oval, specifically Hello
    • 0:58:09comma world.
    • 0:58:10Well this actually fits the paradigm that we looked
    • 0:58:12at earlier of just inputs and outputs.
    • 0:58:15So if I may, if you consider what this puzzle piece is doing,
    • 0:58:18it actually fits this model.
    • 0:58:19The input in this case is going to be Hello comma world in white.
    • 0:58:23The algorithm is going to be implemented as a function by MIT called Say
    • 0:58:28and the output of that is going to be some kind of side effect,
    • 0:58:30like the cat and the speech bubble are saying Hello, world.
    • 0:58:33So already even that simple drag and drop
    • 0:58:36mimics exactly this relatively simple mental model.
    • 0:58:40So let's take things further.
    • 0:58:41Let's go ahead now and make the program a little more interactive so
    • 0:58:44that it says something like Hello, David, or Hello, Carter,
    • 0:58:47or Hello to you specifically.
    • 0:58:49And for this, I'm going to go under Sensing.
    • 0:58:51And you might have to poke around to find these things the first time
    • 0:58:54around, but I've done this a few times so I kind of know where things are
    • 0:58:57and what color.
    • 0:58:58There's this function here.
    • 0:58:59Ask what's your name, but that's in white,
    • 0:59:01so we can change the question to anything
    • 0:59:03we want, and it's going to wait for the human to type in their answer.
    • 0:59:07This function called Ask is a little different
    • 0:59:10from the Say block, which just had this side effect of printing a speech
    • 0:59:14bubble to the screen.
    • 0:59:15The ask function is even more powerful in that after it asks the human to type
    • 0:59:19something in.
    • 0:59:20This function is going to hand you back what
    • 0:59:23they typed in in the form of what's called a return value, which
    • 0:59:27is stored ultimately and by default this thing called Answer.
    • 0:59:30This little blue oval here called Answer is again
    • 0:59:33one of these variables that in math would
    • 0:59:35be called just x or y but in programming we're saying what it does.
    • 0:59:38So I'm going to go ahead and do this.
    • 0:59:40Let me go ahead and drag and drop this block
    • 0:59:41and I want to ask the question before saying anything,
    • 0:59:44but you'll notice that Scratch is smart and it's
    • 0:59:46going to realize I want to insert something in between
    • 0:59:48and it's just going to move things up and down.
    • 0:59:50I'm going to let go and ask the default question, what's your name?
    • 0:59:53And now if I want to go ahead and say hello, David or Carter,
    • 0:59:56let's just do Hello comma, because I obviously
    • 0:59:58don't know when I'm writing the program who's going to use it.
    • 1:00:01So let me now grab another looks block up here, say something again, and now
    • 1:00:07let me go back to Sensing and now grab the return value, represented
    • 1:00:11by this other puzzle piece, and let me just drag and drop it here.
    • 1:00:15Notice it's the same shape, even if it's not quite the same size.
    • 1:00:18Things will grow or shrink as needed.
    • 1:00:20All right, so let's now zoom out.
    • 1:00:22Let me go and stop the old version because I don't want to say Hello,
    • 1:00:25world anymore.
    • 1:00:25Let me hit the green flag and what's my name?
    • 1:00:28All right, David.
    • 1:00:29Enter.
    • 1:00:31Huh.
    • 1:00:32All right, maybe I just wasn't paying close enough attention.
    • 1:00:34Let me try it again.
    • 1:00:35Green flag, D-A-V-I-D, Enter.
    • 1:00:39This seems like a bug.
    • 1:00:41What's the bug or mistake might you think?
    • 1:00:46Yeah?
    • 1:00:46AUDIENCE: Do you need to somehow add them together in the same text box?
    • 1:00:50DAVID MALAN: Yeah, we kind of want to combine them in the same text box.
    • 1:00:53And it's technically a bug because this just looks kind of stupid.
    • 1:00:56It's just saying David after I asked for my name.
    • 1:00:59I'd like it to say maybe Hello then David,
    • 1:01:02but it's just blowing past the Hello and printing David.
    • 1:01:05But let's put our finger on why this is happening.
    • 1:01:07You're right for the solution, but what's the actual fundamental problem?
    • 1:01:10In back.
    • 1:01:11AUDIENCE: So it says hello, but it gets to that last step
    • 1:01:15so quickly you can't see it.
    • 1:01:16DAVID MALAN: Perfect.
    • 1:01:17I mean, computers are really darn fast these days.
    • 1:01:20It is saying Hello, all of us are just too slow in this room
    • 1:01:23to even see it because it's then saying David on the screen so fast as well.
    • 1:01:27So there's a couple of solutions here, and yours is spot on,
    • 1:01:30but just to poke around, you'll see the first example
    • 1:01:32of how many ways in programming be it Scratch or C or Python or anything
    • 1:01:35else, that there are going to be to solve problems?
    • 1:01:38We'll teach you over the course of these weeks,
    • 1:01:40sometimes some ways are better relatively than others,
    • 1:01:43but rarely is there a best way necessarily,
    • 1:01:46because again reasonable people will disagree.
    • 1:01:48And what we'll try to teach you over the coming weeks
    • 1:01:50is how to kind of think through those nuances.
    • 1:01:52And it's not going to be obvious at first glance,
    • 1:01:54but the more programs you write, the more feedback
    • 1:01:57you get, the more bugs that you introduce,
    • 1:01:59the more you'll get your footing with exactly this kind of problem solving.
    • 1:02:03So let me try this in a couple of ways.
    • 1:02:05Up here would be one solution to the problem.
    • 1:02:08MIT anticipated this kind of issue, especially with first-time programmers,
    • 1:02:12and I could just use a puzzle piece that says
    • 1:02:14say the following for two seconds or one second
    • 1:02:17or whatever, then do the same with the next word
    • 1:02:20and it might be kind of a bit of a pause,
    • 1:02:22Hello, one second, two seconds, David, one second, two seconds, but at least
    • 1:02:27it would look a little more grammatically correct.
    • 1:02:29But I can do it a little more elegantly, as you've proposed.
    • 1:02:31Let me go ahead and throw away one of these blocks,
    • 1:02:34and you can just drag and let go and it'll delete itself.
    • 1:02:36Let me go down to Operators because this Join block here is the right shape.
    • 1:02:42So even if you're not sure what goes where, just focus on the shapes first.
    • 1:02:46Let me drag this over here.
    • 1:02:48It grew to fill that.
    • 1:02:50Let me go ahead and say hello comma space.
    • 1:02:52Now it could just say by default Hello, banana,
    • 1:02:55but let me go back to Sensing, Drag answer,
    • 1:03:00and that's going to drag and drop there.
    • 1:03:02So now notice we're sort of stacking or nesting one block on another
    • 1:03:06so that the output of one becomes the input to another, but that's OK here.
    • 1:03:10Let me go ahead and zoom out, hit Stop, and hit Play.
    • 1:03:14All right, what's your name?
    • 1:03:15D-A-V-I-D, Enter, and voila.
    • 1:03:18Now it's presumably as we first intended.
    • 1:03:21[APPLAUSE]
    • 1:03:22(LAUGHING) Oh, thank you.
    • 1:03:28Thank you.
    • 1:03:28No minus 2 this time.
    • 1:03:30So consider that even with this additional example,
    • 1:03:35it still fits the same mental model, but in a little more interesting way.
    • 1:03:38Here's that new function Ask something and wait.
    • 1:03:41And notice that in this case too there's an input, otherwise known
    • 1:03:45henceforth as an argument or a parameter, programming
    • 1:03:48speak for just an input in the context of a function.
    • 1:03:51If we use our drawing as before to represent this thing here,
    • 1:03:54we'll see that the input now is going to be quote unquote "What's your name?"
    • 1:03:58The algorithm is going to be implemented by way of this new puzzle piece,
    • 1:04:02the function called Ask, and the output of that thing this time
    • 1:04:06is not going to be the cat saying anything yet,
    • 1:04:08but rather it's going to be the actual answer.
    • 1:04:12So instead of the visual side effect of the speech bubble appearing,
    • 1:04:15now nothing visible is happening yet.
    • 1:04:18Thanks to this function it's sort of handing me back like a scrap of paper
    • 1:04:22with whatever I typed in written on it so I can reuse D-A-V-I-D one or more
    • 1:04:28times even like I did.
    • 1:04:30Now what did I then do with that value?
    • 1:04:32Well consider that with the subsequent function
    • 1:04:37we had this Say block, too, combined with a join.
    • 1:04:40So we have this variable called Answer, we're joining it
    • 1:04:44with that first argument, Hello.
    • 1:04:46So already we see that some functions like Join
    • 1:04:48can take not one but two arguments, or inputs, and that's fine.
    • 1:04:52The output of Join is presumably going to be Hello, David or Hello, Carter
    • 1:04:57or whatever the human typed in.
    • 1:04:59That output notice is essentially becoming the input to another function,
    • 1:05:04Say, just because we've kind of stacked things
    • 1:05:06or nested them on top of one another.
    • 1:05:08But methodically, it's really the same idea.
    • 1:05:12The input now are two things, Hello comma and the return value
    • 1:05:17from the previous Ask function.
    • 1:05:20The function now is going to be Join, the output is going to be Hello, David.
    • 1:05:23But that Hello, David output is now going
    • 1:05:25to become the input to another function, namely that first block called Say,
    • 1:05:30and that's then going to have the side effect of printing out Hello, David
    • 1:05:34on the screen.
    • 1:05:35So again as sort of sophisticated as ours as yours as others programs
    • 1:05:39are going to get, they really do fit this very simple mental model
    • 1:05:41of inputs and outputs and you just have to learn to recognize the vocabulary
    • 1:05:45and to know what kinds of puzzle pieces or concepts ultimately to apply.
    • 1:05:49But you can ultimately really kind of spice these things up.
    • 1:05:52Let me go back to my program here that just is
    • 1:05:54using the speech bubble at the moment.
    • 1:05:55Scratch's inside has some pretty fancy interactive features, too.
    • 1:05:58I click the Extensions button in the bottom left corner.
    • 1:06:01And let me go ahead and choose the Text to Speech extension.
    • 1:06:05This is using a Cloud service, so if you have an internet connection
    • 1:06:08it can actually talk to the Cloud or a third party service,
    • 1:06:11and this one is going to give me a few new green puzzle pieces, namely
    • 1:06:15the ability to speak something from my speakers
    • 1:06:17instead of just saying it textually.
    • 1:06:19So let me go ahead and drag this.
    • 1:06:21Now notice I don't have to interlock them if I'm just kind of playing around
    • 1:06:24and I want to move some things around.
    • 1:06:25I just want to use this as like a canvas temporarily.
    • 1:06:27Let me go ahead and steal the Join from here,
    • 1:06:30put it there, let me throw away the Say block by just moving it
    • 1:06:34left and letting go, and now let me join this in
    • 1:06:37so I've now changed my program to be a little more interesting.
    • 1:06:41So now let me stop the old version.
    • 1:06:43Let me start the new.
    • 1:06:44What's your name?
    • 1:06:45Type in David.
    • 1:06:46And voila:
    • 1:06:47PROGRAM: Hello, banana.
    • 1:06:51DAVID MALAN: (LAUGHING) OK, minus 2 for real.
    • 1:06:52All right, so what I accidentally threw away there, intentionally
    • 1:06:59for instructional purposes, was the actual answer
    • 1:07:02that came back from the ask block.
    • 1:07:05That's embarrassing.
    • 1:07:06So now if I play this again, let's click the green icon.
    • 1:07:10What's your name?
    • 1:07:11David.
    • 1:07:11And now:
    • 1:07:13PROGRAM: Hello, David.
    • 1:07:14DAVID MALAN: There we go.
    • 1:07:15Hello, David.
    • 1:07:16All right, thank you.
    • 1:07:18[APPLAUSE]
    • 1:07:22OK, so we have these functions then in place, but what more can we do?
    • 1:07:27Well what about those conditionals and loops and other constructs?
    • 1:07:30How can we bring these programs to life so it's not just
    • 1:07:32clicking a button and voila, something's happening?
    • 1:07:34Let's go ahead and make this now even more interactive.
    • 1:07:36Let me go ahead and throw away most of these pieces
    • 1:07:39and let me just spice things up with some more audio under Sound.
    • 1:07:42I'm going to go to Play Sound Meow until done.
    • 1:07:44Here we go, green flag.
    • 1:07:46[MEOW]
    • 1:07:47OK, it's a little loud, but it did exactly do what it said.
    • 1:07:51Let's hear it again.
    • 1:07:52[QUIETER MEOW]
    • 1:07:53OK.
    • 1:07:54It's kind of an underwhelming program eventually
    • 1:07:56since you'd like to think that the cat would just meow on its own, but.
    • 1:07:59[MEOW]
    • 1:07:59I have to keep hitting the button.
    • 1:08:01Well this seems like an opportunity for doing something again and again.
    • 1:08:04So all right, well if I wanted to meow, meow, meow,
    • 1:08:06let me just grab a few of these, or you can even right click or Control click
    • 1:08:09and you can Copy Paste even in code here.
    • 1:08:12Let me play this now.
    • 1:08:13[THREE MEOWS]
    • 1:08:15All right, so now like it's not really emoting
    • 1:08:17happiness in quite the same way.
    • 1:08:19It might be hungry or upset.
    • 1:08:20So let's slow it down.
    • 1:08:22Let me go to Control, wait one second in between,
    • 1:08:25which might be a little less worrisome.
    • 1:08:28Here we go, Play.
    • 1:08:29[THREE SLOWER MEOWS]
    • 1:08:34OK, so if my goal was to make the cat meow three times,
    • 1:08:39I dare say this code or algorithm is correct.
    • 1:08:42But let's now critique its design.
    • 1:08:45Is this well-designed?
    • 1:08:47And if not, why not?
    • 1:08:50What are your thoughts here?
    • 1:08:51Yeah?
    • 1:08:54AUDIENCE: You could use the forever or a repeat to make it more--
    • 1:08:59DAVID MALAN: Yeah, so yeah, agreed.
    • 1:09:01I could use forever or repeat, but let me push a little harder.
    • 1:09:04But why?
    • 1:09:04Like this works, I'm kind of done with the assignments, what's bad about it?
    • 1:09:08AUDIENCE: There's too much repetition.
    • 1:09:10DAVID MALAN: Yeah, there's too much repetition, right?
    • 1:09:12If I wanted to change the sound that the cat is making
    • 1:09:15to a different variant of meow or have it bark instead like a dog,
    • 1:09:18I could change it from the dropdown here apparently,
    • 1:09:21but then I'd have to change it here and then I'd have to change it here,
    • 1:09:24and God, if this were even longer that just gets tedious quickly
    • 1:09:26and you're probably increasing the probability
    • 1:09:28that you're going to screw up and you're going
    • 1:09:29to miss one of the dropdowns or something stupid and introduce a bug.
    • 1:09:32Or, if you wanted to change the number of seconds you're waiting,
    • 1:09:35you've got to change it in two, maybe even more places.
    • 1:09:37Again, you're just creating risk for yourself
    • 1:09:40and potential bugs in the program.
    • 1:09:41So I do like the repeat or the forever idea so that I don't repeat myself.
    • 1:09:45And indeed, what I alluded to being possible,
    • 1:09:48copy pasting earlier, doesn't mean it's a good thing.
    • 1:09:51And in code, generally speaking, when you
    • 1:09:53start to copy and paste puzzle pieces or text next week,
    • 1:09:56you're probably not doing something quite well.
    • 1:09:59So let me go ahead and throw away most of these to get rid of the duplication,
    • 1:10:03keeping just two of the blocks that I care about.
    • 1:10:06Let me grab the Repeat block for now, let me move this inside of the Repeat
    • 1:10:10block, it's going to grow to fit it, let me reconnect all this
    • 1:10:13and change the 10 just to a 3, and now, Play.
    • 1:10:17[THREE SLOW MEOWS]
    • 1:10:22So, better.
    • 1:10:23It's the same thing.
    • 1:10:24It's still correct, but now I've set the stage
    • 1:10:26to let the cat meow, for instance, four times by changing one thing,
    • 1:10:2940 times by changing one thing, or it could just use the Forever block
    • 1:10:33and just walk away and it will meow forever instead.
    • 1:10:35If that's your goal, that would be better.
    • 1:10:37A better design but still correct.
    • 1:10:40But you know what?
    • 1:10:41Now that I have a program that's designed
    • 1:10:42to have a cat meow, wow like why?
    • 1:10:46I mean, MIT invented Scratch, Scratch as a cat,
    • 1:10:49why is there no puzzle piece called Meow?
    • 1:10:51This feels like a missed opportunity.
    • 1:10:52Now to be fair, they gave us all the building blocks
    • 1:10:55with which we could implement that idea, but a principle of programming
    • 1:10:58and really computer science is to leverage what we're
    • 1:11:00going to now start calling Abstraction.
    • 1:11:03We have step-by-step instructions here, the Repeat, the Play,
    • 1:11:07and the Wait that collectively implements this idea
    • 1:11:09that we humans would call meowing.
    • 1:11:11Wouldn't it be nice to abstract away those several puzzle
    • 1:11:14pieces into just one that literally just says what it does, meow?
    • 1:11:18Well here's where we can make our own blocks.
    • 1:11:20Let me go over here to Scratch under the pink block category
    • 1:11:25here and let me click Make a Block.
    • 1:11:28Here I see a slightly different interface
    • 1:11:30where I can choose a name for it and I'm going to call it Meow.
    • 1:11:33I'm going to keep it simple.
    • 1:11:33That's it.
    • 1:11:34No inputs to meow yet.
    • 1:11:35I'm just going to click OK.
    • 1:11:37Now I'm just going to clean this up a bit here.
    • 1:11:40Let me drag and drop Play Sound and Wait over here.
    • 1:11:44And you know what?
    • 1:11:45I'm just going to drag this way down here, way down
    • 1:11:48here because now that I'm done implementing Meow,
    • 1:11:50I'm going to literally abstract it away, sort of out of sight,
    • 1:11:53out of mind, because now notice at top left there is a new pink puzzle
    • 1:11:58piece called Meow.
    • 1:11:59So at this point, I'd argue it doesn't really matter how Meow is implemented.
    • 1:12:04Frankly, I don't know how Ask or Say was implemented by MIT.
    • 1:12:07They abstracted those things away for us.
    • 1:12:09Now I have a brand new puzzle piece that just says what it is.
    • 1:12:12And this is now still correct, but arguably better design.
    • 1:12:17Why?
    • 1:12:17Because it's just more readable to me, to you,
    • 1:12:20it's more maintainable when you look at your code
    • 1:12:22a year from now for the first time because you're sort of finally looking
    • 1:12:24back at the very first program you wrote.
    • 1:12:26It says what it does.
    • 1:12:28The function itself has semantics, which conveys what's going on.
    • 1:12:31If you really care about how Meow is implemented,
    • 1:12:34you could scroll down and start to tinker with the underlying
    • 1:12:37implementation details, but otherwise you don't need to care anymore.
    • 1:12:42Now I feel like there's an even additional opportunity
    • 1:12:45here for abstraction and to factor out some of this functionality.
    • 1:12:50It's kind of lame that I have this Repeat block that
    • 1:12:53lets me call the Meow function, so to speak, use the Meow function
    • 1:12:57three times.
    • 1:12:58Wouldn't it be nice if I could just call them Meow function,
    • 1:13:01aka use the Meow function, and pass it in input that tells the puzzle
    • 1:13:05piece how many times I want it to meow?
    • 1:13:07Well let me go ahead and zoom out and scroll down.
    • 1:13:10Let me right click or Control click on the pink piece here and choose Edit,
    • 1:13:14or I could just start from scratch, no pun intended, with a new one.
    • 1:13:17Now here, rather than just give this thing a name Meow, let me go ahead
    • 1:13:21and add an input here.
    • 1:13:23I'm going to go ahead and type in, for instance, n,
    • 1:13:25for number of times to meow, and just to make
    • 1:13:28this even more user friendly and self descriptive,
    • 1:13:31I'm going to add a label, which has no functional impact,
    • 1:13:33it's just an aesthetic, and I'm just going
    • 1:13:35to say Times, just to make it read more like English
    • 1:13:38in this case that tells me what the puzzle piece does.
    • 1:13:40Now I'm going to click OK.
    • 1:13:42And now I need to refine this a little bit.
    • 1:13:44Let me go ahead and grab under Control a repeat block,
    • 1:13:51let me move the Play, Sound, and Wait, into the repeat block.
    • 1:13:54I don't want 10 and I also don't want 3 here.
    • 1:13:57What I want now is this n that is my actual variable that Scratch
    • 1:14:02is creating for me that represents whatever input the human programmer
    • 1:14:05provides.
    • 1:14:06Notice that snaps right in place.
    • 1:14:08Let me connect this and now voila, I have an even fancier version of Meow
    • 1:14:12that is parameterized.
    • 1:14:13It takes input that affects its behavior accordingly.
    • 1:14:17Now I'm going to scroll back up, because out of sight, out of mind,
    • 1:14:20I just care that Meow exists.
    • 1:14:21Now I can tighten up my code, so to speak, use even fewer lines
    • 1:14:25to do the same thing by throwing away the Repeat block,
    • 1:14:28reconnecting this new puzzle piece here that takes an input like 3 and voila,
    • 1:14:32now we're really programming, right?
    • 1:14:34We've not made any forward progress functionally.
    • 1:14:37The thing just mouse three times.
    • 1:14:39But it's a better design.
    • 1:14:41As you program more and more, these are the kinds
    • 1:14:44of instincts still start to acquire so that one,
    • 1:14:46you can start to take a big assignment, a big problem set, something
    • 1:14:49for homework even, that feels kind of overwhelming at first, like, oh my God
    • 1:14:53where do I even begin?
    • 1:14:54But if you start to identify what are the subproblems of a bigger problem?
    • 1:14:58Then you can start making progress.
    • 1:15:00I do this to this day where if I have to tackle some programming-related project
    • 1:15:04it's so easy to drag my feet and ugh, it's going to take forever to start,
    • 1:15:08until I just start writing down like a to do list
    • 1:15:11and I start to modularize the program and say, all right, well
    • 1:15:14what do I want this thing to do?
    • 1:15:15Meowing.
    • 1:15:15What's that mean?
    • 1:15:16I've got to have it say something on the screen.
    • 1:15:18All right, I need to have it say something on the screen
    • 1:15:20some number of times.
    • 1:15:21Like literally a mental or written checklist, or pseudocode code,
    • 1:15:25if you will, in English on a piece of paper or text file,
    • 1:15:28and then you can decide, OK, the first thing I
    • 1:15:30need to do for homework to solve this real world problem,
    • 1:15:33I just need a Meow function.
    • 1:15:34I need to use a bunch of other code, too,
    • 1:15:36but I need to create a Meow function and boom,
    • 1:15:39now you have a piece of the problem solved not unlike we did with the phone
    • 1:15:43book there, but in this case, we'll have presumably other problems to solve.
    • 1:15:47All right, so what more can we do?
    • 1:15:49Let's add a few more pieces to the puzzle here.
    • 1:15:51Let's actually interact with the cat now.
    • 1:15:53Let me go ahead and now when the green flag is clicked, let me go ahead
    • 1:15:56and ask a question using an event here.
    • 1:15:59Let me go ahead and say, let's see, I want
    • 1:16:03to do something like implement the notion of petting the cat.
    • 1:16:07So if the cursor is touching the cat like here, something like this,
    • 1:16:12it'd be cute if the cat meows like you're petting a cat.
    • 1:16:15So I'm going to ask the question, when the green flag is clicked,
    • 1:16:17if let's see I think I need Sensing.
    • 1:16:21So if touching mouse pointer, this is way too big
    • 1:16:23but again the shape is fine, so there goes.
    • 1:16:25Grew to fill.
    • 1:16:26And then if it's touching the mouse pointer,
    • 1:16:28that is if the cat to whom this script or this program,
    • 1:16:32any time I attach puzzle pieces MIT calls them a script
    • 1:16:35or like a program, if you will, let me go ahead then and choose a sound
    • 1:16:39and say play sound meow until done.
    • 1:16:42All right, so here it is to be clear.
    • 1:16:44When the green flag is clicked, ask the question,
    • 1:16:46if the cat is touching the mouse pointer then place sound meow.
    • 1:16:50Here we go.
    • 1:16:51Play.
    • 1:16:53[SILENCE]
    • 1:16:56All right, let's try again.
    • 1:16:58Play.
    • 1:16:58[SILENCE]
    • 1:17:01Huh.
    • 1:17:02I'm worried it's not Scratch's fault. Feels like mine.
    • 1:17:07What's the bug here?
    • 1:17:10Why doesn't this work?
    • 1:17:12Yeah, in back, who just turned.
    • 1:17:14AUDIENCE: [INAUDIBLE]
    • 1:17:20DAVID MALAN: Yeah, the problem is the moment I click that green flag,
    • 1:17:23Scratch asks the question, is the cat touching the mouse pointer?
    • 1:17:26And obviously it's not because the cursor was like up there a moment ago
    • 1:17:29and it's not down there.
    • 1:17:31It's fine if I move the cursor down there, but too late.
    • 1:17:34The program already asked the question.
    • 1:17:35The answer was no or false or zero, however you want to think about it,
    • 1:17:39so no sound was played.
    • 1:17:40So what might be the solution here be?
    • 1:17:43I could move my cursor quickly, but that feels
    • 1:17:45like never going to work out right.
    • 1:17:47Other solutions here?
    • 1:17:49Yeah, in way back?
    • 1:17:50Could you use the forever loop?
    • 1:17:53The Forever loop.
    • 1:17:54So I could indeed use this Forever loop because if I want my program
    • 1:17:57to just constantly listen to me, well let's literally do something forever,
    • 1:18:00or at least forever as long as the program is
    • 1:18:02running until I explicitly hit Stop.
    • 1:18:05So let me grab that.
    • 1:18:05Let me go to Control, let me grab the Forever block,
    • 1:18:09let me move the If inside of this Forever block, reconnect this,
    • 1:18:12go back up here, click the green flag, and now nothing's happened yet,
    • 1:18:16but let me try moving my cursor now.
    • 1:18:18[MEOW]
    • 1:18:19Oh.
    • 1:18:20So now.
    • 1:18:21[MEOW]
    • 1:18:22That's kind of cute.
    • 1:18:23So now the cat is actually responding and it's
    • 1:18:24going to keep doing this again and again.
    • 1:18:27So now we have this idea of taking these different ideas, these different puzzle
    • 1:18:31pieces, assembling them into something more complicated.
    • 1:18:34I could definitely put a name to this.
    • 1:18:36I could create a custom block, but for now
    • 1:18:38let's just consider what kind of more interactivity we can do.
    • 1:18:40Let me go ahead and do this.
    • 1:18:41By again grabbing a, when green flag clicked,
    • 1:18:45let me go ahead and click the video sensing,
    • 1:18:48and I'm going to rotate the laptop because otherwise we're
    • 1:18:51going to get a little inception thing here where the camera is picking up
    • 1:18:54the camera is up there.
    • 1:18:55So I'm going to go reveal to you what's inside the lectern
    • 1:18:58here while we rotate this.
    • 1:19:02Now that we have a non video backdrop, I'm going to say this.
    • 1:19:07Instead of the green flag clicked, actually, I'm
    • 1:19:09going to say when the video motion is greater than some arbitrary
    • 1:19:13measurement of motion, I'm going to go ahead and play sound meow until done.
    • 1:19:19And then I'm going to get out of the way.
    • 1:19:21So here's the cat.
    • 1:19:23We'll put them on top of there.
    • 1:19:26[MEOW]
    • 1:19:27OK.
    • 1:19:28All right, and here we go.
    • 1:19:30[MEOW]
    • 1:19:33So my hand is moving faster than 50 something or other,
    • 1:19:35whatever the unit of measure is.
    • 1:19:37[MEOW]
    • 1:19:39AUDIENCE: Aw.
    • 1:19:39DAVID MALAN: (LAUGHING) Thank you.
    • 1:19:40So now we have an even more interactive version.
    • 1:19:43[MEOW]
    • 1:19:44But I think if I sort of slowly.
    • 1:19:48[LAUGHING]
    • 1:19:50(LAUGHING) Right?
    • 1:19:51It's completely creepy, but I'm not like exceeding the threshold--
    • 1:19:56[MEOW]
    • 1:19:57Until finally my hand moves as fast as that.
    • 1:19:59And so here actually is an opportunity to show you
    • 1:20:02something a former student did.
    • 1:20:03Let me go ahead here and--
    • 1:20:05[MEOW TWICE]
    • 1:20:05OK, got to stop this.
    • 1:20:08Let me go ahead and zoom out of this in just a moment.
    • 1:20:11[MEOW]
    • 1:20:11If someone would be--
    • 1:20:12[LAUGHING]
    • 1:20:13(LAUGHING) If someone would be comfortable
    • 1:20:15coming up not only masked but also on camera on the internet
    • 1:20:17I thought we'd play one of your former classmate's projects here up on stage.
    • 1:20:21Would anyone like to volunteer here and be up on stage?
    • 1:20:24Who's that?
    • 1:20:24Yeah.
    • 1:20:25Come on down.
    • 1:20:25What's your name?
    • 1:20:26AUDIENCE: Sahar.
    • 1:20:27DAVID MALAN: Sahar.
    • 1:20:28All right, come on down.
    • 1:20:29Let me get it set up for you here.
    • 1:20:31[MEOW]
    • 1:20:32[APPLAUSE]
    • 1:20:37[MEOW]
    • 1:20:42All right, let me go ahead and full screen this here.
    • 1:20:45So this is whack-a-mole by one of your firmer predecessors.
    • 1:20:50It's going to use the camera focusing on your head, which will have
    • 1:20:53to position inside of this rectangle.
    • 1:20:55Have you ever played the whack-a-mole game at an arcade?
    • 1:20:57AUDIENCE: Yeah.
    • 1:20:57DAVID MALAN: OK.
    • 1:20:58So for those who haven't, these little moles pop up
    • 1:21:00and with a very fuzzy hammer you sort of hit down.
    • 1:21:02You though, if you don't mind, you're going
    • 1:21:04to use your head to do this virtually.
    • 1:21:06So let's line up your head with this red rectangle, if you could,
    • 1:21:11we'll do beginner.
    • 1:21:12[MUSIC PLAYING]
    • 1:21:14All right, here we go.
    • 1:21:15Sahar.
    • 1:21:17Give it a moment.
    • 1:21:18OK, come a little closer.
    • 1:21:19[DINGING]
    • 1:21:20And now hit the moles with your head.
    • 1:21:23[DING]
    • 1:21:24There we go, one point.
    • 1:21:25[DING]
    • 1:21:26One point.
    • 1:21:28[DINGING]
    • 1:21:30Nice.
    • 1:21:3215 seconds to go.
    • 1:21:33There we go.
    • 1:21:34Oh yeah.
    • 1:21:34One point.
    • 1:21:36[LAUGHING]
    • 1:21:38[DINGING]
    • 1:21:40Six seconds.
    • 1:21:42AUDIENCE: Oh no.
    • 1:21:43DAVID MALAN: There we go.
    • 1:21:44Quick!
    • 1:21:45[DINGING]
    • 1:21:47All right, a round of applause for Sahar.
    • 1:21:49Thank you.
    • 1:21:50[APPLAUSE]
    • 1:21:57So beyond having a little bit of fun here,
    • 1:21:59the goal was to demonstrate that by using
    • 1:22:01some fairly simple, primitive, some basic building blocks
    • 1:22:04but assembling them in a fun way with some music, maybe
    • 1:22:06some new costumes or artwork, you can really bring programs to life.
    • 1:22:10But at the end of the day, the only puzzle pieces really involved
    • 1:22:13were ones like the ones I just dragged and dropped and a few more,
    • 1:22:16because there were clearly lots of moles.
    • 1:22:18So the student probably created a few different sprites, not a single cap,
    • 1:22:21but at least four different moles.
    • 1:22:23They had like some kind of graphic on the screen that showed Sahar
    • 1:22:26where to position her head.
    • 1:22:27There were some kind of timer, maybe a variable
    • 1:22:29that every second was counting down.
    • 1:22:32So you can imagine taking what looks like a pretty impressive project
    • 1:22:35at first glance, and perhaps overwhelming
    • 1:22:37to solve yourself, but just think about what are the basic building blocks?
    • 1:22:40And pluck off one piece of the puzzle, so to speak, at a time.
    • 1:22:45So indeed if we rewind a little bit.
    • 1:22:47Let me go ahead here and introduce a program
    • 1:22:50that I myself made back in graduate school
    • 1:22:53when Scratch was first being developed by MIT.
    • 1:22:55Let me go ahead and open here, give me just one second,
    • 1:22:59something that I called back in the day Oscar Time that
    • 1:23:03looks a little something like this.
    • 1:23:05If I fullscreen it and hit Play.
    • 1:23:07[MUSIC - SESAME STREET, "I LOVE TRASH"]
    • 1:23:10OSCAR THE GROUCH: (SINGING) Oh, I love trash.
    • 1:23:12DAVID MALAN: So you'll notice a piece of trash is falling.
    • 1:23:15I can click on it and drag and as I get close and close to the trash can notice
    • 1:23:18OSCAR THE GROUCH: (SINGING) Anything ragged or--
    • 1:23:20DAVID MALAN: It wants to go in, it seems.
    • 1:23:22And if I let go--
    • 1:23:23OSCAR THE GROUCH: (SINGING) Yes, I--
    • 1:23:25DAVID MALAN: One point.
    • 1:23:26Here comes another.
    • 1:23:27OSCAR THE GROUCH: (SINGING) If you really want to see something trashy--
    • 1:23:29DAVID MALAN: I'll do the same, two points.
    • 1:23:30OSCAR THE GROUCH: (SINGING) I have here a sneaker that's tattered and worn--
    • 1:23:32DAVID MALAN: There's a sneaker falling from the sky,
    • 1:23:34so another sprite of some sort.
    • 1:23:35OSCAR THE GROUCH: (SINGING) The laces are torn.
    • 1:23:37A gift from my mother--
    • 1:23:39DAVID MALAN: I can also get just a little lazy
    • 1:23:41and just let them fall into the trash themself if I want to.
    • 1:23:45So you can see it doesn't have to do with my mouse cursor,
    • 1:23:48it has to do apparently with the distance here.
    • 1:23:50Let's listen a little further.
    • 1:23:52I think some additional trash is about to make its appearance.
    • 1:23:56Presumably there's some kind of variable that's keeping track of this score.
    • 1:23:59OSCAR THE GROUCH: (SINGING) I love--
    • 1:24:01DAVID MALAN: OK, let's see what the last chorus here is.
    • 1:24:03OSCAR THE GROUCH: (SINGING) Rotten stuff.
    • 1:24:05I have here some newspaper, crusty and
    • 1:24:08DAVID MALAN: OK, and thus he continues.
    • 1:24:10And the song actually goes on and on and on
    • 1:24:13and I do not have fond memories of implementing this and hearing
    • 1:24:16this song for like 10 straight hours, but it's
    • 1:24:19a good example to just consider how was this program composed?
    • 1:24:22How did I go about implementing it the first time around?
    • 1:24:25And let me go ahead and open up some programs now
    • 1:24:27that I wrote in advance just so that we could
    • 1:24:29see how these things are assembled.
    • 1:24:31Honestly, the first thing I probably did was probably
    • 1:24:35to do something a little like this.
    • 1:24:37Here is just a version of the program where
    • 1:24:39I set out to solve just one problem first
    • 1:24:42of planting a lamp post in the program.
    • 1:24:44Right?
    • 1:24:45I kind of had a vision of what I wanted.
    • 1:24:47You know, it evolved over time, certainly,
    • 1:24:48but I knew I wanted trash to fall, I wanted
    • 1:24:50a cute little Oscar the Grouch to pop out
    • 1:24:51of the trashcan, and some other stuff, but wow that's a lot
    • 1:24:54to just tackle all at once.
    • 1:24:56I'm going to start easy, download a picture of a lamp post,
    • 1:24:59and then drag and drop it into the stage as a costume and boom, that's
    • 1:25:03version one.
    • 1:25:04It doesn't functionally do anything.
    • 1:25:06I mean, literally that's the code that I wrote to do this.
    • 1:25:09All I did was use like the Backdrops feature
    • 1:25:11and drag and drop and move things around,
    • 1:25:13but it got me to version one of my program.
    • 1:25:16Then what might version two be?
    • 1:25:18Well I considered what piece of functionality
    • 1:25:21frankly might be the easiest to pluck off next and the trash can.
    • 1:25:24That seems like a pretty core piece of functionality.
    • 1:25:26It just needs to sit there most of the time.
    • 1:25:29So the next thing I probably did was to open up,
    • 1:25:32for instance, the trash can version here that looks a little something now
    • 1:25:38like this.
    • 1:25:38So this time I'll show you what's inside here.
    • 1:25:40There is some code, but not much.
    • 1:25:43Notice at bottom right I change the default cat to a picture of a trashcan,
    • 1:25:47instead, but it's the same principle that I can control.
    • 1:25:50And then over here I added this code.
    • 1:25:52When the green flag is clicked, switch the costume
    • 1:25:55to something I arbitrarily called Oscar 1.
    • 1:25:57So I found a couple of different pictures
    • 1:25:59of a trash can, one that looks closed, one that looks partly open,
    • 1:26:02and eventually one that has Oscar coming out,
    • 1:26:04and I just gave them different names.
    • 1:26:06So I said Switch to Oscar 1, which is the closed one by default,
    • 1:26:09then forever do the following: if touching the mouse pointer,
    • 1:26:13then switch the costume to Oscar 2, else switch to Oscar 1.
    • 1:26:18That is to say, I just wanted to implement this idea of the can opening
    • 1:26:22and closing, even if it's not exactly what I wanted ultimately,
    • 1:26:24I just wanted to make some forward progress.
    • 1:26:26So here, when I run this program by clicking Play, notice what happens.
    • 1:26:32Nothing yet, but if I get closer to the trash can,
    • 1:26:36it indeed pops open because it's forever listening
    • 1:26:40for whether the sprite, the trash can in this case,
    • 1:26:43is touching the mouse pointer.
    • 1:26:44And that's it.
    • 1:26:45That was version 2, if you will.
    • 1:26:48If I went in now and added the lamp post and compose the program together,
    • 1:26:51now we're starting to make progress.
    • 1:26:52Right?
    • 1:26:53Now it would look a little something more like the program
    • 1:26:55I intended ultimately to create.
    • 1:26:57What piece did I probably bite off after that?
    • 1:27:00Well, I think what I did is I probably decided
    • 1:27:02let me implement one of the pieces of trash, not the shoe in the newspaper
    • 1:27:06all at once.
    • 1:27:07Let's just get one piece of trash working correctly first.
    • 1:27:10So let me go ahead and open this one.
    • 1:27:12And again, all of these examples will be available on the course's website
    • 1:27:16so you can see all of these examples, too.
    • 1:27:18It's not terribly long, I just implement it in advance
    • 1:27:21so we could flip through kind of quickly.
    • 1:27:23Here's what I did here.
    • 1:27:25On the right hand side, I turned my sprite into a piece of trash
    • 1:27:28this time instead of a cat, instead of a trash can,
    • 1:27:31and I also created, with Carter's help, a second sprite, this one a floor.
    • 1:27:36It's literally just a black line because I just wanted initially
    • 1:27:39to have some notion of a floor so I could detect
    • 1:27:42if the trash is touching the floor.
    • 1:27:44Now without seeing the code yet, just hearing that description,
    • 1:27:48why might I have wanted the second sprite and this black line for a floor
    • 1:27:53with the trash intending to fall from the sky?
    • 1:27:55What might I have been thinking?
    • 1:27:56Like what problem might I be trying to solve?
    • 1:27:58Yeah?
    • 1:27:58AUDIENCE: You don't want the first sprite to go through it.
    • 1:28:01DAVID MALAN: Yeah, you don't want the first sprite to start at the top,
    • 1:28:04go through, and then boom, you completely lose it.
    • 1:28:06That would not be a very useful thing.
    • 1:28:09Or it would seem to maybe eat up more and more of the computer's memory
    • 1:28:12if the trash is just endlessly falling and I can't grab it.
    • 1:28:15It might be a little traumatic if you tried to get it
    • 1:28:17and you can't pull it back out and you can't fix the program.
    • 1:28:19So I just wanted the thing to stop.
    • 1:28:21So how might I have implemented this?
    • 1:28:22Let's look at the code at left.
    • 1:28:24Here I have a bit of randomness, like I proposed earlier exists.
    • 1:28:29There's this blue function called Go To x,
    • 1:28:31y that lets me move a sprite to any position,
    • 1:28:35up, down, left, right, I picked a random x location, either here or over here,
    • 1:28:40negative 240 to positive 240, and then a y value of 180, which is the top.
    • 1:28:45This just makes the game more interesting.
    • 1:28:46It's kind of lame pretty quickly if the trash always falls from the same spot.
    • 1:28:51Here's this a little bit of randomness, like most any game would have,
    • 1:28:54that spices things up.
    • 1:28:55So now if I click the green flag, you'll see that it just falls,
    • 1:28:59nothing interesting is going to happen, but it
    • 1:29:01does stop when it touches the black line because notice what we did here.
    • 1:29:05I'm forever asking the question if the distance of the sprite, the trash,
    • 1:29:10is to the floor is greater than zero, that's fine.
    • 1:29:14Change the y location by negative 3.
    • 1:29:17So move it down 3 pixels, down 3 pixels, until the distance to the floor
    • 1:29:21is not greater than zero, it is zero or even negative, at which point
    • 1:29:25it should just stop moving altogether.
    • 1:29:27There's other ways we could have implemented this,
    • 1:29:29but this felt like a nice, clean way that logically, just
    • 1:29:31made it make sense.
    • 1:29:32OK, now I got some trash falling, I got a trash can that opens and closes,
    • 1:29:36I have a lamp post, now I'm a good three steps into the program.
    • 1:29:40We're making progress.
    • 1:29:42If we consider one or two final pieces, something
    • 1:29:45like the dragging of the trash, let me go ahead and open up this version 2.
    • 1:29:50Dragging the trash requires a different type of question.
    • 1:29:53Let me zoom in here.
    • 1:29:55Here's the piece of trash.
    • 1:29:56I only need one sprite, no floor here because I just
    • 1:29:59want the human to move it up, down, left, right and the human's
    • 1:30:02not going to physically be able to move it outside of the world.
    • 1:30:05If we zoom in on this code, the way we've solved this is as follows.
    • 1:30:09We're using that And conjunction that we glimpsed earlier because when
    • 1:30:13the green flag is clicked, we're forever asking this question or really
    • 1:30:17these questions, plural, if the mouse is down
    • 1:30:20and the trash is touching the mouse pointer, that's equivalent
    • 1:30:25logically to clicking on the trash.
    • 1:30:28Go ahead and move the trash to the mouse pointer.
    • 1:30:31So again it takes this very familiar idea
    • 1:30:33that you and I take for granted every day on Macs and PCs of clicking
    • 1:30:36and dragging and dropping.
    • 1:30:37How is that implemented?
    • 1:30:39Well Mac OS or Windows are probably asking a question.
    • 1:30:43For every icon, is the mouse down and is the icon touching the mouse?
    • 1:30:48If so, go to the location of the mouse forever
    • 1:30:52while the mouse button is clicked down.
    • 1:30:54So how does this work in reality now?
    • 1:30:56Let me go ahead and click on the Play.
    • 1:30:58Nothing happens at first, but if I click on it, I can move it up,
    • 1:31:02down, left, right.
    • 1:31:04It doesn't move thereafter.
    • 1:31:05So I now need to kind of combine this idea of dragging with falling,
    • 1:31:09but I bet I could just start to use just one single program.
    • 1:31:12Right now I'm using separate ones to show different ideas,
    • 1:31:14but now that's another bite out of the problem.
    • 1:31:17If we do one last one, something like the scorekeeping
    • 1:31:20is interesting, because recall that every time we dragged a piece of trash
    • 1:31:23into the can, Oscar popped out and told us the current score.
    • 1:31:27So let me go ahead and find this one, Oscar variables,
    • 1:31:31and let me zoom in on this one.
    • 1:31:33This one is longer because we combined all of these elements.
    • 1:31:37So this is the kind of thing that if you looked at first glance, like,
    • 1:31:40I have no idea how I would have implemented this
    • 1:31:42from nothing, from scratch literally.
    • 1:31:44But again, if you take your vision and componenitize it
    • 1:31:49into these smaller, bite-sized problems, you
    • 1:31:51could take these baby steps, so to speak, and then
    • 1:31:53solve everything collectively.
    • 1:31:55So what's new here is this bottom one.
    • 1:31:58Forever do the following: if the trash is touching
    • 1:32:03Oscar, the other sprite that we've now added to the program,
    • 1:32:06change the score by 1.
    • 1:32:08This is an orange and indeed if we poke around
    • 1:32:10we'll see that orange is a variable, like an x or y but with a better name,
    • 1:32:15changing it means to add 1 or if it's negative subtract 1.
    • 1:32:19Then go ahead and have the trash go to pick random.
    • 1:32:24What is this all about?
    • 1:32:25Well, let me show you what it's doing and then we can infer backwards.
    • 1:32:29Let me go ahead and hit Play.
    • 1:32:31All right, it's falling, I'm clicking and dragging it, I'm moving it over,
    • 1:32:34and I'm letting go.
    • 1:32:35All right, let me do it once more.
    • 1:32:36Letting go, let me stop.
    • 1:32:39Why do I have this function at the end called Go To x and y randomly?
    • 1:32:46Like what problem is this solving here?
    • 1:32:48Yeah, in way back.
    • 1:32:50AUDIENCE: Just the same track teleported to the top
    • 1:32:54after you put it in the trash can.
    • 1:32:56DAVID MALAN: Yeah, exactly.
    • 1:32:57Even though the human perceives this as like a lot of trash falling
    • 1:33:00from the sky, it's actually the same piece
    • 1:33:01of trash, just kind of being magically moved back to the top
    • 1:33:04as though it's a new one.
    • 1:33:06There, too, you have this idea of reusable code.
    • 1:33:09If you were constantly copying and pasting your pieces of trash
    • 1:33:12and creating 20 pieces of trash, 30 pieces of trash, just because you
    • 1:33:15want the game to have that many levels, probably doing something wrong.
    • 1:33:19Reuse the code that you wrote, reuse the sprites that you wrote,
    • 1:33:22and that would give you not just correctness, but also a better design.
    • 1:33:27Well let's take a look at one final set of building blocks
    • 1:33:30that we can compose ultimately into something
    • 1:33:32particularly interactive as follows.
    • 1:33:34Let me go ahead and zoom out here and let
    • 1:33:36me propose that we implement something like some kind of maze-based game.
    • 1:33:41Let me go ahead here.
    • 1:33:42So I want to implement some maze-based game that
    • 1:33:46looks at first glance like this.
    • 1:33:47Let me hit Play.
    • 1:33:48It's not a very fun game yet, but here's a little Harvard
    • 1:33:50shield, a couple of black lines, this time vertical instead of horizontal,
    • 1:33:54but notice you can't quite see my hand here,
    • 1:33:56but I'm using my arrow keys to go down, to go up, to go left, to go right,
    • 1:34:01but if I keep going right, right, right, right, right, right,
    • 1:34:04right it's not going anywhere.
    • 1:34:05And left, left, left, left, left, left, left, left, left, left, left, left,
    • 1:34:08left it eventually stops.
    • 1:34:09So before we look at the code, how might this be working?
    • 1:34:13What kinds of scripts, collections of puzzle pieces,
    • 1:34:17might collectively help us implement this?
    • 1:34:20What do you think?
    • 1:34:21AUDIENCE: [INAUDIBLE]
    • 1:34:29DAVID MALAN: Perfect, yeah.
    • 1:34:30There's probably some question being asked, if touching the black line,
    • 1:34:33and it happens to be a couple of sprites, each of which
    • 1:34:35is just literally a vertical black line we're probably asking a question like,
    • 1:34:39are you touching it?
    • 1:34:40Is the distance to it zero or close to zero?
    • 1:34:43And if so, we just ignore the left or the right arrow at that point.
    • 1:34:48So that works.
    • 1:34:49But otherwise, if we're not touching a wall,
    • 1:34:51what are we probably doing instead forever here?
    • 1:34:55How is the movement working presumably?
    • 1:34:57Yeah and back.
    • 1:34:59Oh are you scratching?
    • 1:35:01OK, sure.
    • 1:35:02Let's go on.
    • 1:35:03AUDIENCE: [INAUDIBLE]
    • 1:35:06DAVID MALAN: Sorry, say a little louder.
    • 1:35:08AUDIENCE: Presumably it's continually looking for you to hit the arrow keys
    • 1:35:11and then moving when you do.
    • 1:35:12DAVID MALAN: Exactly.
    • 1:35:13It's continually, forever listening for the arrow keys up, down, left, right,
    • 1:35:17and if the up arrow is pressed, we're probably
    • 1:35:19changing the y by a positive value.
    • 1:35:22If the down arrow is pressed, we're going down by y,
    • 1:35:25and left and right accordingly.
    • 1:35:26So let's actually take a quick look.
    • 1:35:28If I zoom out here and take a look at the code that implements this,
    • 1:35:31there's a lot going on at first glance, but let's see.
    • 1:35:34First of all, let me drag some stuff out of the way
    • 1:35:36because it's kind of overwhelming at first glance,
    • 1:35:38especially if you, for instance, were poking around online as for problem set
    • 1:35:410 just to get inspiration, most projects out there
    • 1:35:44are going to look overwhelming at first glance
    • 1:35:46until you start to wrap your mind around what's going on.
    • 1:35:49But in this case, we've implemented some abstractions
    • 1:35:52from the get go to explain to ourselves and to anyone else looking
    • 1:35:55at the code what's going on.
    • 1:35:57This is that program with the two black lines and the Harvard shield going up,
    • 1:36:01down, left, and right.
    • 1:36:02It initially puts the shield in the middle, 0,0, then
    • 1:36:06forever listens for keyboard, as I think you were describing,
    • 1:36:09and it feels for the walls, as I think you were describing.
    • 1:36:13Now how is that implemented?
    • 1:36:14Don't know yet.
    • 1:36:15These are custom blocks we created as abstractions to kind of hide
    • 1:36:19those implementation details because honestly that's
    • 1:36:21all I need to know right now.
    • 1:36:22But, as aspiring programmers, if we're curious now,
    • 1:36:25let's scroll down to the actual implementation
    • 1:36:28of listening for keyboard.
    • 1:36:29This is the one on the left and it is a little long,
    • 1:36:32but it's a lot of similar structure.
    • 1:36:35We're doing the following, if the up arrow is pressed, then change y by 1.
    • 1:36:40Go up.
    • 1:36:40If the down arrow is pressed, then change y by negative 1.
    • 1:36:44Go down.
    • 1:36:45Right arrow, left arrow, and that's it.
    • 1:36:48So it just assembles all of those ideas, combines it
    • 1:36:50into one new block just because it's kind of overwhelming,
    • 1:36:53let's just implement it once and tuck it away.
    • 1:36:55And if we scroll now over to the Feel for Walls function,
    • 1:36:59this now is asking the question as hypothesized,
    • 1:37:02if I'm touching the left wall, change my x value by 1, sort of move away from it
    • 1:37:07a little bit.
    • 1:37:08If I'm touching the right wall, then move x by negative 1
    • 1:37:11to move a little bit away from it.
    • 1:37:13So it kind of bounces off the wall.
    • 1:37:14Just in case it slightly went over, we keep the crest within those two walls.
    • 1:37:20All right, then a couple of more pieces here to introduce.
    • 1:37:23What if we want to actually add some kind of adversary or opponent
    • 1:37:27to this game?
    • 1:37:28Well, let me go ahead to maybe this one here where the adversary in this game
    • 1:37:34might, for instance, be designed to be bouncing to stand in your way.
    • 1:37:38This is like a maze and you're trying to get the Harvard shield from the bottom
    • 1:37:41to the top or vice versa.
    • 1:37:42Uh oh, Yale is in the way and it seems to be automatically bouncing
    • 1:37:47back and forth here.
    • 1:37:48Well, let me ask someone else.
    • 1:37:49Hypothesize.
    • 1:37:50How is this working?
    • 1:37:51This is an idea you have, this as an idea you see.
    • 1:37:54Let's reverse engineer in your head how it works.
    • 1:37:59How might this be working?
    • 1:38:00Yeah, in back.
    • 1:38:01AUDIENCE: If the Yale symbol is touching a right wall or left wall,
    • 1:38:05then have it bounce.
    • 1:38:07DAVID MALAN: Yeah, so if the Yale symbol is
    • 1:38:09touching the left wall or the right wall, we somehow have it bounce.
    • 1:38:11And indeed we'll see there's a puzzle piece that can do exactly
    • 1:38:14that technically off the edge, as we'll see,
    • 1:38:16but there's another way we can do this.
    • 1:38:18Let's look at the code.
    • 1:38:19The way we ourselves can implement exactly
    • 1:38:21that idea bounce is just with a little bit of logic.
    • 1:38:24So here's what this version of the program is doing.
    • 1:38:27It's moving Yale by default to 0,0 just to arbitrarily put it somewhere,
    • 1:38:31pointing it direction 90 degrees, which means just horizontally, essentially,
    • 1:38:35and then it's forever doing this: if touching the left wall
    • 1:38:39or touching the right wall, here's our translation of bounce.
    • 1:38:43We're just turning 180 degrees.
    • 1:38:44And the nice thing about that is we don't
    • 1:38:46have to worry if we're going from right to left or left to right.
    • 1:38:49180 degrees is going to work on both of the walls.
    • 1:38:52And that's it.
    • 1:38:54After we do that, we just move one step, one pixel, at a time
    • 1:38:57but we're doing it forever so something is happening continually
    • 1:39:00and the Yale icon is bouncing back and forth.
    • 1:39:03Well one final piece here, what if now we
    • 1:39:06want another adversary, a more advanced adversary down the road for instance,
    • 1:39:12to go and follow us wherever we are such that this time
    • 1:39:17we want the other sprite to not just bounce back and forth,
    • 1:39:23but literally follow us no matter where we go.
    • 1:39:28How might this be implemented on the screen?
    • 1:39:31I bet it's another forever block, but what's inside?
    • 1:39:34AUDIENCE: So forever get the location of the of the Harvard shield
    • 1:39:38and move one step towards it.
    • 1:39:39DAVID MALAN: Yeah, forever point at the location of the Harvard shield
    • 1:39:42and go one step toward it.
    • 1:39:43This is just going to go on forever if I just give up, at least in this version.
    • 1:39:48Notice it's sort of twitching back and forth because it goes one
    • 1:39:51pixel then one pixel then one pixel.
    • 1:39:52It's sort of in a frantic state here.
    • 1:39:54We haven't finished the game yet, but if we see inside, we'll see exactly that.
    • 1:39:58It didn't take much to implement this simple idea.
    • 1:40:00Go to a random position just to make it kind of fair,
    • 1:40:03initially, then forever point towards Harvard,
    • 1:40:06which is what we called the Harvard crest sprite, move one step.
    • 1:40:09Suppose we now wanted to make a more advanced level.
    • 1:40:12What's a minor change I could logically make to this code just
    • 1:40:15to make MIT even better at this?
    • 1:40:17AUDIENCE: Change the number of steps to two.
    • 1:40:19DAVID MALAN: All right, change the number of steps to two.
    • 1:40:21So let's try that.
    • 1:40:22So now they got twice as fast.
    • 1:40:24Let me go ahead and just get this out of the way.
    • 1:40:26Oops, let me make it a fair fight.
    • 1:40:29Green flag.
    • 1:40:31All right, I unfortunately am still moving one pixel at a time,
    • 1:40:34so this isn't going to end well.
    • 1:40:35It caught up to me.
    • 1:40:36And if we're really aggressive and do something like 20 steps at a time,
    • 1:40:42click the green flag.
    • 1:40:44Jesus, OK, so that's how you might then make your levels progressively
    • 1:40:49harder and harder.
    • 1:40:50So it's not an accident that we chose these particular examples
    • 1:40:53here involving these particular schools because we have one more
    • 1:40:56demonstration we thought we'd introduce today
    • 1:40:58if we could get one other volunteer to come up and play
    • 1:41:02what was called by one of your predecessors Ivy's Hardest Game.
    • 1:41:07Let's see, you in the middle.
    • 1:41:08Do you want to come on up?
    • 1:41:09What's your name?
    • 1:41:10AUDIENCE: Celeste.
    • 1:41:11DAVID MALAN: Say again?
    • 1:41:12AUDIENCE: Celeste.
    • 1:41:12DAVID MALAN: Come a little closer, actually.
    • 1:41:14Sorry, hard to hear here.
    • 1:41:17All right, round of applause here if we could, too.
    • 1:41:19[APPLAUSE]
    • 1:41:25OK, sorry, what was your name?
    • 1:41:27AUDIENCE: Celeste.
    • 1:41:27DAVID MALAN: Ceweste
    • 1:41:28AUDIENCE: Celeste.
    • 1:41:28DAVID MALAN: Celeste.
    • 1:41:29AUDIENCE: Yes.
    • 1:41:29DAVID MALAN: Come on over.
    • 1:41:30Nice to meet you, too.
    • 1:41:31So here we have on this other screen Ivy's Hardest Game
    • 1:41:35written by a former CS50 student.
    • 1:41:37I think you'll see that it combines these same principles.
    • 1:41:40The maze is clearly a little more advanced.
    • 1:41:42The goal at hand is to initially move the Harvard crest to the sprite all
    • 1:41:47the way on the right so that you catch up to him in this case,
    • 1:41:49but you'll see that there's different levels
    • 1:41:51and different levels of sophistication.
    • 1:41:54So if you're up for it, you can use just these arrow keys up, down, left, right.
    • 1:41:57You'll be controlling the Harvard sprite and if we
    • 1:42:00could raise the volume just a little bit, we'll make this our final example.
    • 1:42:04Here we go, clicking the green flag.
    • 1:42:07[MUSIC PLAYING]
    • 1:42:10Feeling ready?
    • 1:42:12AUDIENCE: Yep.
    • 1:42:12DAVID MALAN: Spacebar.
    • 1:42:14[MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"]
    • 1:42:15MC HAMMER: (SINGING) Can't touch this.
    • 1:42:17You can't touch this.
    • 1:42:21You can't touch this.
    • 1:42:25Can't touch this.
    • 1:42:27My, my, my, my music--
    • 1:42:29DAVID MALAN: Excellent.
    • 1:42:30MC HAMMER: (SINGING) so hard.
    • 1:42:31Makes me want to say, oh my Lord.
    • 1:42:33Thank you for blessing me--
    • 1:42:34DAVID MALAN: Two Yales now.
    • 1:42:36MC HAMMER: (SINGING) Feels good when you know you're down.
    • 1:42:38A super dope homeboy--
    • 1:42:40AUDIENCE: Oh!
    • 1:42:40DAVID MALAN: Oh!
    • 1:42:41Keep going.
    • 1:42:43MC HAMMER: (SINGING) You can't touch this.
    • 1:42:45I told you, homeboy.
    • 1:42:46Can't touch this.
    • 1:42:48Yeah, that's how living--
    • 1:42:49DAVID MALAN: All right.
    • 1:42:50MC HAMMER: (SINGING) Can't touch this.
    • 1:42:52Look at my eyes, man.
    • 1:42:53You can't touch this.
    • 1:42:56You let me bust the funky lyrics.
    • 1:42:57You can't touch this.
    • 1:42:58Fresh new kicks and pants.
    • 1:43:00You got it like that and you know you want to dance.
    • 1:43:02So move out of your seat and get a fly girl and catch this beat.
    • 1:43:06[LAUGHING]
    • 1:43:06Hold on.
    • 1:43:07Pump a little bit and let them know what's going on like that, like that.
    • 1:43:10Cold on a mission, so fall on back.
    • 1:43:12Let them know that you're too--
    • 1:43:13DAVID MALAN: There you go.
    • 1:43:14There you go.
    • 1:43:15[APPLAUSE]
    • 1:43:19MC HAMMER: (SINGING) Can't touch this.
    • 1:43:21Why you standing there, man?
    • 1:43:22You can't touch this.
    • 1:43:24Yo, sound the bell.
    • 1:43:25School's in, sucker.
    • 1:43:26Can't touch this.
    • 1:43:27Give me a song or rhythm, making them sweat that's what give them.
    • 1:43:31[CHEERING]
    • 1:43:32They know.
    • 1:43:32You talking the Hammer when you're talking about a show.
    • 1:43:35That's hyped and tight.
    • 1:43:36Singers are sweating so them a wipe or a tame to learn.
    • 1:43:39DAVID MALAN: Second to last level.
    • 1:43:40Oh!
    • 1:43:41MC HAMMER: (SINGING) That chart's legit.
    • 1:43:43Either work hard or you might as well quit.
    • 1:43:45That word because you know--
    • 1:43:47DAVID MALAN: Oh!
    • 1:43:48Keep going, keep going!
    • 1:43:50Yes!
    • 1:43:50MC HAMMER: (SINGING) You can't touch this.
    • 1:43:53DAVID MALAN: You're almost there.
    • 1:43:55MC HAMMER: (SINGING) Break it down.
    • 1:43:56DAVID MALAN: There you go.
    • 1:43:57Go, go, go!
    • 1:43:59Oh.
    • 1:44:00One more.
    • 1:44:03Yes!
    • 1:44:03[CHEERING]
    • 1:44:04There you go.
    • 1:44:08MC HAMMER: (SINGING) Stop, Hammer time.
    • 1:44:10"Go with the flow," it is said.
    • 1:44:12If you can't groove to this, then you're probably dead.
    • 1:44:14So wave your hands in the air, bust a few moves,
    • 1:44:16run your fingers through your hair.
    • 1:44:17This is it.
    • 1:44:18For a winner.
    • 1:44:19Dance to this and you're going to get thinner.
    • 1:44:21Now move, slide your rump.
    • 1:44:23Just for a minute let's all do the bump.
    • 1:44:26[CHEERING]
    • 1:44:26DAVID MALAN: Yes!
    • 1:44:27[APPLAUSE]
    • 1:44:29Congratulations.
    • 1:44:36All right, that's it for CS50.
    • 1:44:38Welcome to the class.
    • 1:44:38We'll see you next time.
    • 1:44:41[MUSIC PLAYING]
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video