tactiq-free-transcript-2WtPyqwTLKM
tactiq-free-transcript-2WtPyqwTLKM
00:17:33.469 Oh,
00:17:34.729 all right.
00:17:36.739 This is CS 50.
00:17:38.839 Harvard University's introduction to the
00:17:40.770 intellectual enterprises of computer science
00:17:43.089 and the arts of programming.
00:17:44.569 My name is David Malan and actually took this class myself some years
ago.
00:17:48.290 But I almost didn't.
00:17:49.489 It was my first year in college and my roommates
00:17:52.030 and I were living in Matthews Hall for those familiar,
00:17:55.329 nice Matthews.
00:17:56.959 Um, our claim to fame actually at the time was that our room was
literally mat
00:18:01.300 just three years prior. So,
00:18:03.319 uh, onward from that, but my first year I didn't really have the, the
nerves to, like,
00:18:07.939 set foot in this classroom, let alone computer science.
00:18:10.780 In fact, for me, computer science and CS 50 in particular was very
much this class to,
00:18:14.989 to beware.
00:18:16.079 Like I was kind of comfortable with computers,
00:18:18.459 but I certainly wasn't among those more comfortable with computers.
00:18:22.030 And so I shied away my first year instead.
00:18:24.349 I actually took a whole, a lot of classes in government.
00:18:26.689 In fact, in high school I was really enjoying history.
00:18:29.239 I love this constitutional law class that I took my senior year of
high school.
00:18:32.900 And so I figured, ok, if that's what I liked in high school.
00:18:35.199 And if that's where my comfort zone was,
00:18:37.099 that's probably what I should be doing in college.
00:18:39.609 And so I essentially declared as my concentration
00:18:41.619 or major government for the first year,
00:18:43.579 year and a half of school.
00:18:44.979 But my sophomore year when I was living actually in Mather House
instead.
00:18:50.170 Ok. No one from Mather
00:18:51.839 in Mather House instead.
00:18:53.520 Um, it was, I, I sort of followed some friends,
00:18:56.530 I think the first week of class in September of that year to a class
called CS 50.
00:19:01.000 And
00:19:01.449 honestly, like, once I stepped foot in that classroom,
00:19:04.079 the professor at the time was a famous
00:19:05.589 computer scientist by the name of Brian Kernahan.
00:19:08.040 Now at Princeton,
00:19:09.199 like I was hooked and literally would I go
00:19:11.479 back thereafter on Friday evenings when at the time problem
00:19:14.800 sets were released and like sit down at like 7:08
00:19:17.189 p.m. on Friday nights and like dive into homework,
00:19:19.751 which isn't necessarily something we recommend per se.
00:19:22.352 But for me, it was like this sign
00:19:24.271 that like, wow, I've sort of found my calling.
00:19:27.192 I found my classmates here on campus and that's not gonna be the case
for everyone.
00:19:31.352 Certainly, we have no expectations that after taking one computer
science class,
00:19:34.891 that you will or might want to take others.
00:19:37.011 But what's so empowering about computer science and CS 50
00:19:40.202 in particular is it's so applicable to the broader world,
00:19:43.911 whether you're in the arts,
00:19:45.062 humanity,
00:19:45.673 social sciences natural sciences or beyond.
00:19:48.083 It's just so applicable the concept and the practical programming
00:19:52.093 skills with which you will exit a class like this.
00:19:55.333 Now, it's going to be challenging along the way.
00:19:57.583 And indeed I put in my time back in the day and even I did find it
challenging.
00:20:01.463 And here, for instance, is a photograph of a very famous MIT hack,
00:20:05.024 so to speak from down the road,
00:20:06.404 whereby it communicates that getting an education from MIT
00:20:09.473 is like trying to drink from a fire hose,
00:20:12.284 which is to say there's going to be
00:20:13.536 so much information,
00:20:14.755 like so much new stuff that certainly on any given day of the week,
00:20:17.855 you're not gonna be able to absorb
00:20:19.865 all of it that first time around.
00:20:22.046 But at the end of the day, it's through that, that challenge,
00:20:24.845 putting the time in that the returns are therefore just
00:20:27.816 so much higher at the end of the course.
00:20:30.276 And indeed, when you walk out of the course with a much better
understanding,
00:20:33.326 not only of computer science and programming,
00:20:35.286 but ultimately how to teach yourself new technologies
00:20:39.276 and beyond for the next three plus months.
00:20:41.177 Well, we have teaching fellows, teaching assistants,
00:20:43.137 course assistants and myself by your side,
00:20:45.088 sort of guiding you through the courses material.
00:20:47.287 But the goal by terms end is to take
00:20:49.807 those and leave those training wheels off so that you're
00:20:52.708 well equipped to sort of go teach yourself new and
00:20:56.197 be ideas beyond the class itself take comfort though,
00:20:59.697 in knowing that most CS 50 students have never taken a CS course
before.
00:21:03.608 And indeed, as per the syllabus,
00:21:04.797 what ultimately matters in this course is not so
00:21:07.137 much where you end up relative to your class,
00:21:09.160 but where you end up relative to yourself when you began,
00:21:12.489 and when you began is thus today.
00:21:14.780 And so consider just how comfortable or uncomfortable you are with
computing,
00:21:18.619 let alone computer science and programming,
00:21:20.520 particularly if you've never explored either
00:21:22.890 in a classroom before and consider the Delta just a few months from
now,
00:21:27.099 that will really describe how far you have come.
00:21:29.599 And that is all that matters, not how much the student to the left, to
the right
00:21:33.589 in front or behind you right now knows
00:21:36.682 with that said, let me add some additional inspiration if I may.
00:21:40.472 Um Here's a photograph of my own very first homework assignment in CS
50 from 1996.
00:21:46.312 Um And I will draw your attention to the fact that even though this
00:21:49.121 is a so called Hello World program that will play with ourselves next
week,
00:21:53.352 it is
00:21:54.182 pretty much literally the shortest easiest program
00:21:57.621 you can write in a programming language
00:21:59.391 called C I still somehow got minus two on my very first homework as
00:22:04.663 which is to say, like we're all going to make mistakes along the way.
00:22:07.343 But the goal will be to learn and enjoy
00:22:10.703 that process here on out the end of the day too.
00:22:13.984 Like me, you'll exit with your own proudly held high to
00:22:17.534 CS 50 t-shirt. As is our tradition too.
00:22:20.264 With that said,
00:22:21.264 there are so many other traditions within CS 50
00:22:23.754 both on campus and off and in particular,
00:22:26.504 do we try in CS 50 to provide not only the
00:22:29.264 academic support structure that you might want going through the
class,
00:22:32.186 but also a collective shared community experience,
00:22:35.326 which is to say in just a few days,
00:22:36.755 we'll start off the term formally with CS 50 Puzzle Day,
00:22:39.495 which is not only an opportunity to get together with friends
00:22:42.095 with pizza and prizes and also logic puzzles of sorts,
00:22:45.686 but really to send the message.
00:22:46.916 The computer science itself is not about programming,
00:22:49.196 it's not about c it's not about Python, it's not about programming
languages per se,
00:22:53.255 but about problem solving ever more so collaboratively with other
00:22:56.676 smart people by your site in this class or beyond.
00:23:00.057 And indeed are there at toward the end of the semester.
00:23:02.917 Reinforcements of the same by way of a little
00:23:05.118 something that we call the CS 50 hackathon,
00:23:07.177 which will be an opportunity overnight
00:23:09.027 to dive into your own final projects.
00:23:11.128 The capstone of this course thereafter followed by the CS 50 fair,
00:23:14.787 which will be a campus wide exhibition for students,
00:23:16.947 faculty and staff across campus of your very own final projects.
00:23:20.848 Be it your very own web app or mobile app or anything else? You decide
to create
00:23:25.417 by term end. And indeed, the
00:23:27.699 a sort of objective at the end of the day, truly,
00:23:30.800 with that final project in particular is going to be to create for
yourselves,
00:23:34.900 for your classmates, for attendees,
00:23:36.640 something that we didn't even teach you how to do.
00:23:39.790 And indeed, that will signal ultimately that you're indeed on your way
00:23:43.349 and ready
00:23:44.699 toward that end thought,
00:23:45.890 we would give you a sense of CS fifty's past by way of this short
video,
00:23:49.819 if we could dim the lights,
00:23:51.060 that paints a picture of all that awaits here and beyond.
00:24:53.920 All right. So welcome aboard to CS 50 in computer science itself.
00:24:58.229 So what is computer science?
00:24:59.630 Well,
00:24:59.770 let's put simply it's the study of information like how
00:25:02.640 do you represent it and how do you process it.
00:25:05.069 But more fundamentally what we'll teach in this class is computational
thinking.
00:25:08.859 That is to say the application of
00:25:10.750 ideas from computer science to problems of interest
00:25:13.469 to us within the class and problems of interest to you after the
class.
00:25:17.189 And so at the end of the day,
00:25:18.437 what computer science really is is about problem
00:25:21.546 solving ergo that sort of global applicability.
00:25:24.286 And by problem solving, we mean something quite simple.
00:25:27.207 In fact, we can distill it as follows with this mental image,
00:25:29.916 this is problem solving, you've got some problem to solve,
00:25:32.866 thus known as the input that you want to solve.
00:25:35.286 And the goal of course, to problem solving is to actually produce a
solution.
00:25:38.937 So the output in this model would be the solution.
00:25:41.557 The interesting part
00:25:43.083 it's going to be and how you process that input and turn it into that
output
00:25:47.754 ergo
00:25:48.354 solving problems.
00:25:49.673 But before we can do that,
00:25:50.884 we all kind of have to agree how to represent these inputs and
outputs,
00:25:54.764 especially if we want to do it in
00:25:55.884 a standardized global way using literally computers,
00:25:59.053 be them laptops, desktops,
00:26:00.404 phones or most any other kind of electronic device nowadays.
00:26:03.813 So how can we do that? Well, there's different ways to represent in,
00:26:07.411 in the world, for instance, if I were to take attendance, uh old
school style,
00:26:10.850 maybe in a smaller room,
00:26:11.760 I might do 1234567 and so forth and just count people
00:26:16.730 on my human hands that's actually known as Unary notation,
00:26:20.331 otherwise mathematically known as base one because you're using
00:26:23.750 your fingers literally in this model as digits.
00:26:26.750 But a little quick question, how high can you count with one human
hand
00:26:32.020 five is incorrect if you use a different base system than one.
00:26:37.829 So it's obviously correct if you're just using
00:26:39.800 Unary and just counting 12345.
00:26:42.670 But I dare say I can come up with more patterns in my human hand alone
that
00:26:48.010 would enable me without a second hand or a couple of feet to count
higher than five.
00:26:52.739 In fact,
00:26:53.369 maybe for those more comfortable how high could
00:26:55.459 you actually count on a single human hand?
00:26:58.040 Perhaps
00:26:59.489 so? 31 believe it or not is in fact the correct answer. But why?
00:27:04.040 Well, here I initially started pretty naively, 12345.
00:27:08.300 And I just kind of combined all of my fingers and counted the
collective total.
00:27:11.920 But what if I'm a little more clever and take
00:27:13.719 into account the pattern of fingers that go up?
00:27:15.800 So maybe this is still zero.
00:27:17.339 This is one.
00:27:18.750 But now maybe we just agree universally that this is two.
00:27:23.060 Even though it's just my single pointer finger,
00:27:25.239 maybe we all just agree that this is three with two fingers up.
00:27:29.260 Maybe we agree that this is often offensive with just one middle
finger up.
00:27:33.140 But this would then be four, this could then be five, this could then
be six,
00:27:38.219 this could be seven.
00:27:39.609 And if I keep permuting my fingers in this way, allow me to spoil it.
00:27:42.959 This would be in fact 31.
00:27:45.939 But again, why?
00:27:47.219 But the difference here is that we're no longer using
00:27:49.400 Unary or base one as a mathematician would say,
00:27:52.119 but rather base two.
00:27:53.650 Because if we take into account, not just the total number of fingers
that I'm using,
00:27:57.839 but whether each finger is down or up being therefore
00:28:02.109 in two potential states down up A B black.
00:28:06.040 However, you want to distinguish those two states of the world,
00:28:09.339 you're now operating what's called base two and perhaps more
00:28:11.880 familiarly even if you're not a computer person per se,
00:28:14.880 this is the so called binary system.
00:28:17.189 And odds are, even if you're not a computer science person at all,
00:28:20.380 you probably generally know that computers only understand or speak
what,
00:28:24.089 what alphabet, so to speak.
00:28:26.790 So zero ones and zeros, zeros and ones otherwise known as the binary
system.
00:28:31.369 And in fact,
00:28:31.849 there's a term of art here that's worth noting when you're using
00:28:34.369 zeros and ones which of course are a total of two digits,
00:28:37.890 you have binary digits, so to speak by implying two,
00:28:41.180 which means there's two possibilities,
00:28:42.780 zero or one if we actually get rid of some
00:28:44.849 of these letters and then join these two phrases.
00:28:47.229 Here, you have a technical term that is a bit, a bit is just a binary
digit which is
00:28:52.609 say it's a zero or one.
00:28:54.479 And this is in contrast, of course, with the desk system,
00:28:57.349 you and I know as the decimal system deck implying 10 because in the
real world,
00:29:02.119 you and I daily use zero through nine, which is 10 possibilities.
00:29:05.770 Computers only use zero and one
00:29:07.910 that is to say two bits to represent information instead.
00:29:11.489 So how do we represent this information?
00:29:13.369 Especially when at the end of the day,
00:29:14.719 what we're using are indeed computers and electronic devices.
00:29:18.430 Well, if I want to represent zero,
00:29:20.880 I can actually think of this as kind of analogous to the physical
world.
00:29:24.410 Maybe I have a,
00:29:25.130 a light bulb that's off or on a controlled by a switch that turns it
off or on.
00:29:30.099 So you can think of a binary digit that is a
00:29:33.430 zero as really just being a light bulb that is off.
00:29:36.420 By contrast, if you think of a one in the digital world, as of course,
00:29:41.579 being the second of two possibilities,
00:29:43.270 you can think of that in human or analog world,
00:29:46.109 the physical world as being a light bulb that is on.
00:29:49.329 And in fact, what's inside of your Mac, your PC, your Android phone,
00:29:52.520 your iphone are millions of tiny little light switches known as
00:29:56.400 transistors that just can be turned on or off on or off.
00:30:00.410 And essentially you can use those transistors to store
00:30:03.719 information because if you want to store a zero,
00:30:05.680 you turn one of those switches off.
00:30:07.060 If you want to store a one, you turn one of those switches on.
00:30:10.319 Of course, that sort of invites the question.
00:30:12.599 Well, how do we count higher than zero or one? Well, we would seem to
need
00:30:17.520 to use more than just maybe a single bit, a single light bulb.
00:30:21.459 So if we wanted to count higher than for instance, zero or one,
00:30:25.130 why don't we go ahead and maybe do this?
00:30:27.060 So
00:30:27.859 just so I have some place to put these,
00:30:29.339 let me borrow some of our actual physical light bulbs here from the
stage
00:30:34.089 and let me propose that.
00:30:35.939 Now with three bits on the stage, three light switches, three
transistors,
00:30:40.780 whatever metaphor you're most comfortable with.
00:30:42.680 This is how a computer would represent
00:30:45.160 A zero because all of them are off. So it's off, off off.
00:30:49.719 But if a computer wanted to count to one, we could do naively this,
00:30:53.760 we could turn this on.
00:30:54.880 And if the computer wanted to turn uh represent two, we could do
00:30:58.030 this. And if the computer wanted to represent three,
00:31:00.670 we could do this.
00:31:02.729 But I'm kind of painting myself into a corner and not using these
light bulbs as
00:31:07.630 cleverly as I could because at the moment I've only counted as high as
three.
00:31:11.550 So if I want to count to 4 to 5 to 6, I'm gonna need more and more
light bulbs.
00:31:15.510 Can we be a little more clever?
00:31:17.020 Well, again, someone else who's among those more comfortable.
00:31:20.099 What's the spoiler here? How high
00:31:22.380 using binary zeros and ones could I count with three
00:31:26.989 light bulbs total and back?
00:31:29.089 Yeah.
00:31:30.020 So seven, here is the answer.
00:31:32.420 And if that too, you're sort of wondering how are people figuring out?
31 and seven?
00:31:35.949 That's the goal at hand here. So let me do this. Let me turn all of
these off again.
00:31:40.319 So that my three light bulbs are switches
00:31:43.260 again, represent zero.
00:31:44.800 And the first one's easy. This is how a computer would represent the
number one.
00:31:48.729 It would be, uh, it would be uh on off off.
00:31:53.199 How though is a computer gonna represent two.
00:31:55.510 Well, just like my proposed finger example, let's do this, let's turn
this one off.
00:32:00.364 And this one on that is how a computer would represent two by saying
off on off.
00:32:07.194 In other words, 010 would be the way to pronounce it digitally.
00:32:11.015 What if I instead want to represent three?
00:32:12.944 That's how on my finger, I did this with two fingers. Well, I'm gonna
turn this one on,
00:32:17.395 this is three. Now, this will for those less comfortable would be non
obvious.
00:32:21.484 This now is how I would represent the number four.
00:32:26.935 This is how I would represent five.
00:32:29.819 This is how I would represent six.
00:32:33.479 And this as per the spoiler is how it would represent
00:32:36.479 seven. So perhaps very non obvious what it was.
00:32:40.439 I just did or why I chose those patterns.
00:32:42.959 But I dare say if you sort of rewind in your mind's eye or literally
later on video,
00:32:47.069 you'll find that I actually did show you eight distinct patterns of
light bulbs.
00:32:52.380 The first one was off, off, off, the last one was on, on,
00:32:55.660 on and there were another six total in between then, well, wait by
seven.
00:32:59.579 Well, if you start counting at zero
00:33:01.609 and I claim there's eight possibilities,
00:33:03.469 you can only count from 0 to 7 as we will soon see.
00:33:06.819 So how are these patterns coming about?
00:33:09.060 And what is it that our computers are actually doing? Well?
00:33:11.640 It's actually doing something a little like this quite like in
decimal.
00:33:15.819 So in the human world,
00:33:16.939 you and I are very much in the habit of using base 10 0 through nine A
K A decimal.
00:33:21.939 Well, how do we use it instinctively as humans?
00:33:25.579 Well, what's this number obviously on the screen?
00:33:28.780 123?
00:33:30.140 But why is it 100 and 23 like for years?
00:33:32.760 You haven't really thought about why this pattern
00:33:35.079 of symbols or digits on the screen?
00:33:37.640 123 represents mathematically this number that you know, obviously is
123.
00:33:43.479 But if you rewind to grade school, odds are like me,
00:33:46.260 you were taught that the rightmost digit is in the ones column.
00:33:49.319 This second digit is in the tens column. This digit is in the
00:33:52.640 H
00:33:52.900 column and so forth.
00:33:54.160 So even though none of us have to do this math explicitly,
00:33:57.050 what you're instantly doing is 100 times one plus 10 times two plus
one times three,
00:34:02.040 which gives you 100 plus 20 plus 3.
00:34:05.069 0, that's why it is 123 because these digits in this order have that
significance.
00:34:12.969 The digits to the left have more weight, so to speak than the digits
to the right.
00:34:17.239 So what can we take away from this?
00:34:19.090 Well, let's generalize it first as just any three digit number.
00:34:22.110 So number, number, number, the ones column, the tens column, the
hundreds column.
00:34:26.370 But there's some math going on there and it's not particularly
sophisticated.
00:34:29.228 Those are actually powers of 10, so 10 to the 0 10 to the 1, 10 to the
two.
00:34:35.389 And there's your death
00:34:36.688 system because the base in this value is a 10.
00:34:40.260 That's because there's 10 possibilities for each of those
placeholders,
00:34:44.260 zero through nine.
00:34:45.840 But in the binary world,
00:34:47.208 in the world of computers where all they have are zeros and ones.
00:34:50.179 Why?
00:34:50.580 Because all they have physically is transistors, tiny, tiny,
00:34:53.610 tiny light bulbs that can be off or on.
00:34:55.938 If you only have two digits to play with the 10 base should of course
become a two base.
00:35:01.809 And now if we do some math here, two to the 02 to the one and two to
the two,
00:35:06.198 you get the ones column, the twos column, the fours column.
00:35:10.009 And if we keep going 8, 1632 6400 and 28 and so forth,
00:35:14.329 it's powers of two instead of powers of 10.
00:35:17.409 But this is to say computers represent information in exactly
00:35:21.479 the same way you and I have since childhood,
00:35:24.300 but they have fewer digits at their disposal.
00:35:26.959 So these columns need to be weighted differently.
00:35:29.590 So we can still count from zero all the way up toward infinity.
00:35:33.590 So what does this mean? Well, here we have three bits on the screen,
000.
00:35:39.219 If we were to convert this now mentally or on paper pencil to decimal,
how do we do it.
00:35:44.229 Well, four times zero plus two times zero, plus one times zero.
00:35:47.399 That gives us the mathematical number you and I know as zero,
00:35:50.320 that was three light bulbs off, off, off.
00:35:53.860 Well, what if we turn on one light bulb all the way on the right.
00:35:57.570 What decimal number does this binary number? 001 represent
00:36:03.570 just one because it's four times 02 times 01 times one.
00:36:07.199 Here's where things got more interesting.
00:36:08.889 Even if non obvious in light bulb form or even physical hand form
00:36:12.649 010 in binary is what in decimal
00:36:16.429 two? Because it's two times one and that's it.
00:36:18.830 011 in binary is of course now
00:36:21.899 three, this is now four, this is now five.
00:36:25.820 This is now six and seven on, on,
00:36:28.530 on or 111 is the highest we can count with these three bits.
00:36:32.739 All right. So how might a computer intuitively count as high as eight?
00:36:36.959 What do you need to do? Presumably
00:36:39.489 you gonna need to add a bit.
00:36:40.659 So you need another light bulb, another switch, you need more memory.
00:36:43.879 So to speak, to use nomenclature with which you're probably familiar.
00:36:46.800 So, in fact, if we change all of those to zero,
00:36:49.260 but we give ourselves one more bit for a total of four,
00:36:52.199 that's got to be the eights place because it's just another power of
two.
00:36:55.439 So 1000 is the decimal number eight. You don't say 1000
00:37:00.506 in binary. You literally say 1000.
00:37:03.466 But that is the number you and I know as eight and you can keep going
up and up and up and
00:37:07.625 how then computers with Excel or any kind of number crunching
00:37:11.246 software count really high and keep track of really big numbers.
00:37:14.716 The computer just throws more and more transistors at it,
00:37:17.845 more and more bits to count higher
00:37:20.395 and higher
00:37:21.302 and higher than this
00:37:23.241 turns out though one bit, three bits,
00:37:25.332 even four bits aren't that useful in practice
00:37:27.781 because like literally you can count as high as
00:37:30.241 uh seven or maybe 15 or 31.
00:37:32.931 So more commonly as as is commonly known is to use a bite of bits
instead,
00:37:38.991 uh how many bits is in a bite for those familiar?
00:37:41.889 So it's just eight, why eight?
00:37:43.179 It's just more useful than one or two or three or some other number.
00:37:47.239 And as an aside, it happens to be a power of two,
00:37:49.399 which is just useful electronically as well.
00:37:51.879 So a byte then is just eight bits.
00:37:54.100 And here are those columns I rattled off off the top of my head. Here
is how it can be
00:37:58.375 computer would represent zero in decimal, but using eight binary
digits or bits,
00:38:03.875 uh little trivia.
00:38:04.995 And again, this is not what computer science is about,
00:38:06.804 but it helps to sort of know sort of the lower
00:38:09.054 bounds and the upper bounds of these kinds of values.
00:38:11.594 How high can you count with eight bits or one byte
00:38:15.155 if this is zero?
00:38:18.540 Yeah. So it's actually 255.
00:38:21.120 So if I were to change all of these zeros to ones and
00:38:23.729 then do some quick mental or calculator math 128 plus 64 plus 3216,
00:38:29.169 +842 and one would actually give me
00:38:32.080 255 total plus zero, which gives me 256 total possibilities.
00:38:38.729 So this is only to say this is not again, the kind of math we'll
frequently do,
00:38:42.620 but you'll commonly see in the computer world
00:38:44.719 and programming world powers of two numbers like 2
00:38:47.889 55 2 56 Y because these are the common units of measures that systems
tend to use.
00:38:54.879 So let me pause here and see with respect to binary zeros, ones
00:39:00.159 transistors and the like any questions
00:39:03.330 or confusion, we can clear up
00:39:07.169 just
00:39:07.679 on and off,
00:39:09.639 like
00:39:10.080 bolted.
00:39:11.290 A really good question. Why are bits just on or off instead of maybe
00:39:16.389 uh sort of 0% 50% 100% by playing with voltages.
00:39:20.310 So the voltage inference of yours is actually correct.
00:39:23.229 That's what computers typically do. Maybe they use zero
00:39:25.989 ish vaults to represent zero,
00:39:27.929 maybe fiveish volts to represent uh uh to represent one.
00:39:32.370 It turns out it's just really easy to do extremes in computers.
00:39:36.055 If you start to split that range of voltage levels.
00:39:39.115 For those who remember any of their electricity,
00:39:41.025 it just gets harder and harder to be exact.
00:39:43.216 And if you get things a little too murky,
00:39:45.256 you might mistake a zero for a one or a two or a three.
00:39:49.016 So it turns out it's just simpler to use the binary system.
00:39:52.466 But there do exist computers known as turn
00:39:54.852 computers that actually use three values, 01 and two,
00:39:59.382 which is somewhere of course between binary and decimal.
00:40:01.872 But you can do different things.
00:40:02.832 It's just simple on and off and case in point,
00:40:05.021 I don't want to really be dramatic and turn off my computer.
00:40:07.231 But if I pulled out the power plug that could be off literally A K A
zero plug it back in.
00:40:12.481 That's a one. There's just a cleanliness and simplicity
00:40:15.781 to that
00:40:16.862 other questions or confusion
00:40:19.662 that we can clear
00:40:21.172 up.
00:40:24.040 No. OK.
00:40:25.679 So if, if you're in agreement for the moment that, ok, using just
zeros and ones,
00:40:30.959 we can represent any number we want from zero on up,
00:40:34.370 let me propose that we do more useful things with our computers in our
00:40:37.219 pockets and desktops and laps like represent letters for the sake of
Google Docs,
00:40:41.909 Microsoft word or any kind of text, uh text that we might want to
write.
00:40:46.639 So knowing now that computers only contain Zer or only use
00:40:50.100 zeros and ones and therefore only contain hardware like transistors.
00:40:54.439 How could you represent something like a capital?
00:40:57.139 Letter A in English inside of a computer,
00:41:00.500 which of course is not a number anymore.
00:41:03.790 Like, what could we do? Yeah.
00:41:05.870 Use
00:41:06.189 the album
00:41:06.780 and then
00:41:07.879 that would be a different number. So it would be one.
00:41:11.459 Ok. Yeah.
00:41:12.010 So we could take the alphabet A through Z in
00:41:14.030 English and we could just assign each letter a number.
00:41:16.729 And honestly, that is not only the correct answer,
00:41:18.889 it's really the only answer because at the end of the day,
00:41:21.449 if all you have are zeros and ones available to you,
00:41:24.610 that is the entirety of the potential solution to this problem.
00:41:29.635 So it turns out that yes,
00:41:30.895 capital letter A some years ago was decided by a bunch of people in
00:41:34.504 a room shall be represented with this pattern of zeros and ones
01000001.
00:41:42.155 And now trained as you are to do a bit of quick binary math.
00:41:45.245 What decimal number is used to represent apparently capital A
00:41:49.360 so 65 because it's 64 plus one
00:41:51.679 plus one times one is 65. What is B turns out at 66? What is C 67?
00:41:57.459 So they kept things simple there on out might have been
00:42:00.050 nice if a were zero or maybe a were one.
00:42:02.929 But nope, we're stuck with 65 instead.
00:42:05.000 But everything after that is very much predictable. And in fact, for
lowercase s
00:42:09.679 there's a whole other set of numbers uh such as lowercase A happens
00:42:14.810 to be 97 lowercase B happens to be 98 and so forth.
00:42:18.780 But again, this is sort of like CS trivia.
00:42:20.860 But what's important here is that they're indeed contiguous from 65 to
66 to 67 on up.
00:42:26.840 That's something we're going to be able to leverage
00:42:28.909 beyond the letter A alone.
00:42:30.610 What is this system? What is this mapping that you yourself proposed?
It's
00:42:34.149 Asy, the American standard code for information interchange.
00:42:36.760 And indeed, it was a bunch of Americans years ago who came up with
this system.
00:42:40.699 Unfortunately, at the time, they only allocated seven
00:42:44.300 and eventually eight bits total
00:42:47.479 four representing letters both uppercase and lowercase
00:42:51.229 numbers on the keyboard as well, punctuation symbols as well.
00:42:54.520 And so per our conversation a moment ago,
00:42:56.830 if the Americans in this room, so to speak, only used eight bits
total.
00:43:01.929 How many different characters can we represent with a computer in this
story?
00:43:08.870 So only 255 technically 256 if we again start counting from zero.
00:43:14.010 So that's not nearly enough, but it is to represent all human
languages,
00:43:18.260 but it is enough to represent at least English among some others.
00:43:21.820 So here for instance, is a chart of the asking mapping.
00:43:24.820 And sure enough if we zoom in on like this column here,
00:43:27.459 65 is capital A 66 is capital B dot dot 0.72 is H 73 is I and so
forth.
00:43:34.830 So there is a standardized mapping for at least all of these English
letters.
00:43:38.790 Well,
00:43:39.060 suppose you were to receive an email or a text message or
00:43:42.899 like a Google doc containing this pattern of zeros and ones.
00:43:46.810 So 01001000 and so forth
00:43:50.209 and so forth. So three bytes worth three sets of eight bits that is to
say three bytes,
00:43:57.800 each of which represents a single letter in
00:44:00.629 Asy.
00:44:01.719 What
00:44:02.739 message have you received? Well, I'll do the math this time.
00:44:05.280 So we don't have to suppose what you really received was decimal 72 73
33.
00:44:11.949 What message did you just receive
00:44:14.979 if you recall the previous chart?
00:44:17.179 Hi, was in fact correct. Why? Because H is 72 I is 73 and wait a
minute 33.
00:44:22.879 So here's H here's I 33.
00:44:25.030 If we sh uh um highlight, it instead happens to be an exclamation
point.
00:44:29.090 So like that is literally what is going on underneath the hood, so to
speak.
00:44:32.810 When you get a text message today that literally
00:44:34.969 says in all caps and an exclamation point.
00:44:36.899 Hi,
00:44:37.689 your phone has received at least three bytes each
00:44:40.939 of which represents a letter of the alphabet.
00:44:43.080 Your computer is quickly doing the mental math to figure out exactly
00:44:45.959 what numbers those are and then looking up in the so called
00:44:48.889 Asy chart in its memories
00:44:50.679 uh in some sense, what letter should you actually see on the screen
there.
00:44:55.540 And so if you were to then display that message,
00:44:59.639 um you would see it indeed in English as opposed to those numeric
equivalents.
00:45:05.209 How else might we use this then?
00:45:07.959 Well, here again, is that chart and maybe just to vary things,
00:45:10.520 maybe take a little pressure off of me here.
00:45:12.120 Why don't we try spelling something else this time, a different three
letter word,
00:45:16.500 but maybe eight volunteers.
00:45:17.939 Could we get a bite's worth of volunteers and I can sweeten the deal
with some
00:45:21.179 stress balls in exchange.
00:45:22.699 You just have to be comfortable coming up on stage and being on the
internet.
00:45:25.320 So, yes, 12,
00:45:27.310 about 34,
00:45:28.919 about 567.
00:45:32.000 And how about eight? Come on up, a round of applause for our
volunteers. Yep. Come
00:45:36.739 on.
00:45:42.610 All right. So what I'm gonna have each of you do is represent a bit
00:45:46.909 in a particular order.
00:45:48.199 So if you want to just in any order, line yourselves up here, facing
the audience,
00:45:52.370 come on over.
00:45:54.780 All right.
00:45:55.699 And we will have you rep, well, we gotta see who,
00:45:57.620 who ends up where uh screech this way a little bit
00:46:00.159 this way this way.
00:46:01.570 All right. So you shall be the one's place and just hold that in front
of you.
00:46:05.659 Two's place,
00:46:07.540 threes,
00:46:08.379 uh fours place,
00:46:09.939 eighth place,
00:46:11.770 16,
00:46:13.590 3264
00:46:16.419 and 128 and just compress yourselves a little bit if you could.
00:46:20.379 So, each of these folks represents a bit in a particular place.
00:46:24.709 And let's say this, if you're just standing there,
00:46:26.620 sort of uncomfortably without any hand raise,
00:46:28.699 we'll assume that you're representing a zero.
00:46:30.459 Quite simply if your hand goes up though,
00:46:32.790 the audience should assume that you're representing a one.
00:46:35.250 And therefore we'll
00:46:36.274 do spell out a three letter word and on each round of this, you'll
either stay,
00:46:40.215 stay like this or you'll raise your hand.
00:46:42.574 But first let's actually meet some of our volunteers here.
00:46:45.784 Uh, starting with position number one,
00:46:47.715 if you'd like to say your name perhaps where you're from and, or
studying.
00:46:50.875 Hi, my name is Brooke. I'm from Indiana
00:46:54.024 and I'm studying biology and computer science.
00:46:57.794 Nice. Welcome.
00:46:59.135 Hi, I'm Becca. I'm from like Maryland DC area
00:47:03.064 and I'm studying electrical engineering.
00:47:05.709 Welcome,
00:47:09.709 engineering.
00:47:11.399 Hi, I'm Sharon. I'm from Rhonda and I'm studying CS and Math.
00:47:16.120 Hi, I'm Grace. I'm from Alabama and I'm studying electrical
engineering.
00:47:21.739 Hi, I'm Angelina. I'm from Maryland and also I say in Matthews,
00:47:28.790 um, and I'm studying applied math and
00:47:30.750 econ as well as environmental science and public policy.
00:47:34.590 I'm Owen Bells and I'm from rural Virginia and I'm studying CS
00:47:39.389 and, and
00:47:40.219 my name is Max. I'm from London.
00:47:42.379 I am also staying in Matthews
00:47:44.100 and I'm studying computer science
00:47:45.820 and
00:47:46.439 neuroscience.
00:47:47.129 Welcome
00:47:47.449 aboard as well. If you're wondering too why I was wearing these
glasses at the start.
00:47:51.010 So, very common on the internet nowadays is these POV videos.
00:47:53.649 So it turns out these Ray Bands actually record video footage and we
00:47:56.169 have a couple of them and we thought we'd offer them to a
00:47:58.360 couple of volunteers if anyone wants to record their point of view for
00:48:02.054 everyone here and Vlad here is going to help make sure they're
recording
00:48:05.715 second volunteer. Yes. Number two.
00:48:07.715 All right. So Asla
00:48:09.614 gets those set on the backs of your pieces of paper.
00:48:12.685 You have instructions for the following three rounds.
00:48:15.044 Each round represents a letter, the audience participation.
00:48:17.784 Part of this is to actually do the mental math
00:48:19.895 to figure out what number these volunteers are representing.
00:48:23.324 So
00:48:24.364 go ahead and execute round one, either keeping your hand down or
raising it
00:48:29.405 appropriately.
00:48:32.699 OK? What number are our volunteers here representing
00:48:37.290 66? Because we have a 64 plus a two that then maps to what ask the
letter
00:48:42.610 B was the first letter. OK? Hands down round two. Go
00:48:47.850 little harder.
00:48:49.100 What's now being represented?
00:48:54.189 I'm starting to hear it.
00:48:55.709 79 is in fact correct? 79 because we have a 64 and an eight and a four
and a two and a one.
00:49:03.280 So if it's a 79 we have the
00:49:04.820 Al Asy letter
00:49:06.419 O. OK? So we've got bo
00:49:08.729 and then lastly third round go,
00:49:13.590 we have 01010111.
00:49:18.510 What number is this?
00:49:20.439 87? Which spells in the letter
00:49:23.469 W which spells the word
00:49:26.229 not bow,
00:49:26.929 take a bow if you could. All right, a round of applause for our
volunteers here
00:49:32.540 and come on off this way
00:49:34.760 and help yourself to a CS 50 stress fall.
00:49:37.959 So this is to say,
00:49:39.649 uh,
00:49:39.870 if you can handle the lad on the way out,
00:49:41.969 we only need the glasses back.
00:49:43.820 Thank you to our volunteers.
00:49:44.899 So this is only to say we've now agreed on how we can represent
numbers from zero on up.
00:49:48.729 We've agreed on how we can represent letters, but at least letters
using as
00:49:53.129 and in fact, these are more than just decoration.
00:49:55.389 In fact, there's a little bit of uh trivia by lectures.
00:49:58.030 And if you want to come up for your very own CS 50 stress ball
00:50:00.850 turns out there are 64 light bulbs at the foot of the stage here.
00:50:04.209 If you break them down into eight bytes or single, eight bit or single
byte chunks,
00:50:10.669 there's an eight letter word that happened to be spelled out today
using this here as
00:50:14.512 you chart. So today's mystery is what exactly is that their word?
00:50:19.222 But of course, if you have only eight bits,
00:50:22.042 you can only represent like 256 characters, which sounds like plenty
for English.
00:50:26.722 And indeed,
00:50:27.222 it is zero through nine A through B capital and
00:50:30.393 lower C uppercase and lowercase as well as punctuation.
00:50:32.933 But there's so many other human languages in
00:50:35.603 the world that have other characters for instance,
00:50:37.933 we have
00:50:38.673 a, not just the English alphabet we might see on here on a US English
keyboard.
00:50:42.156 We have accented characters.
00:50:43.956 We have uh various Asian languages have even many more glyphs.
00:50:46.916 We need more than 256 possible characters.
00:50:50.565 And so nowadays, computers do not just use seven or even eight bits.
00:50:55.186 They might use eight bits for some letters, like all of the English
letters,
00:50:58.766 they might use 16 bits for certain other languages, maybe even 24 or
32 bits.
00:51:04.895 And fun fact, if you have 32 bits
00:51:07.395 and we have more than that on the stage, if you've got 32 bits,
00:51:10.428 you can actually represent as many as 4 billion possible characters,
00:51:14.708 which is quite a bit,
00:51:16.829 no pun intended. So what else can we represent?
00:51:20.089 Well, the goal of this system, not just Asy,
00:51:23.198 but something known as unicode,
00:51:24.769 which is a newer standard is to be backwards compatible with
00:51:27.979 Asy. So humans left all of those other numbers alone, 65 66 67 and so
forth.
00:51:33.168 But they added to it a superset of representations that maybe are 60
00:51:37.711 24 or 32 bits. The goal being
00:51:40.592 to be able digitally to represent all human languages,
00:51:44.342 past present and future and even through uh pictograms,
00:51:48.941 things like uh smiley faces and the like even
00:51:52.332 people places things in emotions that transcend human language.
00:51:56.342 And in fact, odds are within the past few minutes or hours.
00:51:59.342 Most of you have used one or more of these here emoji,
00:52:02.582 these pictograms which it turns out are just character
00:52:06.334 on a keyboard.
00:52:07.375 You might have to hit a special button to pull up that form of the
keyboard.
00:52:10.625 But these are just here characters.
00:52:12.995 And so these emoji have exploded in popularity for a number of
reasons.
00:52:16.625 One of which is my God.
00:52:17.774 What are we going to do with 4 billion possible patterns of zeros and
ones we can start
00:52:21.975 to kind of have some fun with it and
00:52:23.284 represent things beyond English and human languages alone.
00:52:27.185 Now, as an aside,
00:52:28.594 um when it comes to unicode, it turns out unicode years ago,
00:52:31.764 standardized this pattern of 32
00:52:34.929 zeros and ones to represent just one of those emoji.
00:52:38.709 So emoji tend to use even more bits here.
00:52:41.169 Um Anyone
00:52:42.300 know what decimal number? This is,
00:52:44.629 this is not a fun mathematical exercise.
00:52:46.409 The spoiler is 4 billion, 36 million,
00:52:48.709 991,106 is the decimal number that actually represents as of present,
00:52:55.090 the most popular emoji in the world.
00:52:57.949 Does anyone want to hazard a guess what Emoji this here number
represents?
00:53:05.560 Yeah.
00:53:06.560 Which other emoji come to mind?
00:53:10.489 Hearts, hearts. No, but it's actually this so called face
00:53:14.489 with tears of joy. So perhaps think about the frequency of it. Did you
send that one?
00:53:19.000 And even though it's obviously a picture on the screen?
00:53:21.620 Sure, it actually is more like a font because underneath the hood,
00:53:25.540 it's indeed just a pattern of zeros and ones
00:53:27.379 or a decimal number that the computer is storing.
00:53:29.760 But the computer be it Mac Os or Windows or, or IOS
00:53:33.383 or Android know to display that pattern as this here picture.
00:53:37.512 But the pictures might look different depending on the hardware. Why?
00:53:40.312 Because there's companies like Google and Microsoft and
00:53:42.742 Meta and others that have their own artists on
00:53:45.572 staff as employees and those artists interpret the
00:53:49.232 descriptions like face with tears of joy differently.
00:53:52.153 So those of you with an Android phone actually see face with tears of
00:53:55.865 looking a little something like this.
00:53:57.486 And if you have telegram for instance, installed on your phone,
00:54:00.025 it's even more animated than that.
00:54:01.986 It's this here, Emoji using the same pattern of zeros and ones.
00:54:06.325 So different artists render these here emoji in different ways,
00:54:09.845 but all they are here are patterns now for all of the other answers.
00:54:13.736 Save one that was uh shouted out a moment ago. This is a sort of um
00:54:18.290 uh cloud diagram of the most popular emoji as of a couple of years ago
per unicode,
00:54:23.479 whereby the size of the emoji indicates its relative popularity.
00:54:27.000 So heart I did hear over here is indeed one of the most popular ones
as well. Question.
00:54:38.050 For example,
00:54:40.110 a
00:54:40.870 really good question, why do certain emoji not show up on one device
or another?
00:54:45.409 It depends on how recent the software is pretty much every year.
00:54:48.719 The humans behind the Unicode Consortium release new emoji,
00:54:52.330 which is to say they decide that this other pattern will represent
this new emoji.
00:54:56.280 This other pattern will represent this new emoji.
00:54:58.489 And unless you update your phone, your laptop,
00:55:00.810 your desktop to the very latest software and the manufacturer of that
device or
00:55:04.949 software also update by hiring an artist to draw those pictures in
their own font
00:55:10.110 in their own style.
00:55:11.199 You're gonna see usually just like an empty black square or maybe
00:55:14.189 just like a black and white heart instead of something more colorful,
00:55:17.229 really just placeholders because it's as though you don't have the
right font
00:55:20.379 installed or really you have an older version of that same font
install,
00:55:24.580 but it's become sort of an annual tradition that new and more.
00:55:27.270 Uh emoji are released every year,
00:55:28.949 which is among the reasons why uh these updates contain uh more and
more.
00:55:33.270 Yeah,
00:55:36.919 that is an amazing segue. How do you represent color
00:55:40.570 in bytes? Well, you use R GB, which happens to be by coincidence, the
next slide.
00:55:45.379 So let's again, recap, we know how to represent letters.
00:55:48.260 We know how to represent numbers. We can even represent emoji.
00:55:50.889 But those emoji technically on the screen are of course composed of
00:55:54.000 colors like a whole bunch of yellow for that their smiley face.
00:55:57.100 How do computers then using only zeros and ones represent colors?
00:56:01.590 Well, by convention they typically use a system that by an acronym is
called R GB red,
00:56:06.139 green, blue.
00:56:07.397 And this is to say that a computer to represent a single dot on the
screen,
00:56:11.587 maybe this one, this one,
00:56:13.318 this one will allocate some number of bits or some number
00:56:17.568 of bytes to represent the color of just that their dot
00:56:20.857 Otherwise known as a pixel,
00:56:22.468 you can actually see pixels on your phone or even on your TV or
monitor.
00:56:25.647 If you go really close, especially if it's an older monitor,
00:56:28.508 you can see the tiny little squares,
00:56:30.498 each of those has some number of bits telling the device what
00:56:34.325 color to use.
00:56:35.476 In particular,
00:56:36.565 these devices typically use three numbers in total three bytes.
00:56:40.885 So that is to say 24 bits per pixel. And they do this
00:56:45.395 if you were to represent a single dot on the
00:56:47.275 screen using these three numbers just by intent here,
00:56:50.216 this is 72 73 33 which in the context of a text message, an email,
00:56:54.835 a Google doc represents of course high textually,
00:56:58.756 what if a computer uses that same pattern of
00:57:01.253 zeros and ones that is the same pattern of decimal digits
00:57:04.904 to represent the color on a screen which is
00:57:07.224 German if you're opening an image using Photoshop.
00:57:10.323 So using a different piece of software that knows
00:57:12.364 about colors and images and not just text.
00:57:14.964 Well, this would imply
00:57:16.783 that you want that dot on the screen to have like a medium amount of
red,
00:57:20.364 a medium amount of green and a little bit of blue.
00:57:23.043 Why do I see medium and little?
00:57:24.724 Well, again, if each of these numbers is using eight bits
00:57:28.342 or one byte, the highest we can count as we discovered was 255. So I'm
kind of
00:57:33.582 uh averaging here.
00:57:34.451 So 72 out of 255 feels to me like a medium amount of red,
00:57:38.291 33 feels like relatively little blue.
00:57:40.902 But if now the computer combines those wavelengths of light, so to
speak,
00:57:45.691 a medium amount of red, medium amount of green, a little bit of blue,
00:57:48.931 what you get is the color code for a single dot
00:57:51.372 And does anyone want to guess what color roughly this represents
00:57:55.511 these three bytes,
00:57:58.800 not white, not purple,
00:58:00.669 not, not brown,
00:58:02.459 yellow, in fact is the answer.
00:58:04.330 So it represents in a single pixel roughly this shade here of yellow,
00:58:09.169 which is to say if we look back at any of those emoji,
00:58:12.169 which again are represented by patterns of zeros and ones,
00:58:15.489 but you and I as humans perceive them as images on the screen.
00:58:18.770 Let me actually go ahead and zoom and zoom in further to one such
sample emoji.
00:58:23.340 And when you zoom in far enough or you put the phone close enough to
your face,
00:58:26.570 you can actually see all of those little dots known as pixels,
00:58:30.040 all of the little square.
00:58:31.335 And given that so many of these pixels are yellow.
00:58:33.966 That is to say that that pattern of three bytes,
00:58:36.956 72 73 33 is used to represent this pixel,
00:58:41.615 another three identical bytes are used to represent this pixel, this
one,
00:58:46.186 this one and so forth.
00:58:47.575 So now if you've taken digital photos on your phone or a camera,
00:58:50.355 you're probably generally familiar from the
00:58:51.936 internet and hardware today that a photograph
00:58:54.466 is what like one megabyte 10 megabytes depending on the resolution of
it.
00:58:59.582 Well, megabyte means millions of bytes.
00:59:02.041 Where are all of these bytes inside of these
00:59:04.461 photographs or these images you're taking or downloading,
00:59:07.422 they correspond to every one of the single pixels on the screen.
00:59:12.181 There's at least three bytes being used to represent every one of
those dots
00:59:17.562 as an aside bit of a white lie because nowadays there's
00:59:20.302 fancy compression software that can use fewer than that many bytes.
00:59:24.201 But in general, that's where all of those bytes, those millions of
bytes
00:59:28.300 are coming from.
00:59:29.780 So how was that for an answer to? How do we represent colors? Thank
you.
00:59:33.729 So if we agreed now that there's this way and perhaps others to
represent colors,
00:59:39.939 well, how do we represent not just images but videos?
00:59:43.149 Well, videos, you know, once upon a time or movies were called motion
pictures.
00:59:47.510 So most pictures with motion. Why is that? Well, it
00:59:50.830 analogous to growing up if you ever played with
00:59:53.070 one of these picture books and in fact,
00:59:54.550 there's memes nowadays that have made these popular again, whereby
00:59:57.939 you have a whole bunch of images on individual sheets of paper.
01:00:02.139 And if you flip through them fast enough your
01:00:04.699 human mind and eyes perceive it as actual motion,
01:00:08.219 even though you're just seeing image, image, image, image, image
image,
01:00:11.600 but it's so fast, it looks like motion.
01:00:13.659 That's all a video is on your screen. That's all a film is on your TV.
01:00:18.510 It is not in fact continuous motion, it's maybe 30 frames or images
per second,
01:00:23.909 maybe 24 frames or images per second.
01:00:26.770 Which is to say we know how to represent numbers, we know how to
represent letters,
01:00:30.949 we know how to represent colors and thus images.
01:00:32.929 Now we kind of get videos
01:00:34.625 for free because it's just more of the same use more and more of those
patterns.
01:00:39.054 Why are videos so darn large, why are they gigabytes to download
billions of bytes?
01:00:43.925 Because there's so many darn images, 30 some odd images per second
01:00:48.584 in those kinds of videos and maybe lastly just to top off our
multimedia.
01:00:53.135 How could you represent sound?
01:00:55.735 Maybe musicians in the room.
01:00:57.784 How using only zeros and ones could you represent something as
sonorous as music,
01:01:04.820 something analogous? Digital. Yeah.
01:01:09.010 Yeah.
01:01:09.290 So each number that we store in the computer could correspond
01:01:11.949 to a certain frequency which has a direct relationship to the,
01:01:15.129 the sound or the pitch of a note.
01:01:17.310 For instance, in the world of piano and many other instruments.
01:01:19.760 You've got like your a, your b, your c maybe you have sharps and flats
as well.
01:01:23.489 We could just agree like the
01:01:24.820 Asy people did years ago to represent the musical note.
01:01:27.810 A let's use this pattern, musical note. A
01:01:30.633 sharp, let's use this pattern and so forth.
01:01:32.992 But maybe pitch alone or frequency alone is not enough. Maybe we need
that number,
01:01:37.613 but maybe a second number for the volume.
01:01:39.943 The sort of digital equivalent of how hard are you hitting the key on
the piano?
01:01:43.302 Maybe a third number. For how long are you holding the key down?
01:01:47.542 So maybe the pitch and the volume and the duration kind of like R GB,
01:01:52.805 we could use three bytes to represent every musical note
01:01:56.795 in some piece.
01:01:57.885 And if we wanted to keep track of what instrument
01:02:00.355 should be played by the computer to sound that music,
01:02:04.166 well, maybe that's just 1/4 bite or something else as well,
01:02:07.406 which is to say at the end of the day, all we have are these zeros and
ones
01:02:11.716 to throw at the problem.
01:02:14.229 So for now that's it for representing information.
01:02:17.429 We've got our numbers, we've got our letters, we've got our image,
01:02:20.399 colors and images, our videos and now sound
01:02:23.070 any questions on how computers then are representing as promised
01:02:27.659 those inputs
01:02:28.989 and outputs
01:02:30.459 using just zeros and ones.
01:02:33.100 Yeah, in the middle.
01:02:34.610 Yeah,
01:02:42.639 I
01:02:42.850 mean,
01:02:48.639 correct.
01:02:49.129 So the computer is taking as input at the end of the day,
01:02:51.229 zeros and ones and is output zeros and ones.
01:02:53.669 However,
01:02:54.429 as we'll learn in this class by writing
01:02:56.570 software by writing code that understands those zeros
01:02:59.780 and ones we will enjoy not just literally seeing zeros and ones we
will see ABC,
01:03:04.889 we will see colors, we will see uh
01:03:06.709 video,
01:03:07.209 we will hear sounds so long as we write the code to interpret those
zeros and ones.
01:03:11.969 And indeed, it's worth noting now that that same pattern I keep using
for an example,
01:03:15.219 7270 3, 33.
01:03:16.956 How does a computer know is that the message high? Is that the color
yellow?
01:03:21.575 Is it a.in a video alone.
01:03:24.145 Just depends on the context.
01:03:25.835 Simply put,
01:03:26.676 if you're opening that pattern of zeros and
01:03:28.855 ones with like Excel or a calculator program,
01:03:32.075 odds are the software will interpret those three bytes as numbers.
01:03:35.716 Of course, if though you open that same pattern in a text messaging
program,
01:03:39.466 Google Docs, Microsoft word, they that same pattern will be
interpreted as
01:03:43.756 a sequence of letters.
01:03:45.052 Instead, if you have been photoshopped that same pattern,
01:03:47.241 you'll probably see a single dot That happens to be yellow.
01:03:51.162 Conversely, once you yourself are a programmer or even better
programmer,
01:03:55.691 you will be able to write in code how you
01:03:58.322 want the computer to treat these patterns of zeros and ones
01:04:01.422 you can essentially tell the computer use this to store a
01:04:04.572 number or a letter or a color or something else.
01:04:07.981 That's the power the programmer themselves have at the end of the day,
01:04:13.001 other questions
01:04:14.310 on representing things with bits.
01:04:19.419 No. All right.
01:04:20.850 So lastly, then in this middle of this black box,
01:04:24.409 so to speak is the sort of secret sauce that solves
01:04:27.110 problems that converts those inputs to outputs those problems to
solutions.
01:04:31.060 So what is an algorithm? It's really just step by step instructions
01:04:34.510 for solving some problem. And in fact, um it's, it's the
01:04:40.310 uh
01:04:41.260 an algorithm is step by step instructions for solving some problem.
01:04:44.550 And indeed,
01:04:45.129 I think back to my own first time in CS
01:04:47.020 50 where we learned the same from Professor Brian Kernahan.
01:04:50.040 And as luck would have,
01:04:51.169 it just had my 25th reunion where we pulled some video footage from
1996.
01:04:55.540 And so we're actually fortunate to have the very
01:04:58.239 first few minutes of CS 50/25 years ago,
01:05:02.110 I myself took it. But the lessons back then as today are fundamentally
the same.
01:05:07.080 And what's important indeed is to not only express yourself correctly
01:05:11.929 but precisely as we'll explore today.
01:05:14.979 This then is Professor Brian Carnahan, who years ago, very men
01:05:18.500 memorably introduced us and my classmates to
01:05:21.100 algorithms by actually in class shaving his beard
01:05:25.629 if we could dim the lights here for Brian.
01:05:28.719 The other thing that we're gonna talk about in
01:05:29.899 this class is the notion of an algorithm.
01:05:32.080 The algorithm is a very precise description of how to do something.
01:05:35.270 And the operative word there is precise, it has to be very, very,
very, very
01:05:39.179 precise.
01:05:40.030 And the task that I'm going to do is that I'm going to trim my beard,
01:05:42.969 which has gotten out of
01:05:44.510 whack.
01:05:53.409 And I brought a variety of things which
01:05:55.800 one might use
01:06:00.010 to trim
01:06:02.270 and beards
01:06:04.020 with.
01:06:21.949 All right.
01:06:22.600 So suffice it to say, I don't have much of a beard, but I do have this
here,
01:06:25.659 other technology known uh once upon a time as
01:06:28.459 a phone book and these phone books of course,
01:06:30.100 have lots of information in them happen to be
01:06:32.030 storing numbers and letters in particular for those unfamiliar.
01:06:35.219 Uh They are storing humans names from A to Z here
01:06:38.330 in English and associated with everyone's name is a number.
01:06:40.739 So even if you've never had occasion to physically use this kind of
device,
01:06:43.830 turns out it's pretty much equivalent to the contacts or the address
01:06:47.229 book app on your IOS phone or your Android phone as well.
01:06:49.976 Why?
01:06:50.416 Because if you pull up your contacts, of course, you see some familiar
names here,
01:06:53.545 alphabetized by first name or last name.
01:06:55.885 And if you click on any of those names,
01:06:57.686 you reach the person you're presumably trying to call or text picture
here,
01:07:01.686 then is John Harvard, whose number here is +19494682750,
01:07:06.206 which you're welcome to call or text at your leisure.
01:07:08.535 But here is John Harvard. That's part way through the phone book
digitally. Well,
01:07:13.142 turns out that physically in the phone book,
01:07:15.172 we might use an algorithm step by step instructions for finding John
Harvard
01:07:19.912 in pretty much the same way as I Os Android,
01:07:23.251 Mac Os Windows or other operating systems themselves use.
01:07:26.481 So I could, when looking for John Harvard first name, starting with
Jay,
01:07:30.531 I could start at the beginning of the phone book and start looking
page by page
01:07:34.701 by page for John Harvard. And if he's there, I can call,
01:07:38.879 this is an algorithm. It's indeed step by step. But that was a bug a
few pages turned.
01:07:43.229 But
01:07:44.110 is this algorithm correct
01:07:47.260 step by step? Assuming I'm paying attention?
01:07:50.320 So yes, like if John Harvard is in here,
01:07:52.469 I will eventually find him once I get to the J section.
01:07:55.159 Now this is a little tedious. It's gonna take like a while, a few
dozen, few 100 pages.
01:07:58.870 So maybe I could do things a little smarter from grade school, like
2468, 1012,
01:08:05.669 1416 and so forth going twice as fast.
01:08:08.750 Is that algorithm correct?
01:08:10.810 So no, but why
01:08:13.320 I could miss him, right?
01:08:14.270 I could just get unlucky really with 5050
01:08:16.129 probability because John Harvard could be sandwiched between
01:08:19.529 two pages. Now, this isn't a complete loss. This algorithm.
01:08:23.089 Maybe what I could do is if I get past the J section to like K I
01:08:27.430 could double back at least one page just to make sure I didn't miss
John Harbor.
01:08:32.020 So I can still go twice as fast
01:08:34.078 like an extra step just to make sure I didn't mess up.
01:08:36.948 So the first algorithm might take as many pages as there are in the
phone book.
01:08:40.787 So if this
01:08:41.337 phone book has 1000 pages,
01:08:43.559 in the worst case, if I'm not looking for John Harvard,
01:08:45.599 but someone whose name starts with Z might take me 1000 pages to
actually get there.
01:08:50.328 Second algorithm twice as fast, literally might take me 500 plus one
01:08:54.930 to get there because I'm going to at a time so long as I indeed fix
that bug.
01:08:58.640 But what we used to do back in the day and what your phone is doing
today,
01:09:03.359 albeit digitally is going roughly to the middle
01:09:06.040 of the phone book looking down and realizing,
01:09:08.240 oh, I'm accidentally in the M section.
01:09:10.459 So halfway through the phone book. But what do I now know about John
Harvard?
01:09:14.120 Is he to the left or to the right?
01:09:16.537 So he's obviously to the left because J comes before M. So what I can
do f
01:09:20.499 literally and what your computer does figuratively is tear the problem
in half,
01:09:24.287 throw half of the problem away, leaving us now with the same
fundamental problem,
01:09:29.749 but it's half as big.
01:09:31.037 So I've gone from 1000 pages suddenly to 500 pages and compare this to
the other two,
01:09:37.158 1000 pages, 999 998 versus 1000 pages. 998 996 994. That's still slow.
01:09:45.889 I went from 1000 to 500 in just one step of this algorithm. What do I
do next?
01:09:50.988 I go roughly to the middle here. Oh, I went a little too far. I'm in
the E section now.
01:09:55.229 So it was John Harvard to the left or right now.
01:09:57.939 So he's to the right.
01:09:58.850 So I can again, tear the problem in half,
01:10:00.700 throw the left half away knowing now
01:10:02.939 that John Harvard must alphabetically be in here
01:10:05.569 and I can divide and divide and divide and conquer this problem again
and again,
01:10:10.470 by using that heuristic of going left or going right.
01:10:13.209 And I dare say if I do this correct,
01:10:14.984 I'll eventually be left with one single page on which John Harvard's
number
01:10:19.214 actually is or maybe he's not in the phone book at all.
01:10:23.475 So how many steps maximally might that third and final algorithm take,
it's not 1000
01:10:28.975 it's not even 500 or 501.
01:10:31.625 How many times can you divide
01:10:33.615 1000 pages in half again and again and again,
01:10:37.500 roughly
01:10:40.279 910. So what typically 10 times give or take?
01:10:43.740 There's a bit of rounding there because it's not a perfect power of
two,
01:10:46.330 but roughly 10 times like that is
01:10:48.709 fundamentally better than both of the two algorithms
01:10:51.890 because I go from 1000 pages to 500 to 250 to 125 and so forth,
01:10:57.899 literally having the problem again and again,
01:11:01.009 so we can actually appreciate and see this even more so graphically.
01:11:04.640 And this is among the things we'll do later in the
01:11:06.890 term when we speak to not only writing correct code,
01:11:09.910 but is your code well designed?
01:11:12.270 Is it better than your previous code? Is it better than someone else's
code?
01:11:16.629 Is it better than some other product?
01:11:18.359 If you have given more thought to the algorithms and the quality
thereof,
01:11:21.879 you can perhaps minimize the time required to solve problems but no
less correctly.
01:11:27.100 So if we have a simple
01:11:28.187 Y plot here on the Y axis or vertical is the amount
01:11:31.077 of time to solve in whatever unit of measure seconds pages.
01:11:34.006 However,
01:11:34.416 you want to count on the horizontal or X axis
01:11:37.267 is the size of the problem measured in for instance,
01:11:40.017 numbers of pages.
01:11:41.276 So this would mean zero pages in the phone book.
01:11:43.816 This would mean a lot of pages in the phone book. This would mean no
time to solve.
01:11:47.736 This would mean a lot of time to solve.
01:11:49.497 What's the relationship then among those three algorithms?
01:11:52.256 Well, the first one is essentially a straight line, a slope of one.
01:11:55.804 And if the phone book has N
01:11:57.994 pages in it,
01:11:58.914 we'll describe the slope here as essentially 1/1 for the algorithm
01:12:03.293 with uh the first algorithm turning page by page by page,
01:12:06.994 which is to say
01:12:08.394 if we were to add one more page to the phone book next year,
01:12:11.463 first algorithm is gonna take one more step
01:12:13.874 but the second algorithm is definitely better.
01:12:16.434 It's definitely faster, but it's still a straight line.
01:12:19.003 So it's gonna take roughly in over two steps
01:12:21.534 on average because you only have to go through half
01:12:23.851 of the phone book because you're going two pages
01:12:25.420 at a time instead of the whole phone book.
01:12:27.561 In the worst case, if someone's name is Z
01:12:29.891 to go through every page in total.
01:12:32.011 So if we actually compare these, let me just draw some dash lines,
01:12:34.951 suppose that you have this many pages in the phone book.
01:12:37.931 If you just draw this vertical white line here,
01:12:40.541 it's gonna take this much time in red using the first algorithm,
01:12:43.811 but it's gonna literally take half as much time in yellow for
01:12:47.011 the second algorithm because you're literally going two pages at once.
01:12:49.919 But the third and final algorithm is a fundamentally different shape.
01:12:54.060 It instead looks a little something like this.
01:12:56.640 It looks like it's flatter and flatter and flatter.
01:12:59.720 It's always increasing, it never gets perfectly flat,
01:13:02.959 but it grows so much more slowly as a function of phone book size.
01:13:08.149 And for those who recall their algo their logarithms,
01:13:11.020 this would be described is log base two of N.
01:13:13.665 And in fact, that's where the math came from log base two of 1000 is
roughly
01:13:17.785 10 in total, even if you need a calculator to confirm this much.
01:13:21.544 But this shape is fundamentally different. Why?
01:13:24.645 Well, suppose that Cambridge where we are in Austin,
01:13:26.845 the town across the street next year combined their two phone books
and
01:13:29.935 they go from 1000 pages each to one phone book with 2000 pages.
01:13:34.080 The first algorithms gonna literally take twice as many steps or
pages.
01:13:37.609 Second algorithm is gonna take half as many or uh
01:13:40.609 uh 50% more because you're going two at a time.
01:13:43.299 But the third algorithm is gonna barely miss a beat. Why?
01:13:46.729 Because if this is 1000 pages here and 2000 pages is
01:13:50.500 over there just inferring from the shape of the green line,
01:13:53.569 it's not gonna be much higher on the vertical axis than
01:13:57.279 uh the other two were.
01:13:58.939 So more specifically, if you ho
01:14:01.799 have a 2000 page phone book next year,
01:14:04.279 how many more steps will it take you using that third and final
algorithm?
01:14:08.890 Just one because you'll divide and conquer a 2000
01:14:11.839 page phone book into a 1000 page phone book.
01:14:14.319 And then you're back at the original story
01:14:16.560 and that's the sort of power of learning algorithms.
01:14:19.290 That's the power of learning computer science and learning how
01:14:22.000 to program is to be able to navigate big data,
01:14:24.919 so to speak things, the size of Google things,
01:14:27.379 the size of artificial intelligence training data sets using better
and better,
01:14:31.709 more
01:14:32.296 algorithms that perform faster and therefore not
01:14:34.986 only make the software more competitive but
01:14:36.707 also make it more usable and more favorable for people like you and me
01:14:41.077 when using that software.
01:14:43.077 So
01:14:43.776 when it comes to implementing algorithms as programmers, as computer
scientists,
01:14:48.827 what you're really doing is taking these algorithms which might
01:14:51.696 be expressed in English conceptually as we just did,
01:14:54.356 but really just translating them to code,
01:14:56.693 be it C or C++ or Python or R or Ruby
01:15:00.253 or any number of other languages that exist in the world.
01:15:03.284 But for now,
01:15:04.173 let's consider how we might implement that algorithm
01:15:06.574 using something that's literally still English but pseudo
01:15:09.233 code something that is still correct but precise
01:15:13.443 and finite as per Professor Kernahan s advice,
01:15:16.173 which is to say, use your own vernacular of English
01:15:18.590 and just say what you mean.
01:15:19.860 But very succinctly, there's no one way to write pseudo code.
01:15:22.951 It's not some formal language. I'm just going to convert the steps I
did intuitively
01:15:27.250 to step by step instructions as follows.
01:15:30.201 Step one, what I did was pretty much pick up the phone book.
01:15:33.880 Step two,
01:15:34.641 what I did was pretty much open to middle of phone book for the third
algorithm.
01:15:38.681 Step three, look at page, step four,
01:15:41.960 if person is on page,
01:15:44.020 then step five, call person
01:15:46.500 if that does not prove to be the case. Step six.
01:15:49.529 Else if the person is earlier in the book, then
01:15:52.870 open to the middle of the left half of the book
01:15:56.089 and then go back to line three.
01:16:00.399 Then
01:16:01.220 if the PLT if the person is later in the book,
01:16:04.509 open to the middle of the right half of the book and again, go to line
three.
01:16:09.240 ELT, there's 1/4 and final case.
01:16:11.830 If the person like John Harvard is not on the page is not earlier is
not later.
01:16:15.609 What's the fourth scenario we'd best consider?
01:16:18.779 He's just not there else. We should do something specific like quit.
01:16:22.660 Now, as an aside,
01:16:23.609 everyone in this room has probably had one of
01:16:25.279 these stupid technical support issues where your phone or your
01:16:27.979 laptop or your desktop computer just freeze all of a
01:16:30.520 sudden or maybe it spontaneously reboots for no reason.
01:16:33.299 Odds are that's because not you, but some other human made a mistake.
01:16:38.149 They probably wrote code working at Microsoft or Apple or Google or
somewhere else.
01:16:42.529 And they didn't actually anticipate that, oh, there could be 1/4
scenario
01:16:46.779 that could happen in the real world.
01:16:48.785 But if there's no code that tells the computer
01:16:51.075 what to do in that fourth and final scenario,
01:16:53.725 who knows what the computer is going to do?
01:16:55.604 It might by default reboot, it might be by default freeze.
01:16:58.794 That's just a hint of the bugs, the mistakes in software to come.
01:17:02.825 But even though this is just one way to write this code
01:17:07.004 A K a pseudo code.
01:17:08.524 There are some salient characteristics that we'll use throughout
today.
01:17:12.024 One, there are these verbs, these actions and henceforth is a spot
01:17:16.609 and computer scientists or programmers,
01:17:18.259 we're going to start to call these by what uh
01:17:20.060 more and more technical audience would these are functions,
01:17:23.509 a function is an action or a verb.
01:17:26.060 It's like a bite size task that a computer can do for you.
01:17:29.089 Those then are functions in this here pseudo code.
01:17:32.140 But there's other types of code in here. There are these things here.
If e
01:17:36.419 if,
01:17:36.970 if
01:17:37.450 e
01:17:37.640 those are examples of what we're going to start calling conditionals.
01:17:40.740 These are sort of proverbial forks in the road where maybe you go this
way,
01:17:44.359 maybe you go this way, but you decide which way to go based on a
question.
01:17:49.504 The questions that you ask are what we'll
01:17:51.584 technically call boolean expressions named after mathematician Bull.
01:17:55.734 A boolean expression is a question with a yes or no answer, a true or
false answer.
01:18:01.654 A black or white answer a one or zero answer.
01:18:04.725 There's two possibilities and there's a hint of the binary underneath
a
01:18:08.314 boolean expression is going to tell you yes or no.
01:18:11.515 You should go down that fork in the road.
01:18:14.759 Notice what's important here is that indentation mattered as a result.
01:18:19.890 Notice that on line four, when I first asked if the person is on page
question mark,
01:18:25.000 so to speak, I should only do line five per its indentation.
01:18:28.979 If the answer is yes or true,
01:18:31.799 I should only open to the middle of the left half of the book and go
back to line three if
01:18:36.496 person is instead earlier in the book.
01:18:38.485 So indentation in pseudo code and in
01:18:40.496 many programming languages has a logical significance.
01:18:43.625 It tells you whether to do things or not.
01:18:46.226 But there's another construct in here,
01:18:48.405 go back to go back to which literally makes me go
01:18:51.456 back to line three potentially again and again and again,
01:18:54.645 creating some kind of cycle or what we'll typically call
01:18:57.556 a loop
01:18:58.492 instead.
01:18:59.372 So even in this relatively simple real world algorithm,
01:19:02.852 we have these four fundamental characteristics of most computer
01:19:06.151 programs that we will write in this class.
01:19:07.892 And you might write beyond this class
01:19:09.952 that we have some technical jargon now to describe them.
01:19:12.602 But what's important to note is that line eight and line
01:19:15.952 11, even though they're saying, go back to line three, go back to line
three.
01:19:20.359 You might think you're running the risk of what we'll call an
01:19:23.069 infinite loop where you literally get stuck in a loop forever,
01:19:26.560 which doesn't sound like a good thing if at some point,
01:19:29.009 you want to turn your computer off, even though it's still working.
01:19:31.470 But
01:19:32.439 these will not induce infinite loops. Why?
01:19:35.189 What is happening in this particular algorithm every time
01:19:37.950 we go back to line three that guarantees eventually
01:19:41.910 we will stop going back to line three
01:19:46.750 exactly if the person is on the page, we will call them or we will
quit.
01:19:50.189 But more importantly, because we keep dividing and conquering the
problem.
01:19:54.009 In this case, having the phone book, having the phone book,
01:19:56.819 eventually we're going to run out a phone book.
01:19:59.370 In which case indeed,
01:20:00.459 John Harvard is either on that page or not and we will call or we will
quit instead.
01:20:05.580 So we'll see in time and in fact, allow me to promise,
01:20:08.339 odds are at some point you will write code that seems to
01:20:11.506 take control of the computer for you where it's doing something,
01:20:14.457 doing something, doing something and it literally won't respond to you
anymore.
01:20:17.577 That's just gonna be because of a mistake,
01:20:19.506 a so called bug that you yourself will
01:20:21.486 invariably have uh added to your code accidentally.
01:20:25.006 But we will show you ways for terminating it or breaking out of those
conditions.
01:20:29.336 And indeed what we'll do in just a little bit after
01:20:31.387 a break for today's lecture is explore not just these concepts but
01:20:35.704 of the ways you can use them to solve real and very visual and audio
problems.
01:20:40.474 But for now,
01:20:40.994 let's at least connect it to something that's been all too germane in
recent months,
01:20:44.374 the past few years, namely artificial intelligence,
01:20:46.994 which is the topic we'll come back to at the end of the course too,
01:20:49.954 to give you a sense of like what the connection is with what
everyone's
01:20:52.874 been talking about in the world of A I and what it is,
01:20:55.554 we're going to spend the next few weeks building up to by writing
code.
01:20:59.104 If you were to,
01:21:00.181 to implement
01:21:01.090 something like a chat bot,
01:21:02.500 for instance, that just answers questions and has a conversation with
you,
01:21:06.050 you could do that using pseudo
01:21:07.561 code. And as we'll soon see, you can use uh C Python any number of
other languages too,
01:21:12.721 that pseudo code might look like this.
01:21:14.491 When implementing a chat bot, you could tell the chat bot
01:21:17.250 if the student says hello to you, then say hello back.
01:21:21.011 And the indentation as per earlier implies this is conditional.
01:21:24.930 E
01:21:25.279 if the student says goodbye to you, say goodbye to the student.
01:21:29.520 If the student asks you how you are, say you are well,
01:21:32.970 so you can just enumerate question after question after
01:21:35.399 question and just handle all of these conditional possibilities.
01:21:39.540 But things kind of escalate quickly,
01:21:41.209 especially with the tools of today like chat GP T.
01:21:43.759 Are we really gonna have the wherewithal
01:21:45.700 as programmers to write another conditional?
01:21:47.959 Like else if the student asked why 111 in
01:21:49.910 binary seven in decimal like this kind of hints?
01:21:52.350 Oh my God.
01:21:52.970 There's like an infinite number of things this human could ask the
chatbot.
01:21:56.470 Do we really have to write an infinite number of conditionals?
01:22:00.140 Like that's just not possible.
01:22:02.180 Like there's not enough time in the day,
01:22:03.620 there's not enough lines of code available we artificial intelligence
surely
01:22:07.500 needs to be able to figure some of this out instead.
01:22:10.580 And so indeed, this is not how you implement A I,
01:22:14.069 but rather how you implement an A I like a chatbot is
01:22:17.339 you typically train it based on lots and lots of data,
01:22:21.140 you give it lots of inputs, lots of inputs, training data
01:22:24.410 and let it figure out
01:22:26.279 what it should say in response to certain questions.
01:22:29.270 And it boils down to a lot of probability,
01:22:31.220 a lot of statistics otherwise known now as large language models,
01:22:34.850 which if we really peek under the hood are actually typically
implemented
01:22:38.100 with what are called neural networks inspired by the world of biology,
01:22:41.250 whereby we humans have all of these neurons that tra transmit
electrical signals,
01:22:45.629 such that my brain tells my hand to
01:22:47.410 this way, this way and this other way.
01:22:49.229 And so what computer scientists have been doing over the past many
years
01:22:53.399 is implementing in software using literally zeros and ones graphs or
networks,
01:22:59.180 neural networks that look a little something like this
01:23:01.270 where each of the circles represents a neuron,
01:23:03.290 each of the arrows represents a pathway between them
01:23:06.439 and provides as input to these networks,
01:23:09.160 huge amounts of data like all of the internet, all of Wikipedia,
01:23:12.660 all of the books that it might consume as input.
01:23:15.149 And then the goal of this neural network as per this single final
neuron right here
01:23:20.520 is to produce an answer to a question. Maybe it's simple, like, yes,
no.
01:23:24.479 Or maybe it's something like the answer to the 111 question
01:23:28.209 or how are you or goodbye or hello or the like.
01:23:31.899 And what these neural networks do is use statistics and probability
01:23:35.569 and try to output the most probabilistically likely answer to this
01:23:40.683 question that's been asked
01:23:42.362 and really just hope
01:23:43.772 that it is correct.
01:23:44.993 There's no programmer at open A I or Google or Microsoft that's
01:23:48.232 trying to anticipate every one of these questions we might ask,
01:23:51.592 not only in English but in other languages as well.
01:23:55.183 So you might be wondering why there's this like 8 ft duck on the
stage.
01:23:58.033 So the persona that CS fifty's own A I takes is in fact
01:24:01.692 that of a rubber duck because it turns out in programming circles.
01:24:04.692 And this is true long before CS
01:24:06.645 it has often been recommended to students and aspiring programmers
that
01:24:10.045 you keep like literally a physical rubber duck on your desk.
01:24:13.395 The idea being in the absence of a friend, family member,
01:24:16.605 colleague T A who could answer technical questions for you.
01:24:20.346 If you're alone in your room and Mather at night, you can sort of talk
to the duck,
01:24:24.645 maybe door closed and ask the duck your questions, or more
importantly,
01:24:28.886 talk the duck through what confusion you're having.
01:24:31.879 And the mere act of talking through the problem
01:24:35.109 explaining logically what you're trying to do what you're actually
01:24:38.209 doing and what the error actually is invariably that sort
01:24:41.169 of proverbial light bulb goes off and you realize,
01:24:43.109 oh, I'm an idiot.
01:24:44.569 I hear in my own words where I've gone awry.
01:24:47.589 And even though this duck will never say anything back to you
01:24:50.459 that alone,
01:24:51.470 rubber duck debugging or rubber ducking tends
01:24:54.549 to be a valuable programming technique,
01:24:56.709 believe it or not.
01:24:57.783 But thanks to these large language models,
01:24:59.712 we have not only physical but virtual ducts as well
01:25:02.453 and so available to you will be in this class,
01:25:04.652 not tools like chat GP T and the like which are through policy
disallowed.
01:25:09.092 It is un it is not reasonable to use chat GP T and the like.
01:25:12.272 But you are allowed and encouraged to use CS fifty's own A I
01:25:16.373 based tools which resemble those same tools but know something about
CS 50
01:25:21.042 aspire to behave akin to a good teaching fellow,
01:25:23.675 guiding you to solutions as opposed to handing you something outright.
01:25:27.826 So this is a tool that will be available literally this URL throughout
the course,
01:25:31.605 CS 50 dot A I,
01:25:32.875 it will also be embedded in the programming environment you'll soon
meet,
01:25:36.315 which is called visual studio code, a cloud based version thereof.
01:25:39.755 The duck will live in that environment as well as well as on stage
from time to time,
01:25:44.956 which is to say will not only talk about but use throughout the
course,
01:25:49.016 this thing known as A I
01:25:51.100 but this is ultimately code
01:25:53.009 that we're going to start writing next week.
01:25:54.689 And unfortunately, this code here is written in a language called c,
01:25:57.850 this is essentially the program that I lost two points on like some 25
plus years ago.
01:26:02.229 And it, it does look admittedly cryptic and that's why today,
01:26:06.370 what we'll focus on is not what this code looks like, nor
01:26:10.490 the zeros and ones that, that code gets converted to.
01:26:13.959 So your computer can understand as input what you want to do.
01:26:17.490 We're going to focus on a much more visual incarnation of this.
01:26:20.569 But I know thus far this has been a lot.
01:26:22.290 So let's go ahead and take a five minute break here.
01:26:24.930 And when we come back in five, we'll do some actual programming. So
see you in five.
01:35:41.160 All right.
01:35:44.109 So it's now time to solve with actual code,
01:35:47.859 some actual problems albeit in a fun and visual and audio way.
01:35:51.890 But recall that where we left off was this like starting next week,
01:35:55.439 you'll be writing code that ultimately looks like this.
01:35:58.109 But thankfully,
01:35:58.950 you will not be writing zeros and ones and no normal person myself
included
01:36:02.629 can understand what all of these zeros and ones are at a glance,
01:36:06.270 we could take out some paper pencil and probably figure it out
01:36:09.098 tediously. But this is exactly the point. Computers only understand
this stuff.
01:36:14.138 But what we as programmers will start writing today and beyond
01:36:18.428 is code at a higher level.
01:36:20.978 And indeed,
01:36:21.317 this is gonna be uh this is gonna be frequent within computer
01:36:24.618 science where there's different like levels of abstraction that we
operate at and
01:36:28.328 the lowest level the nitty gritty is like the zeros and ones
01:36:31.147 that computer understand like that's it in this class for zeros ones.
01:36:34.226 Hopefully you at least have wrapped your mind around why zeros and
ones can be
01:36:38.616 used in triples and in uh as bytes to represent higher and higher
numbers.
01:36:43.445 But let's just now agree that computers can do that.
01:36:46.666 Let's abstract away from that detail and focus on higher level
01:36:50.855 languages than zeros and ones namely a language like this.
01:36:53.815 So this is an example of the very first
01:36:55.596 programming language I learned back in the day.
01:36:57.465 But as per that homework
01:36:58.764 in a language called C, it's an older language,
01:37:01.324 but it remains one of the most
01:37:02.844 popular languages in omnipresent languages nowadays because it's
01:37:06.753 incredibly fast and it's particularly good at
01:37:10.003 making devices operate quickly for us pedagogically.
01:37:14.324 The value of C is not that you're probably in Silicon Valley
01:37:18.324 and other such jobs going to be using C yourself that much.
01:37:21.594 But because it's going to provide a conceptual
01:37:23.651 foundation on top of which we introduce other languages like Python,
01:37:27.631 which is newer and improved, so to speak,
01:37:29.821 that gives you more and more functionality sort of for free out
01:37:33.262 of the box by abstracting away some of the stuff we'll focus on
01:37:37.532 in the coming days first.
01:37:38.762 So at the end of the day,
01:37:39.562 you should better understand languages like Python and javascript and
SQL
01:37:43.961 because of your underlying understanding of a language like C.
01:37:48.740 But this is too much for the first day.
01:37:50.620 Many of you will think that this is too much for the second week.
01:37:53.120 But in fact,
01:37:54.049 c is really only sort of scary looking
01:37:57.100 because all of this darn punctuation in syntax,
01:37:59.490 the semicolon, the parentheses, the double quotes,
01:38:01.790 the curly braces and the like and
01:38:03.259 I concur like this is intellectually uninteresting.
01:38:06.209 And a lot of the challenges early on when learning programming
01:38:08.979 is you just don't have the muscle memory like that.
01:38:11.129 I or some of the teaching fellows might
01:38:13.441 or knowing what symbol should be where,
01:38:15.992 but that's gonna come with time and practice.
01:38:18.671 I guarantee it
01:38:20.151 what we'll do for today though is just throw away all of that un
intellect,
01:38:23.961 uh intellectually uninteresting detail
01:38:26.401 and focus really on ideas.
01:38:28.032 And some of you might be in your comfort zone here because if
01:38:30.832 back in middle school you were playing with a programming language
called scratch,
01:38:34.271 you were probably using at the time just to have fun in class or out
of class,
01:38:37.832 make games, animations, interactive art.
01:38:40.333 What you probably didn't use it for,
01:38:42.483 at least in middle school was to
01:38:44.233 consider and explore programming languages themselves.
01:38:47.324 But what's wonderful about scratch?
01:38:49.054 Which is this graphical programming language from down the
01:38:51.664 street at mit where it was invented some years ago
01:38:54.304 is you can program not by using your keyboard per se,
01:38:57.293 but by dragging and dropping puzzle pieces,
01:39:00.153 otherwise known as blocks that will snap together if it makes logical
sense to do so.
01:39:04.916 And what you won't have to deal with is parentheses
01:39:07.076 and qu uh double quotes and semicolons and all of that
01:39:10.215 at least until next week.
01:39:11.395 But the nice thing about scratch is that after this
01:39:14.005 week and after the so called problems at zero,
01:39:16.005 the first assignment in which you'll use scratch,
01:39:18.136 you'll have a mental model via which it will be easier
01:39:21.715 to pick up all of the subsequent syntax as well.
01:39:24.886 So let's see how we can start programming in scratch
01:39:27.850 by making the simplest of programs.
01:39:29.620 First, you can do this at scratch.mit.edu, you needn't do this.
01:39:34.120 Now in the moment, problems at zero will walk you through all of these
steps.
01:39:37.419 But what I've done here is opened up
01:39:40.049 at scratch.mit.edu precisely the default uh web page there in
01:39:45.640 this is after having clicked the create button in scratch,
01:39:48.370 which is going to allow me to create my first program.
01:39:50.609 But first a tour of the user interface here and what is ultimately
available to you.
01:39:56.120 Well, within the
01:39:56.972 patch environment,
01:39:58.321 we'll see a few different regions of the screen one.
01:40:01.202 We have this palette of puzzle pieces that left
01:40:04.091 the blue ones relate to motion,
01:40:06.082 the purple ones relate to looks, the pink ones relate to sound and so
forth.
01:40:10.341 So the color of the blocks just roughly
01:40:12.082 categorizes what that blocks purpose in life is
01:40:15.202 we're going to be able to use those puzzle
01:40:16.901 pieces by dragging and dropping them from left to right
01:40:20.122 in the right here in the middle of the screen
01:40:21.812 is where I'm going to write my actual programs.
01:40:23.762 This is where I'll drag and drop these puzzle pieces, lock them
01:40:26.094 together and actually write my code.
01:40:28.784 What am I going to be coding?
01:40:30.644 Well, I'm going to be controlling one or more sprites much like in the
world of games.
01:40:34.133 If familiar a sprite is like a character that you might see on the
screen.
01:40:37.443 The default character in the world of scratch is in fact a cat that
looks like this.
01:40:41.764 And if in this case, I have just one cat,
01:40:44.304 I can then make that cat do things in his own little world at top,
01:40:48.014 right by making the cat move up down left, right,
01:40:50.804 spinning around or doing other things as well.
01:40:53.224 But if you want to introduce a dog or a bird or
01:40:55.215 number of other custom characters,
01:40:57.085 you just add more sprites and they get their own place in that same
world
01:41:01.405 as for how to think about movement in this world.
01:41:03.996 It's actually pretty familiar even though it gets a little numeric for
a moment.
01:41:06.925 If scratch at the moment is in the middle of the screen,
01:41:10.025 the cat is at zero, comma zero.
01:41:11.985 If you think about XY coordinates or latitude longitude.
01:41:14.766 If you move the cat all the way up,
01:41:16.306 this would still be X equals zero but it would be Y 180.
01:41:19.936 What's the 1 8180 pixels vertically or dots on the screen?
01:41:24.337 This is negative 180 pixels on the screen at the bottom.
01:41:27.647 By contrast, if you go left and right,
01:41:29.687 your X value might change negative 240 but Y is zero or positive 240
01:41:35.757 Y is zero as well.
01:41:37.127 But most of the time you won't need to know or
01:41:39.288 care about what the pixel coordinates of the cat are.
01:41:42.547 All you're generally going to care about is the programmer most likely
is,
01:41:45.917 do you want the cat to go relatively up down left or right?
01:41:49.217 And let mit figure out the mathematics of moving this thing around in
most cases?
01:41:53.859 All right.
01:41:54.529 So let's go ahead and introduce the first of these programs
01:41:57.220 by doing something quite simple as we did in C there.
01:42:00.220 But a little more simply by writing code as follows.
01:42:03.979 I'm gonna go back to scratch.mit.edu,
01:42:06.790 I've already clicked per before the create button.
01:42:09.689 And if I click on the yellow category of blocks here at left and I'll
zoom in,
01:42:13.919 we'll see a whole bunch of yellow puzzle
01:42:15.790 pieces and probably the most common one you will
01:42:18.240 use to write code in scratch for just this
01:42:20.379 first week is literally when green flag clicked.
01:42:23.520 Why? Well, if we go back over to the cat's world at top, right.
01:42:26.964 Notice that above the cat's rectangular world,
01:42:30.015 there's not only a green flag for starting,
01:42:32.154 there's a red stop sign for stopping as well.
01:42:35.154 So let's do this.
01:42:35.955 Let me go ahead and click and drag when green
01:42:38.165 flag clicked anywhere into the middle and let go.
01:42:40.970 And now
01:42:42.080 I'm gonna go to looks
01:42:43.910 and it looks like there's a whole bunch of purple puzzle pieces here.
01:42:46.939 I'm gonna choose something simple, like say hello,
01:42:49.549 drag it
01:42:50.560 and notice if I get just close enough,
01:42:52.569 it's gonna want to magnetically snap together.
01:42:54.790 So I'll just do that and it does its thing.
01:42:56.759 The fact that there's this white oval with text
01:42:58.890 means that is an input to this say puzzle piece.
01:43:03.450 I can literally then change what the input is if
01:43:05.959 I want to more conventionally say hello Comma World,
01:43:09.029 which in fact,
01:43:09.890 according to Lore was the very first program written in C and
nowadays,
01:43:14.810 in most every language
01:43:16.779 including in Brian Carnahan's book.
01:43:18.569 So Hello World is generally the first
01:43:20.410 program that most any programmer first writes.
01:43:23.470 So that's it as programs go. Let me go ahead and zoom out here.
01:43:26.770 Let me go over to the right and click
01:43:28.589 the green flag and somewhat excitingly maybe underwhelming.
01:43:32.310 We've now written a program that quite simply says Hello World on the
screen.
01:43:36.810 Now, let's make this a little more technical for just a moment. What
is this here?
01:43:40.020 Puzzle piece as I keep calling it,
01:43:41.899 it's actually a similar in
01:43:44.140 it's an incarnation of one of the ideas from our pseudo code.
01:43:47.899 Before, what last time before break did we call? Fun?
01:43:51.589 Ah
01:43:51.899 Damn it.
01:43:52.779 What,
01:43:53.919 what did we call those actions and verbs last time in my pseudo code
01:43:59.240 functions? That's right.
01:44:00.830 So these purple puzzle pieces here are indeed functions and some
01:44:05.470 functions as we can see take inputs like hello comma world.
01:44:09.450 After all, how does scratch know what to say?
01:44:12.029 You have to provide the cat with input,
01:44:14.430 which is to say functions can indeed take inputs like this.
01:44:17.180 In this case, one input.
01:44:18.350 But we'll see opportunities for passing in more input as well.
01:44:22.051 The cat is doing though visually on the screen here
01:44:24.532 at top right is what's generally called a side effect.
01:44:27.282 Sometimes when you call a function, it does something visually.
01:44:31.062 And in this case, you're seeing literally a cartoon speech bubble.
Hello world.
01:44:34.762 That is the side effect of this function.
01:44:37.032 So if we now want to map this to our world
01:44:39.182 of inputs and outputs and see where this side effect is.
01:44:41.762 This is the paradigm I proposed at the start of class that is
01:44:44.863 computer science in a nutshell and will
01:44:47.014 be the framework we use literally throughout
01:44:49.133 the class no matter how uh no matter how the languages in particular
evolve.
01:44:53.983 So what's the input to this particular program?
01:44:56.724 Well, this white oval hello world is my input.
01:44:59.644 The algorithm step by step instructions for solving some problem is
01:45:03.113 implemented in code this language called scratch by way of this purple
01:45:07.675 nozzle piece.
01:45:08.505 And the output of that function given this input is the side effect
whereby
01:45:13.746 the cat indeed says hello world visually on the screen in that speech
bubble.
01:45:19.065 So the exact same paradigm with which we began
01:45:21.286 today governs how exactly this cat here works.
01:45:24.666 Well,
01:45:24.925 let's actually go back to this program and
01:45:26.425 make it a little more interesting than that.
01:45:28.485 Let me go ahead and click the red stop sign
01:45:30.640 and let me actually use a different type of puzzle piece. Another
function
01:45:34.589 that does something a little different. First, I'm gonna get rid of
the say block.
01:45:38.330 So I'm gonna not only pull it away,
01:45:39.950 I'm gonna drag it over anywhere it left and just let go and
01:45:42.819 it will delete itself automatically or I could right click or control
click.
01:45:47.069 And from a little menu, I could also explicitly say delete.
01:45:50.310 And what I'm gonna do now is under sensing which is a
01:45:53.629 light blue shade of puzzle piece.
01:45:55.950 There's a whole bunch here, but I'm gonna focus on this one,
01:45:58.339 ask something and wait.
01:46:01.160 And the default text is what's your name? And that's fine.
01:46:03.589 But because it's a white oval that input can be manually changed by
me.
01:46:06.859 If I wanted to change the question,
01:46:08.600 I'm gonna drag it over here. It's gonna magnetically
01:46:11.279 slaps
01:46:11.660 uh snap together and I'm OK with that question. But what do I want
01:46:15.759 to say with the answer? Well, let's go ahead and do this. I could go
to looks again,
01:46:20.729 I could grab another say block, let it snap in
01:46:24.060 and I could say something like uh hello comma David.
01:46:28.669 But this is gonna be the first of many bugs that I make intentionally
or otherwise.
01:46:32.839 Let me click the green flag
01:46:34.770 scratches now, just like in a web browser prompting me for some input
here.
01:46:39.069 So let me go ahead and type in my name David. Enter
01:46:42.069 and Voila
01:46:42.750 like it works. Hello David.
01:46:45.149 I'm kind of cheating though, right? Because if I zoom out,
01:46:48.189 stop and play again, let me type in like Julia's name here, enter
01:46:53.180 and it still says hello comma David.
01:46:55.290 So that didn't really implement the idea that I wanted. All right.
01:46:58.459 So how can I fix this? Well, it seems that this time I want more than
a side effect.
01:47:02.799 I want to use the value that the human types in.
01:47:05.729 And for this,
01:47:06.620 we need another feature of functions which is that
01:47:08.939 not only can they sometimes have side effects,
01:47:10.939 something visually happens,
01:47:12.350 some functions can hand you back a value
01:47:15.076 you a so called return value that will allow
01:47:18.485 you to actually reuse whatever the human typed in.
01:47:22.715 So a return value is something that gets virtually handed back to you
01:47:26.246 and you can store it in something called a variable like XY and Z in
mathematics.
01:47:30.266 And you can generally reuse it one or more times.
01:47:33.346 So let me actually draw our attention then too
01:47:36.016 at left, not only the blue puzzle piece, ask what's your name and
weight,
01:47:40.246 but notice that there's a special
01:47:42.071 ple piece below it,
01:47:43.082 this blue oval called answer and that represents what
01:47:46.591 a computer scientist would call a return value.
01:47:48.751 So mit has kind of bundled it together side by side to
01:47:52.032 make clear that one of those pieces relates to the other.
01:47:54.932 What it means is that I can do this, I can drag this oval
01:47:58.821 and use this oval as the input to the safe function.
01:48:02.021 Now notice it's not the same size but it is the right shape. So that's
OK.
01:48:05.191 Scratch will sort of grow or shrink things to fit properly.
01:48:09.129 But this too isn't quite right. Let me go ahead and do this.
01:48:12.830 Let me go ahead and stop that.
01:48:14.310 Click the green flag. I'll type in my name again. Dav ID
01:48:17.399 enter
01:48:19.000 and it's just kind of like weird or rude like I wanted a hello at
least.
01:48:22.669 And it just said David on the screen. OK. So I can fix that.
01:48:25.560 Let me stop with the red stop sign.
01:48:27.669 Let me just separate these temporarily and I can leave it in the
middle there,
01:48:31.089 but they have no logical connection temporarily.
01:48:33.770 Let me go back up to looks, let me grab a say block a second one
01:48:38.205 and let me go ahead and say just to be grammatical like hello comma
space
01:48:42.294 and then I'll reconnect this here. So at the moment it looks like what
I want.
01:48:46.345 I want a hello comma and then the return value
01:48:50.015 printed out based on whatever the human typed in.
01:48:52.814 So let me zoom out. Let me click the green flag
01:48:55.814 again. What's your name? Davdav ID and watch the cat's side effect
01:49:01.165 enter.
01:49:02.910 It's still not greeting me properly.
01:49:06.520 There's no hello. And if in case it was too fast, let's do it again.
01:49:09.549 Green flag. Dav ID, enter it just, it rudely just says my name, which
is weird.
01:49:14.560 What's the bug here though? It's a little more subtle.
01:49:18.740 Why? Yeah.
01:49:22.770 Yeah. It's just too quickly going over the say command or the say
function.
01:49:26.109 In this case, my my Mac your PC, your phone is just so darn fast.
01:49:29.600 Both are happening but too fast for my human eyes to even
01:49:32.379 notice so we can solve this in a number of ways.
01:49:35.339 I could actually use a different puzzle piece altogether.
01:49:37.700 In fact, mit kind of anticipated this notice.
01:49:39.839 The first puzzle piece in purple is say hello
01:49:43.000 for a specific number of seconds and you can
01:49:45.109 specify not just the message but the number of
01:49:46.990 seconds ergo two inputs otherwise now known as arguments to
01:49:50.976 function, an input to a function is just an argument now,
01:49:54.416 um And that would be a fix here. I could maybe a little more
explicitly do this.
01:49:59.235 I could go under events, scroll down a little bit and sorry, under
control in orange.
01:50:04.826 I could grab a weight block and I could kind of
01:50:07.255 insert it in the middle and this might actually help.
01:50:09.925 So I could click on the green flag. Dav ID, enter. Hello,
01:50:14.616 David. And I could change the timing to be a little more natural.
01:50:17.826 But what if I want the cat to just say hello comma David all in one
breath, so to speak.
01:50:22.412 Well, for that, I'm gonna need to use a slightly different technique
as follows.
01:50:26.321 Let me go ahead and get rid of the weight.
01:50:28.251 Let me get rid of the second say block and stop the cat with the stop
sign.
01:50:32.001 Let me go under operators here
01:50:34.571 and let me somewhat cleverly grab this
01:50:36.782 a join block at the bottom by default, it's using apple and banana as
placeholders,
01:50:41.162 but those are white ovals so I can change those.
01:50:43.372 Let me drag this over the white oval for the say function
01:50:47.660 and let go and it will snap to Phil.
01:50:49.709 Let me go ahead here and type hello comma space instead of apple.
01:50:54.919 And what should I do instead of banana?
01:50:58.680 Yeah. So the answer return
01:51:00.810 val uh uh the return value. So let me go under sensing again.
01:51:05.549 Let me just drag another copy of it and you can use these again and
again and again,
01:51:08.750 they don't disappear.
01:51:09.839 I want to drag answer over banana. So that the second input to join
01:51:15.149 is actually if you will the output of the ask block like that and it
snaps to fit.
01:51:21.419 So now if I go ahead and click the green flag once more, DAV ID enter,
01:51:25.740 now we have the sort of behavior aesthetically that I cared about.
01:51:29.830 But beyond the aesthetics of this,
01:51:31.529 the goal here really was to map it to again the same paradigm which
we'll see here.
01:51:36.529 The algorithm and the output and the input for this example are as
follows.
01:51:40.379 The input to the save block was quote unquote. What's your name?
01:51:43.569 The function of course implementing that algorithm and
01:51:46.240 code was the ask and weight block.
01:51:48.589 The output though of the ask block recalls, not some visual side
effect.
01:51:53.140 It is a return value called answer like a variable,
01:51:56.399 a special variable like XY and Z in math.
01:51:58.830 But in this one, we generally in programming
01:52:01.359 describe variables with actual words, not just letters,
01:52:04.709 but this output of
01:52:06.459 the say block.
01:52:07.729 I kind of want to make room for it to make
01:52:10.450 pass it into the say block as a second argument.
01:52:13.259 So let's do this.
01:52:13.870 Let's take one step back and propose that now for the join block that
I just used,
01:52:19.410 it takes two inputs.
01:52:20.549 Hello comma space
01:52:21.910 and answer
01:52:23.290 the function in question is indeed the join block.
01:52:26.080 The output of this had better be hello comma David. What do I
01:52:29.350 want to do with the output of the join block? Well, let me clear the
screen.
01:52:33.180 Let me move this over because now the output of
01:52:36.439 the join block is going to instantly become the input to
01:52:40.379 the same block so that the output now in this
01:52:43.020 multi step process is the side effect of hello world.
01:52:46.950 So the fact that I nested these blocks on top of one another,
01:52:52.240 very much deliberate if I zoom in here,
01:52:54.220 notice that hello and answer are on top of join, join is on top of the
say block.
01:53:00.109 And if you think back to like high school math,
01:53:01.509 this is like when you had parentheses and you had to
01:53:03.500 do the things inside parentheses before the things outside
parentheses.
01:53:06.459 It's the same idea, but I'm just visually stacking them
01:53:09.459 instead,
01:53:10.410 but outputs can become inputs depending on what the function there
expects.
01:53:15.310 Let me pause here and see if there's any questions about
01:53:17.979 not so much like what the cat is doing, but how the cat
01:53:21.620 is doing this
01:53:24.379 questions at hand.
01:53:27.660 All right. Well, let's make the cat more catlike and do this.
01:53:31.740 Let me throw away all the say block and just
01:53:34.299 let go there and let me introduce at bottom left,
01:53:36.770 a nice feature of scratch whereby there's also
01:53:38.600 these extensions that tend to use the cloud,
01:53:40.660 the internet to give you even more functionality.
01:53:42.859 And in fact, I'm gonna click on this extension up here, text to
speech.
01:53:46.089 And if I click on that,
01:53:47.299 I suddenly get a whole new category of blocks at the bottom text to
speech,
01:53:51.479 they happen to be green.
01:53:52.819 But what's nice here is that I can actually now have the cat say
something audibly.
01:53:57.865 So let me drag the speak block here instead of the say block.
01:54:01.754 I don't want to just just say hello. Let me stop that.
01:54:04.834 So let me go back under operators.
01:54:06.834 Let me grab another join block because I threw the other one away.
01:54:09.915 Let me change apple to hello comma space again. Let me go to sensing.
01:54:13.955 Let me drag, answer to banana again.
01:54:16.665 And now let me hit the green flag
01:54:19.209 and let me type in my name Dav ID
01:54:21.580 and in a moment I'll hit enter and
01:54:23.839 hello David.
01:54:25.790 All right.
01:54:26.240 It's not exactly cat like, but it was synthesized,
01:54:29.279 but it turns out under these text to speech blocks.
01:54:31.399 There's some others set voice to alto
01:54:34.299 for instance, seems to be the default, but let's change this.
01:54:37.120 So notice that some puzzle pieces don't just take white ovals,
01:54:40.100 they might even have drop down.
01:54:41.459 So whoever created that puzzle piece decided in advance what the
01:54:45.060 available choices are for that input per the drop down.
01:54:49.129 So I'm gonna change it to squeak which sound or actually kitten sounds
even more apt.
01:54:53.060 Let me zoom out.
01:54:54.220 Click the green flag
01:54:56.169 type my name Dav ID. Enter
01:54:58.500 Millar.
01:55:01.189 That's interesting. So it doesn't seem to matter what I type.
01:55:03.779 So, how about David Malan
01:55:06.430 Muller
01:55:06.669 Muller?
01:55:07.970 So it seems to meow proportional to how long the phrase is that I
typed in.
01:55:12.200 We it can get a little creepy quickly. Like if I change kitten to
Giant,
01:55:16.160 let me go ahead and hit play Dav ID. Enter.
01:55:20.149 Hello.
01:55:22.350 So you can sort of uh
01:55:24.100 for very non-academic ways, like start to have fun with this,
01:55:27.000 but just playing around with these various inputs and outputs.
01:55:29.339 But let's actually make the cat do something more catlike
01:55:31.540 and indeed meow instead of saying any words at all.
01:55:33.979 So let me throw all of that away. Let me go now under sound, let me
01:55:38.100 the play sound until done and notice in the drop down here by default,
01:55:43.100 you just get the cat sound, you can record your own sounds.
01:55:45.770 There's a whole library of dogs and birds and all
01:55:47.959 sorts of sounds you can import into the program.
01:55:50.379 I'll keep it simple with cat and let me click the green flag.
01:55:54.970 All right.
01:55:55.640 So the cat meowed once. If I want the cat to meow again, I could do
this
01:56:00.290 if I want the cat to meow a third time I could again, hit play.
01:56:04.020 So this is kind of tedious if to play this game, I have to keep
clicking the button,
01:56:08.080 keep clicking the button to keep the cat alive virtually in this way.
01:56:11.620 So maybe I want this to happen again and again and again. Well, let me
just do that.
01:56:15.569 Let me sort of drag and drop or I could right click or control click.
01:56:19.490 And then a little menu would let me copy paste or duplicate blocks,
01:56:22.549 but I'll just keep dragging and dropping.
01:56:24.180 Let's do this.
01:56:27.330 Cat's kind of hungry, unhappy. So like, like this is like slow things
down.
01:56:31.069 So it's adorable again. So let me go under uh control.
01:56:35.129 Let me grab one of those, wait one second and I'll plop this here,
another one.
01:56:39.350 Let me plop it here. Click play again.
01:56:44.200 Cuter
01:56:45.100 less hungry. Sure.
01:56:46.750 But this program is now,
01:56:48.069 I dare say correct if my goal is to get the cat to meow three times.
01:56:52.520 But now, even if you've never programmed before, critique this
program,
01:56:56.750 it is not well designed even though it is correct.
01:57:00.899 In other words, it could be better.
01:57:03.970 How might you think?
01:57:07.180 Yeah.
01:57:08.459 So using a loop and why,
01:57:09.890 why are you encouraging me to use a loop even though it works as is?
01:57:19.770 Yeah.
01:57:20.189 So to summarize we,
01:57:21.330 it's just easier to use a loop because I could specify explicitly in
one place.
01:57:24.890 How many times I wanted to loop.
01:57:26.390 And moreover, frankly,
01:57:27.930 any time you are copying and pasting something in
01:57:30.450 code or dragging the same thing again and again,
01:57:32.609 odds are you're doing something foolish.
01:57:34.609 Why? Because you're repeating yourself unnecessarily and this is a bit
extreme.
01:57:38.970 But suppose I want to change this program later so that I,
01:57:41.930 the cat pauses two seconds in between mouse.
01:57:44.569 Well, obviously I can just go in here and do two.
01:57:46.910 But what if I forget?
01:57:48.240 And suppose this program isn't like five or six puzzle pieces?
01:57:51.390 Suppose it's 50 or 60 or 500 or 600.
01:57:54.270 Eventually I or a colleague I'm working with is going to screw up.
01:57:57.120 They're going to change a value in one place, forget to change it in
another.
01:58:00.459 So why are you inviting the probability of making a mistake?
01:58:03.410 Just simplify things so that you only have to change inputs in one
place.
01:58:07.950 So how can I do this? Let me zoom out.
01:58:10.040 Let me throw most of this duplication away,
01:58:12.399 leaving me with just the play and the weight function.
01:58:15.450 Let me now under control as well.
01:58:17.640 Grab one of these I could for instance, repeat as follows. Let me grab
a repeat.
01:58:21.939 I'm going to have to kind of move these in two parts. So I'm going to
move this down.
01:58:25.000 It's too small, but it will grow to fit the right shape.
01:58:27.819 Then let me reattach it up here. Let me change the default 10 to a
three.
01:58:32.319 And now I think I've done exactly what you are encouraging,
01:58:34.859 which is simplify and I click play now
01:58:40.729 mao and
01:58:43.080 yeah, so still correct, but arguably better designed as a result.
01:58:47.430 I can keep things simple and change things.
01:58:49.470 Now in just one place and it will continue to work.
01:58:52.029 But this is getting a little tedious. Now, I claim like, why am I re
01:58:55.620 implementing the idea of meowing?
01:58:57.270 Like,
01:58:57.430 wouldn't mit have been better to have just implemented a meow puzzle
01:59:02.279 piece for us because the whole thing is themed around a cat.
01:59:04.669 Why is there not a meow puzzle piece?
01:59:06.470 Why do I need to go through all of this complexity to build
01:59:09.609 that functionality? Well, what's nice about scratch?
01:59:12.419 And what's nice about programming languages in general is
01:59:14.580 you can generally invent your own puzzle pieces,
01:59:17.160 your own functions and then use and reuse them.
01:59:20.279 So let me go ahead and do this. I'm going to go under my blocks in
pink down here.
01:59:24.830 I'm going to go ahead and click,
01:59:26.000 make a block and I'm going to be prompted with this interface here and
I'm going
01:59:29.640 to call this block literally meow because apparently
01:59:31.799 mit forgot to implement it for us.
01:59:33.879 And I'm just going to go ahead and immediately click. OK.
01:59:36.390 And what you'll see now is two things. One on the screen.
01:59:39.819 I've been given this placeholder pink piece that says define meow
01:59:44.399 as follows.
01:59:45.419 So anything I attach to the bottom of that defined
01:59:48.180 block is going to define the meaning of meowing.
01:59:51.495 And at top left, notice what I have under my blocks.
01:59:54.495 I now have a pink puzzle piece called me
01:59:57.555 that is a new function that will do whatever that
02:00:00.395 other block of code tells the cat to do.
02:00:03.785 So, what do I want to do here? Well, I'm gonna keep it simple for now.
02:00:06.700 I'm gonna move the play sound meow until done and wait two seconds.
02:00:10.899 So let's change it back to one second to move things along.
02:00:13.680 And now let me drag the meow puzzle piece over to my loop. Such that.
02:00:18.589 Now, what's it gonna do? It's gonna meow three times and just to be
02:00:22.689 dramatic out of sight out of mind, let me for, for no technical
reason,
02:00:27.189 just drag this all the way the bottom of the screen
02:00:29.774 and then scroll back up just to make the point visually
02:00:32.705 that now meowing exists,
02:00:34.944 that is an implementation detail that we can abstract away,
02:00:38.384 not caring how it exists because I now know at
02:00:41.884 a higher conceptual level if I want to meow,
02:00:44.115 I just use the meow puzzle piece and I or
02:00:46.004 someone else dealt with already how to implement meowing.
02:00:49.395 So now let me go ahead and hit play.
02:00:55.819 OK?
02:00:56.299 So same exact code but arguably better design
02:00:59.169 because I've now given myself reusable code.
02:01:02.330 So I don't have to copy, paste those several blocks I can just use now
again and again.
02:01:06.250 But let's make one refinement.
02:01:07.729 Let me actually scroll down to where I did in fact implement this.
02:01:10.830 Let me control click or right click on it and let me edit the pink
02:01:14.970 block that I created a moment ago because I want to practice what I've
been
02:01:18.812 teaching about inputs. So I don't want this function just to be called
meow.
02:01:22.272 I want this function to also take an input and
02:01:26.283 just for consistency with our use of N earlier,
02:01:28.643 which in computer science generally means number.
02:01:30.973 Let me meow
02:01:32.323 n times and just so that this puzzle piece is even more programmer
friendly.
02:01:37.223 Let me add just a textual label that has no technical significance of
than to
02:01:42.155 make this function sort of read left to right in a more English
friendly way.
02:01:46.215 Meow and times let me click. OK.
02:01:49.416 And now notice this thing at the bottom has
02:01:51.755 changed such that it's not only called meow,
02:01:54.436 there's explicit mention of N which is a circle which is exactly
02:01:59.145 the variable shape that we saw earlier when it was called answer.
02:02:02.996 This is not a return value though.
02:02:05.016 This is what again, we're gonna call an argument, an input two way
function.
02:02:09.029 So let me do this.
02:02:10.040 I'm gonna move this back up to the top so I can see everything in one
place
02:02:13.890 and I'm gonna make one modification because my goal now is to make a
new and improved
02:02:19.109 version of meowing that actually takes into account how
02:02:22.509 many times I want the cat to meow.
02:02:24.810 So instead of using a loop in my own program under when green flag
clicked,
02:02:30.080 I'm gonna detach this temporarily,
02:02:32.200 I'm gonna move this away.
02:02:33.959 I'm gonna move this code over here and I'm going to reattach it here.
02:02:38.640 So focusing for the moment on just the left mia
02:02:41.490 is now defined as repeating three times. The following two functions
play sound
02:02:46.580 and wait. But that's not quite right. I want to get rid of the three.
02:02:50.000 So what can I do because I created this input to the meow function
myself a moment ago,
02:02:56.109 I can actually drag a copy of it over, right?
02:02:59.169 That is change the three to be generally an N.
02:03:02.279 So now I have a function called meow that will meow any number of
times.
02:03:06.490 And what's nice now is my actual program that is governed by that
green flag.
02:03:11.634 I can type in three, I can type in 10, I can type in 100
02:03:15.384 it will just work.
02:03:16.615 And henceforth, I can again, dramatically sort of scroll this down.
02:03:20.265 So we don't know or care about it anymore.
02:03:22.285 Now,
02:03:22.685 my program is a single line whereby this notion of meowing has
02:03:26.274 been abstracted away by just defining my own function or custom block
02:03:32.509 questions then about just this idea,
02:03:34.779 this principle of creating your own functions to kind
02:03:37.569 of hide implementation details once you've solved the problem.
02:03:41.109 Therefore, you don't want to have to think about that same problem
ever again.
02:03:44.919 And that's the beauty of programming typically
02:03:48.250 questions on what here we just did.
02:03:53.979 Oh, all right. Well, let's do this.
02:03:55.520 Let's now make this a little more interactive in code. Let me go to
this green flag.
02:03:59.689 Let me scroll down and just throw all of this hard work away that we
02:04:02.490 have copies on the courses website of all of these programs step by
step.
02:04:05.529 If you want to review them in slower detail, let's do this under
control.
02:04:09.770 Turns out there's other ways to loop.
02:04:11.759 There's this forever block that will just do something for.
02:04:15.009 So in the forever block,
02:04:16.459 there's some place for some other code and I'm gonna move to
02:04:19.939 the control section here and grab one of these if block.
02:04:23.589 So one of these conditionals, let's plug that in here.
02:04:26.330 And now notice if and then there's this sort
02:04:28.939 of trapezoid like placeholder that's gonna probably fit what
02:04:33.879 the if is a conditional
02:04:35.660 forever is a loop,
02:04:37.689 say and so forth have been functions. What was the other key term we
used?
02:04:42.279 So a Boolean expression, we need to put one of those.
02:04:44.430 Yes, no or true false questions here. So what are those?
02:04:47.069 Well, I've been using scratch for some years.
02:04:49.069 So I know under sensing there's one of these shapes here touching
mouse pointer,
02:04:53.569 question mark,
02:04:54.430 the question mark literally evokes the whole
02:04:56.580 idea of a Boolean expression being yes,
02:04:58.250 it's way too big to fit, but it is the right shape. So let me drag it,
let go.
02:05:02.520 It's going to grow to fill and now let me go to sound, let me grab
that play,
02:05:07.120 sound meow until done and put it inside that conditional such
02:05:10.930 that what kind of program have I just implemented here?
02:05:13.379 Arguably.
02:05:15.560 What will this program do when I click the green flag?
02:05:18.660 Yeah.
02:05:20.669 Well,
02:05:21.470 nothing at the moment,
02:05:25.069 but I'm not touching the cat. So if I move the mouse pointer to the
cat
02:05:30.189 again,
02:05:32.040 again,
02:05:33.350 it's kind of like implementing the idea of petting a cat if you will
because
02:05:36.600 I'm forever just waiting and waiting and waiting
02:05:38.990 is the mouse pointer touching that Sprite,
02:05:42.189 touching that cat.
02:05:43.029 And only if so go ahead and play that sound
02:05:46.009 until meow
02:05:47.549 uh
02:05:48.189 play that sound meow until done.
02:05:50.470 But now we can make things a little more interesting.
02:05:52.049 Let me stop this and let me do something actually completely
different. Let me throw
02:05:56.330 all this hard work away. Let me go under extensions.
02:05:59.520 Let me go to video sensing because lots of
02:06:01.700 laptops my own included has a little webcam.
02:06:03.689 Nowadays,
02:06:04.450 let me a approve use of that there and you can see me in the frame and
let me do this.
02:06:09.080 Let me drag one of these when motion exceeds some measure and for the
trial and error,
02:06:13.779 I figured out that 50 tends to work.
02:06:15.399 Well, let me um step out of frame here and program off to the side.
02:06:19.569 And if I go to play sound meow until done,
02:06:22.819 notice that this is an alternative to using when green flag clicked.
02:06:28.000 This is a category of block that's constantly waiting for what we'll
call an event.
02:06:31.959 An event is just something that can happen on the screen.
02:06:34.009 A click, a drag, a mouse movement and so forth. So let me zoom out
here.
02:06:39.319 And now if I can do this, here we go.
02:06:43.950 No, too slow.
02:06:46.180 Still, too slow. Oh, wait, did I click play? Let's see.
02:06:51.740 Try again.
02:06:55.560 There we go. Ok. 50 is a little too high. Apparently. So
02:06:59.290 let's make this a little gentler 10.
02:07:02.109 Ok. Well,
02:07:03.919 there we go.
02:07:06.160 There we go.
02:07:07.120 OK. So we've implemented now more physically the idea of actually
responding
02:07:11.299 to a cat uh to petting a cat. So OK.
02:07:14.439 God
02:07:14.649 damn it.
02:07:15.709 OK.
02:07:18.319 All right. So this is a bug like now this is MIT call.
02:07:21.589 So it's not stopping in response to the red um stop sign.
02:07:25.109 So what do you do in doubt, most extremely reboot for now,
02:07:27.830 I'm just gonna close the window.
02:07:30.370 OK.
02:07:30.919 So now we've seen all of those primitives that we saw in that pseudo
code,
02:07:35.609 but incarnated in this graphical programming language.
02:07:38.520 And again,
02:07:38.819 without parentheses and semicolons and double coats and all
02:07:41.009 that punctuation that we will introduce before long.
02:07:43.220 But for now,
02:07:44.240 we have the mechanisms in place where we can do some really
interesting things.
02:07:48.560 So, in fact, I thought in the spirit of sort of uh um
02:07:52.310 sort of uh thinking back on olden times,
02:07:54.919 thought I'd open up the very first program I wrote when I actually
took,
02:07:58.379 I was cross registered in an mit class and took
02:08:01.100 a class that introduced uh but aspiring teachers to scratch.
02:08:04.709 And I implemented this program here called Oscar Time,
02:08:07.259 which was a game that used a childhood song that I was a fan of.
02:08:10.509 And it allows you to sort of drag trash into a trash can.
02:08:14.529 But to bring this to life and perhaps in exchange for one stress ball
could
02:08:17.759 get one brave volunteer who wants to come up and control this here
keyboard.
02:08:21.049 I saw your hand first. Come on up. Come on up.
02:08:26.560 And you'll see. Thanks to the team.
02:08:27.819 We also have this amazing lamppost here being on Quincy street as we
are.
02:08:32.089 Do you want to introduce yourself to the group?
02:08:33.850 Hi. My name is Anna. I'm from Richmond, Virginia and I'm in Weld.
02:08:38.970 Nice. We
02:08:40.259 all right. Come on over.
02:08:41.689 So here and you'll have a chance to play the very
02:08:44.060 first game I wrote in scratch,
02:08:45.830 which admittedly is more complicated typically than we would expect
02:08:48.609 of a student doing this for the very first time.
02:08:50.200 As in problem said zero. But what I'm going to do is full screen this
year.
02:08:53.450 I'm going to click the green flag.
02:08:54.839 And what you'll see on the screen are these instructions drag as much
02:08:57.419 falling trash as you can to Oscar's trash can before his song ends.
02:09:00.720 And here we go.
02:09:05.569 Oh,
02:09:09.299 anything dirty or ditch
02:09:11.859 or dusty,
02:09:13.529 anything racked
02:09:14.990 or R
02:09:16.029 or rusty.
02:09:17.899 Yes, I love.
02:09:21.169 Oh,
02:09:21.850 there we go.
02:09:22.770 So as an it continues to play, let's kind of tease this apart a little
bit.
02:09:26.290 So one,
02:09:27.100 there's some costumes on the stage like that
02:09:29.009 lamp post is actually never going to move.
02:09:31.129 But there's a couple of sprites,
02:09:32.990 there's the trash can which seems to be a character unto itself.
02:09:36.270 There's this piece of trash that keeps coming back and back, that is a
Sprite.
02:09:39.830 There's now this sneaker which is another sprite.
02:09:42.609 And in fact notice that Oscar of course,
02:09:44.180 keeps popping up from his Sprite once in a while.
02:09:46.959 So Oscar seems to have multiple costumes.
02:09:49.410 So I offer this as an example as you keep playing if you would. Very
good job. So far,
02:09:53.303 the song goes on forever.
02:09:54.422 This was a nightmare to implement, to listen to this all day long. But
02:09:57.442 how do, how do we implement the rest of this?
02:10:00.053 We'll notice that the trash every time she throws
02:10:02.243 it into the trash can does reappear somewhere different.
02:10:05.562 So there's some kind of randomness involved and indeed scratch
02:10:08.533 will let you pick random numbers in a range.
02:10:11.342 So maybe it could be negative 240 maybe it could be positive
02:10:14.272 240 at the 180 point on the top of the screen.
02:10:17.433 So you can sort of randomly put things on the screen.
02:10:19.873 There's apparently what kind of construct that
02:10:22.136 makes the trash fall again and again,
02:10:24.235 I think no one's listening to me, they're all just watching you.
02:10:26.175 But
02:10:28.116 what's making the trash fall from top to bottom?
02:10:31.875 So it's actually some kind of loop because there's
02:10:34.065 a motion block inside of a forever loop.
02:10:36.395 Probably that just keeps moving the trash one pixel, one pixel, one
pixel,
02:10:39.996 one pixel, one pixel, creating the illusion therefore of motion.
02:10:43.445 And if we can cramp the uh crank the song a little bit more,
02:10:46.636 you'll see that this is all synchronized.
02:10:48.476 Now,
02:10:51.790 June, if we can crank the music up
02:10:55.979 anything dirty or stingy or
02:10:58.910 dusty,
02:11:01.209 the song keeps going forever seemingly.
02:11:04.319 And now notice more and more sprites are appearing because they waited
for.
02:11:08.450 Here we go. Climax.
02:11:16.779 Alright. A big round of applause for Hannah.
02:11:19.549 Nicely done.
02:11:20.890 Here we go, here we go, here we go.
02:11:23.069 All right.
02:11:23.589 So this is an interminable song and indeed,
02:11:26.160 I spent like hours building that and just listening to that
02:11:28.589 song on loop was not the best way to program,
02:11:30.779 but
02:11:31.410 the goal here is to really use it as just
02:11:33.680 an intellectual exercise as to how that was implemented.
02:11:36.509 And we won't do the entire thing in detail because I
02:11:38.950 will say back in the day when I was younger,
02:11:41.029 I didn't necessarily write the cleanest code.
02:11:42.910 And in fact, if we see inside this
02:11:45.049 and we poke around the bottom of
02:11:47.040 screen here, you can see all of my different sprites
02:11:49.470 and the code is kind of complex like things just kind of escalated
quickly,
02:11:53.310 but I did not set out and write all of these programs all at once for
each sprite.
02:11:57.439 I pretty much took baby steps so to speak.
02:11:59.750 And so for instance,
02:12:00.870 let me open up just a few sample building blocks
02:12:03.950 here that speak to this that are written in advance.
02:12:06.470 So here's version zero, computer scientists typically start counting
at zero.
02:12:10.229 And let me show you this example here that only has two sprites on the
screen.
02:12:14.250 We have Oscar the trash can
02:12:16.060 and we have the piece of trash and now notice what does Oscar do.
02:12:20.390 Well, let me go ahead and zoom in on this script as it's called.
02:12:24.660 The program is a script when the green flag is clicked,
02:12:27.549 Oscar switches his costume to Oscar one.
02:12:29.930 That's his default costume where the lid is closed, then Oscar does
this forever. If
02:12:35.390 it's Oscar is touching the mouse pointer, change the costume to Oscar
two,
02:12:40.109 otherwise change it back to Oscar one.
02:12:42.669 So that whole idea of animation where Oscar is popping in
02:12:45.399 and out is just like a quick costume change based on
02:12:48.379 a loop inside of which is a conditional waiting for the
02:12:51.540 cursor like Anna did to get near the trash can.
02:12:54.810 Meanwhile, if we look at the piece of trash here, notice that the
trash
02:12:59.640 is actually not doing anything in this first
02:13:01.819 version because I didn't even implement falling first.
02:13:04.450 So let me hit the green flag.
02:13:05.830 Nothing is happening in this very first version.
02:13:08.250 But notice if I click on the trash and drag. As soon as I'm touching
Oscar,
02:13:12.620 there comes that trash can lid and it was just the
02:13:16.180 result of making this one program respond to that input.
02:13:19.850 All right, what did I do next?
02:13:21.410 Well, next, after taking that single baby step, I added one other
feature.
02:13:24.830 Let's see inside this version. One again,
02:13:27.502 Oscar is behaving the exact same way.
02:13:29.502 But notice this time the trash is designed to do the following.
02:13:33.362 First, I'm telling the program that the drag mode is draggable.
02:13:36.853 That is I want the trash to be movable when the user clicks on it.
02:13:40.263 Then I tell the piece of trash to go to a random X location X is the
horizontal.
02:13:45.223 So it's going somewhere between zero and 240.
02:13:48.362 But all the way at the top of the screen 180 then forever,
02:13:52.442 the piece of trash just changes by negative one.
02:13:54.783 So it just moves down and down and down.
02:13:57.136 And without looking at the second script, yet,
02:13:58.746 let me just hit play and notice without even doing anything.
02:14:02.666 And eventually,
02:14:03.206 once there was lots of trash falling like Anna was struggling to keep
up with this.
02:14:06.255 It's just moving one pixel at a time forever until thankfully
02:14:10.246 mit does stop things automatically if they hit the bottom.
02:14:13.395 Lest a six year old get upset that all of a sudden their sprite is
gone forever.
02:14:17.036 So there is some special casing there. But what else is this trash
doing?
02:14:21.695 Let me zoom in here,
02:14:23.419 the piece of trash.
02:14:25.109 Also when the green flag is clicked is forever asking this question.
02:14:29.169 If you are touching Oscar,
02:14:31.509 then pick a new random location between zero and 240 at positive 180
02:14:36.810 go back to the top.
02:14:38.089 So in other words,
02:14:38.870 as soon as this piece of trash is dragged over to Oscar like this and
I let, so
02:14:43.680 it recreates itself at the top.
02:14:45.669 It's just sort of teleporting to the top and thus was born this
02:14:49.270 feature and I won't slog through all of the individual's features
here.
02:14:52.370 But if we do just one more and see inside this one, notice what
happens.
02:14:56.060 Now when I click the green flag, drag this piece of trash in and let
go notice now that
02:15:03.759 what is different about this one?
02:15:06.069 Oh Sorry, wrong one. Let me go ahead and open up this one, see inside.
02:15:09.629 And if I click the green flag here, drag the piece of trash over to
Oscar and let go.
02:15:14.839 What's different this time? All right. Did you go to that?
02:15:17.870 Oh, ok. Sorry. I didn't want to show you that one either
02:15:20.529 wrong. One third time's a charm.
02:15:23.220 Yes.
02:15:23.839 So now let me go ahead and hit play notice at the top left of the
screen,
02:15:27.419 there's a score currently zero.
02:15:29.509 But now when I click the trash and let go notice that the
02:15:32.729 score is being incremented by one and this in fact is how,
02:15:35.970 and
02:15:36.180 your score kept going higher and higher and higher.
02:15:38.234 Every time I noticed, oh, the trash is touching Oscar.
02:15:40.884 Let's not only teleport, let's also increment a variable.
02:15:43.975 And we didn't see this before. But if I go to this uh Oscars
02:15:47.515 scratch now you'll see that it is exactly the same.
02:15:50.785 But if I now go to the trash piece here
02:15:53.589 and we go to when green flag clicked,
02:15:57.160 you'll see that I'm initializing a variable in orange called score to
zero.
02:16:01.470 But if we scroll down to the bottom,
02:16:03.859 Oscar is also doing another thing in parallel at
02:16:06.549 the same time when the green flag is clicked,
02:16:08.799 Oscar is forever checking is the piece of trash
02:16:13.390 tusing Oscar. If so change the score by one
02:16:17.020 and then go to top, which is another location on there that screen.
02:16:20.979 So in other words, even though at a glance,
02:16:22.700 something like Oscar time might look very complicated and it did take
me hours.
02:16:26.520 The goal especially with problems at zero is not going
02:16:28.870 to be to bite off all of that at once.
02:16:30.850 But to take proverbial baby steps,
02:16:32.759 implement one tiny feature so that you feel like you're making
progress at
02:16:36.754 other feature or another.
02:16:37.905 And invariably you might run out of time and
02:16:39.915 not get to the best version of your vision,
02:16:42.044 but hopefully it'll be good.
02:16:43.224 Hopefully it will be better but you'll have
02:16:44.955 these sort of mental milestones hoping that you at
02:16:47.025 least get to that point because as you will
02:16:48.763 soon discover everything in the world of programming,
02:16:51.615 uh Unfortunately, it takes longer than you might expect.
02:16:54.665 That was true for me 25 years ago.
02:16:56.745 And it's still true today.
02:16:59.067 Well, let me introduce one final set of examples here.
02:17:01.909 This one written by one of your own predecessors, a former student.
02:17:05.298 Let me go ahead and open up three baby steps if you
02:17:08.838 will toward an end of implementing a game called Ivy's hardest game,
02:17:13.058 whereby it's now more interactive, quite like Oscar time.
02:17:16.699 So at top right here, notice and I'll zoom in,
02:17:19.418 we have this world that's initially very simple.
02:17:21.798 Two black lines, two walls if you will and a Harvard Sprite in the
middle.
02:17:25.999 But when you click the green flag,
02:17:28.044 notice that nothing happens initially except that the Sprite jumps to
the middle,
02:17:31.884 but I can hit the up key
02:17:33.674 or the down key
02:17:35.075 or the left key or the right key.
02:17:37.433 But if I try to go too far, even though it's not the edge of the
world,
02:17:41.394 it's only touching that their black line, it's still going to stop as
well.
02:17:45.554 So intuitively, how could you implement that type of program?
02:17:48.834 How could you get a Sprite from what we've seen to respond to up down
left, right?
02:17:54.394 But actually move when I touch my arrow keys?
02:17:58.440 Like what does it mean to move. Yeah,
02:18:03.620 every
02:18:04.080 case
02:18:04.250 just a line or every
02:18:06.169 I
02:18:06.450 know
02:18:06.780 what I
02:18:07.209 know what
02:18:07.589 you mean by one.
02:18:08.990 OK.
02:18:10.397 Exactly.
02:18:10.968 So much like with representing information at the end of the day,
02:18:13.579 all we've got is zeros and ones when it comes to algorithms at the
moment,
02:18:17.109 all we have are functions and loops and conditionals
02:18:20.539 and boolean expressions and soon some more things too,
02:18:23.147 but there's not all that much we have at our disposal.
02:18:25.668 So let me zoom out from this and let me
02:18:27.598 actually show you what the Harvard Sprite is doing.
02:18:30.428 It's doing this.
02:18:31.588 When I go up to the green flag here, the Harvard Sprite is going to
zero, comma zero.
02:18:35.609 So dead center in the middle and then it's forever do
02:18:38.040 two things listening for the keyboard and feeling for walls left and
02:18:42.141 right now those are not puzzle pieces that come with scratch.
02:18:44.709 I created my own custom blocks, my own functions to implement those
ideas.
02:18:49.921 Let's not abstract away for now,
02:18:51.501 let's actually look at these features and indeed to your uh to your um
02:18:56.742 to your instincts at left here. What does it mean to listen for the
keyboard?
02:19:00.311 Well, if the up arrow key is pressed, change, why by one move up?
02:19:05.683 If the down arrow key is pressed, change, Y by negative one,
02:19:09.963 if the right arrow key is pressed, change X by one.
02:19:13.053 If the left arrow key is
02:19:14.673 pressed,
02:19:15.234 change X by negative one So take sort of all the magic out of moving
up down left,
02:19:19.204 right by just quantize it as plus minus this and that
02:19:22.683 it's all numbers indeed at the end of the day.
02:19:24.893 But what else is it doing? Notice that it did indeed bounce off the
wall.
02:19:28.772 So my other custom function which I chose feel for walls to kind of
evoke this idea.
02:19:33.325 It's asking two questions if you're touching the left wall,
02:19:37.106 then change X by one. So bounce in the other direction. Else, if
you're
02:19:40.915 touching the right wall, bounce in the negative one direction.
02:19:44.263 And so what are left wall and right wall? I mean, I kind of cheated.
02:19:48.366 I just used two more sprites.
02:19:49.755 These sprites are literally nothing except black lines but because
they exist,
02:19:54.555 I can ask that question in my conditional saying,
02:19:57.236 are you touching those other sprites?
02:19:59.646 And I could have colored them anyway,
02:20:00.967 I want.
02:20:01.488 But this is enough if I zoom in to implement this idea of going
02:20:04.917 up down left and right and preventing the sprite from leaving that
little world.
02:20:10.818 All right.
02:20:11.358 So if you'll agree that there's a way now to implement motion up down
left, right.
02:20:15.967 Let's go ahead and implement this idea by adding
02:20:18.198 a rival into the mix like a Yale Sprite.
02:20:20.597 And what the Yale Sprite is going to do if I click the green flag is
this?
02:20:24.568 So Harvard at the moment is still going to be movable with the arrow
keys up down left,
02:20:28.087 right.
02:20:28.780 But Yale,
02:20:29.389 for better or for worse is just going to mindlessly
02:20:31.200 bounce back and forth from left to right forever.
02:20:33.730 It would seem the operative word being forever. So, how is that
working?
02:20:36.709 Well, let's look, here's the Yale Sprite at the bottom.
02:20:39.629 Let's zoom in on its actual code here.
02:20:42.360 The Yale Sprite starts at 00, it points in direction 90 degrees, which
means left,
02:20:47.179 right, essentially.
02:20:48.370 And then it forever does this.
02:20:50.009 If touching the left wall or touching the right wall, turn around 100
and 80 degrees.
02:20:55.344 So I don't want the Yale Sprite to just stop
02:20:57.625 by moving at one pixel to bounce off slightly.
02:21:00.224 I want it to wrap around and just keep going and going and going
forever
02:21:04.554 and that's it. Everything else is the same. So one final flourish.
02:21:07.815 Let's add a,
02:21:08.485 a more formidable adversary like mit here whereby if I zoom in and hit
play,
02:21:13.620 notice that if I move the Harvard Sprite
02:21:16.549 mit comes chasing me. Now. Now, how is this actually working?
02:21:20.389 Yale is just kind of doing its thing bouncing back and forth.
02:21:22.769 Now, mit is really latched on to me
02:21:25.290 and it's following me up down left, right?
02:21:27.679 So how is that logic now working well again,
02:21:29.849 it's probably doing something forever because
02:21:31.639 that's why it's continually doing it.
02:21:33.290 Let's click on mit. This too is pretty simple even though it's a
pretty fancy idea.
02:21:38.009 Initially, the MIT, Sprite goes to a random position. But thereafter,
if forever
02:21:43.200 points toward the Harvard logo outline,
02:21:45.089 which is just the long name that your predecessor
02:21:47.179 or former student gave the name for that Sprite.
02:21:49.929 And then it moves one step, one step, one step.
02:21:53.070 So suppose this were an actual game and in games like things get
harder and harder,
02:21:56.650 the adversary moves faster and faster, how could we make MIT even
faster
02:22:01.259 by changing just one thing here?
02:22:05.790 Like how do we level up?
02:22:08.259 Change the one to like two pixels at a time? Two steps at a time? So
let's see that.
02:22:12.610 Let's go ahead and zoom out, let's hit play.
02:22:14.639 And now notice that MIT is coming in much faster this time.
02:22:18.419 All right. So it wasn't noticeably faster. Let's do this.
02:22:21.750 Let's move 10 steps at a time. So 10 steps faster than originally.
02:22:25.429 I mean, now and now notice it's kind of twitching back and forth in
this way. Why?
02:22:30.209 Well, probably if we worked out the math, probably the MIT.
02:22:32.889 Sprite is touching the Sprite and it's bouncing off of it.
02:22:35.599 But then it's realizing, oh, I went too far. Let me move back.
02:22:38.099 Wait a minute, I'm still touching it. Let me move down.
02:22:40.089 So you can get into these perverse situations where there is actually
a bug,
02:22:43.000 be it logical or esthetical.
02:22:44.769 But in this case, we probably, we want to fix that.
02:22:46.705 So 10 is probably too fast for this to work particularly well.
02:22:50.035 But the final final flourish here really is to show you the actual
version of a game
02:22:54.655 that one of your predecessors, a past classmate actually implemented.
02:22:58.044 Before thereafter, we will adjourn for cake in the trans,
02:23:01.014 which is the CS 50 tradition. But can we get one more final volunteer
02:23:05.365 to come on up to play Ivy's hardest game.
02:23:08.974 I'm seeing your hand most enthusiastically there. Yeah. Come on down
very happily
02:23:18.200 in just a moment. We will indeed adjourn.
02:23:20.679 But the goal here now is going to be to navigate a maze.
02:23:23.650 That's a little more difficult than the last.
02:23:26.219 Let's have you first though, introduce yourselves to your classmates
in front.
02:23:30.000 Oh, hi y'all. I'm Eric. I'm from Philadelphia
02:23:33.410 and I'm also from Hollis Hall.
02:23:36.160 One person from all this. Nice. Ok. Welcome. All right.
02:23:39.719 So Eric, go ahead and take the keyboard here.
02:23:41.349 It too will be all about up, down left to right. As soon as you click
the green flag
02:23:45.799 and if we can crank the music,
02:23:49.690 you can't touch this.
02:23:51.139 So notice the black walls are a little more involved than last time.
02:23:55.400 But the goal is to get to the Sprite all the way, right?
02:23:57.599 And just touch it at which point you move to the next level.
02:24:01.049 But the next level of course, has Yale doing its thing back and forth,
02:24:05.969 but you've made it to level three,
02:24:08.200 but now there's two, Yale.
02:24:09.650 So another Sprite is in the mix that's randomly moving a little
different
02:24:14.360 in terms of direction.
02:24:16.940 Three Ls
02:24:22.570 next level mits in.
02:24:25.660 We can't touch this.
02:24:28.179 Nice.
02:24:31.740 No,
02:24:34.120 the walls are now gone.
02:24:37.990 Princeton's in the mix.
02:24:48.879 Nice.
02:24:51.509 Two Princetons
02:24:55.269 to.
02:24:56.360 Ok.
02:24:59.610 Oh,
02:25:00.709 ok. New life.
02:25:07.660 You know
02:25:08.250 what?
02:25:11.009 Ok. Another life.
02:25:12.849 Nice. Nice.
02:25:14.530 Oh,
02:25:21.160 nice. Second to last level three, Princetons.
02:25:26.370 Last level.
02:25:35.849 Congratulations.
02:25:38.610 Here we go.
02:25:39.459 All right.
02:25:40.349 This then was CS 50. Welcome aboard. Cake is now served.