All language subtitles for 2024_fall_lecture0-720p-en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish Download
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 00:00:00,000 --> 00:00:02,495 [MUSIC PLAYING] 1 00:00:02,495 --> 00:01:15,460 2 00:01:15,460 --> 00:01:17,230 DAVID MALAN: All right. 3 00:01:17,230 --> 00:01:21,130 This is CS50, Harvard University's Introduction 4 00:01:21,130 --> 00:01:23,590 to the Intellectual Enterprises of Computer Science 5 00:01:23,590 --> 00:01:25,310 and the Art of Programming. 6 00:01:25,310 --> 00:01:28,940 My name is David Malan, and I actually took this class myself some years ago, 7 00:01:28,940 --> 00:01:30,080 but I almost didn't. 8 00:01:30,080 --> 00:01:32,590 It was my first year in college and my roommates 9 00:01:32,590 --> 00:01:35,150 and I were living in Matthews Hall, for those familiar. 10 00:01:35,150 --> 00:01:36,195 [CHEERING] 11 00:01:36,195 --> 00:01:37,840 Nice, Matthews. 12 00:01:37,840 --> 00:01:41,080 Our claim to fame, actually, at the time was that our room was literally 13 00:01:41,080 --> 00:01:43,310 Matt Damon's just three years' prior. 14 00:01:43,310 --> 00:01:45,440 So onward from that. 15 00:01:45,440 --> 00:01:48,190 But my first year, I didn't really have the nerves 16 00:01:48,190 --> 00:01:51,410 to set foot in this classroom, let alone computer science. 17 00:01:51,410 --> 00:01:54,340 In fact, for me, computer science, and CS50 in particular, 18 00:01:54,340 --> 00:01:56,750 was very much this class to beware. 19 00:01:56,750 --> 00:01:59,090 Like, I was kind of comfortable with computers, 20 00:01:59,090 --> 00:02:02,660 but I certainly wasn't among those more comfortable with computers. 21 00:02:02,660 --> 00:02:04,550 And so I shied away my first year. 22 00:02:04,550 --> 00:02:07,310 Instead, I actually took a whole lot of classes in government. 23 00:02:07,310 --> 00:02:09,889 In fact, in high school, I was really enjoying history. 24 00:02:09,889 --> 00:02:11,890 I loved this constitutional law class that I 25 00:02:11,890 --> 00:02:13,520 took my senior year of high school. 26 00:02:13,520 --> 00:02:15,978 And so I figured, OK, if that's what I liked in high school 27 00:02:15,978 --> 00:02:18,110 and if that's where my comfort zone was, that's 28 00:02:18,110 --> 00:02:20,370 probably what I should be doing in college. 29 00:02:20,370 --> 00:02:23,480 And so I essentially declared as my concentration or major government 30 00:02:23,480 --> 00:02:25,680 for the first year, year and a half of school. 31 00:02:25,680 --> 00:02:31,010 But my sophomore year when I was living actually in Mather House instead. 32 00:02:31,010 --> 00:02:32,570 OK, no one from Mather. 33 00:02:32,570 --> 00:02:35,540 In Mather House instead, it was-- 34 00:02:35,540 --> 00:02:38,090 I sort of followed some friends, I think the first week 35 00:02:38,090 --> 00:02:41,790 of class in September of that year to a class called CS50. 36 00:02:41,790 --> 00:02:44,625 And honestly, once I stepped foot in that classroom, 37 00:02:44,625 --> 00:02:47,000 the professor at the time was a famous computer scientist 38 00:02:47,000 --> 00:02:49,980 by the name of Brian Kernighan, now at Princeton. 39 00:02:49,980 --> 00:02:51,090 Like, I was hooked. 40 00:02:51,090 --> 00:02:55,100 And literally would I go back thereafter on Friday evenings when, at the time, 41 00:02:55,100 --> 00:02:57,830 problem sets were released and sit down at 7:00, 42 00:02:57,830 --> 00:03:00,510 8:00 PM on Friday nights and dive into homework. 43 00:03:00,510 --> 00:03:03,180 Which isn't necessarily something we recommend, per se. 44 00:03:03,180 --> 00:03:07,920 But for me, it was like this sign that, wow, I've sort of found my calling. 45 00:03:07,920 --> 00:03:10,108 I found my classmates here on campus. 46 00:03:10,108 --> 00:03:12,150 And that's not going to be the case for everyone. 47 00:03:12,150 --> 00:03:15,470 Certainly, we have no expectations that after taking one computer science class 48 00:03:15,470 --> 00:03:17,760 that you will or might want to take others. 49 00:03:17,760 --> 00:03:20,840 But what's so empowering about computer science and CS50 50 00:03:20,840 --> 00:03:24,660 in particular is it's so applicable to the broader world, 51 00:03:24,660 --> 00:03:28,160 whether you're in the arts, humanities, social sciences, natural sciences, 52 00:03:28,160 --> 00:03:32,780 or beyond, it's just so applicable the concepts and the practical programming 53 00:03:32,780 --> 00:03:36,090 skills with which you will exit a class like this. 54 00:03:36,090 --> 00:03:39,830 Now it's going to be challenging along the way, and indeed, I put in my time 55 00:03:39,830 --> 00:03:42,210 back in the day, and even I did find it challenging. 56 00:03:42,210 --> 00:03:45,750 And here, for instance, is a photograph of a very famous MIT hack, 57 00:03:45,750 --> 00:03:48,410 so to speak, from down the road, whereby it communicates 58 00:03:48,410 --> 00:03:53,010 that getting an education from MIT is like trying to drink from a fire hose. 59 00:03:53,010 --> 00:03:56,600 Which is to say there's going to be so much information, like so much new stuff 60 00:03:56,600 --> 00:03:58,620 that certainly on any given day of the week, 61 00:03:58,620 --> 00:04:02,760 you're not going to be able to absorb all of it that first time around. 62 00:04:02,760 --> 00:04:06,530 But at the end of the day, it's through that challenge, putting the time in, 63 00:04:06,530 --> 00:04:10,862 that the returns are therefore just so much higher at the end of the course. 64 00:04:10,862 --> 00:04:14,070 And indeed, will you walk out of the course with a much better understanding, 65 00:04:14,070 --> 00:04:15,945 not only of computer science and programming, 66 00:04:15,945 --> 00:04:20,640 but ultimately how to teach yourself new technologies and beyond. 67 00:04:20,640 --> 00:04:23,250 For the next three plus months, will we have teaching fellows, 68 00:04:23,250 --> 00:04:26,180 teaching assistants, course assistants, and myself by your side 69 00:04:26,180 --> 00:04:27,990 guiding you through the course's material. 70 00:04:27,990 --> 00:04:32,210 But the goal by term's end is to take those and leave those training wheels 71 00:04:32,210 --> 00:04:36,170 off so that you're well-equipped to go teach yourself 72 00:04:36,170 --> 00:04:39,410 new ideas beyond the class itself. 73 00:04:39,410 --> 00:04:42,800 Take comfort, though, in knowing that most CS50 students have never 74 00:04:42,800 --> 00:04:45,530 taken a CS course before, and indeed, as per the syllabus, 75 00:04:45,530 --> 00:04:48,327 what ultimately matters in this course is not so much where 76 00:04:48,327 --> 00:04:50,660 you end up relative to your classmates but where you end 77 00:04:50,660 --> 00:04:53,210 up relative to yourself when you began. 78 00:04:53,210 --> 00:04:55,560 And when you began is thus today. 79 00:04:55,560 --> 00:04:58,280 And so consider just how comfortable or uncomfortable 80 00:04:58,280 --> 00:05:01,290 you are with computing, let alone computer science and programming, 81 00:05:01,290 --> 00:05:04,950 particularly if you've never explored either in a classroom before, 82 00:05:04,950 --> 00:05:08,540 and consider the delta just a few months from now that will really 83 00:05:08,540 --> 00:05:10,350 describe how far you have come. 84 00:05:10,350 --> 00:05:12,620 And that is all that matters, not how much 85 00:05:12,620 --> 00:05:17,270 the student to the left or the right, in front or behind you right now, knows. 86 00:05:17,270 --> 00:05:21,360 With that said, let me add some additional inspiration, if I may. 87 00:05:21,360 --> 00:05:27,130 Here's a photograph of my own very first homework assignment in CS50 from 1996, 88 00:05:27,130 --> 00:05:28,880 and I will draw your attention to the fact 89 00:05:28,880 --> 00:05:32,270 that even though this is a so-called hello world program that we'll 90 00:05:32,270 --> 00:05:37,290 play with ourselves next week, it is pretty much literally the shortest, 91 00:05:37,290 --> 00:05:40,980 easiest program you can write in a programming language called C. 92 00:05:40,980 --> 00:05:44,900 I still somehow got minus 2 on my very first homework 93 00:05:44,900 --> 00:05:48,090 assignment, which is to say, we're all going to make mistakes along the way. 94 00:05:48,090 --> 00:05:53,570 But the goal will be to learn and enjoy that process here on out. 95 00:05:53,570 --> 00:05:55,670 At the end of the day, too, like me, you'll 96 00:05:55,670 --> 00:05:58,250 exit with your own proudly held high took 97 00:05:58,250 --> 00:06:01,080 CS50 t-shirt as is our tradition too. 98 00:06:01,080 --> 00:06:04,530 With that said, there are so many other traditions within CS50, 99 00:06:04,530 --> 00:06:05,760 both on campus and off. 100 00:06:05,760 --> 00:06:10,440 And in particular, do we try in CS50 to provide not only the academic support 101 00:06:10,440 --> 00:06:12,690 structure that you might want going through the class, 102 00:06:12,690 --> 00:06:15,990 but also a collective shared community experience. 103 00:06:15,990 --> 00:06:18,830 Which is to say in just a few days we'll start off the term formally 104 00:06:18,830 --> 00:06:21,320 with CS50 Puzzle Day, which is not only an opportunity 105 00:06:21,320 --> 00:06:24,230 to get together with friends, with pizza and prizes 106 00:06:24,230 --> 00:06:27,980 and also logic puzzles of sorts, but really to send the message that computer 107 00:06:27,980 --> 00:06:30,570 science itself is not about programming, it's not about C, 108 00:06:30,570 --> 00:06:33,810 it's not about Python, it's not about programming languages per se, 109 00:06:33,810 --> 00:06:38,000 but about problem solving, ever more so collaboratively with other smart people 110 00:06:38,000 --> 00:06:40,620 by your side in this class or beyond. 111 00:06:40,620 --> 00:06:44,210 And indeed, are there, toward the end of the semester, reinforcements 112 00:06:44,210 --> 00:06:47,930 of the same by way of a little something that we call the CS50 Hackathon, which 113 00:06:47,930 --> 00:06:51,780 will be an opportunity overnight to dive into your own final projects, 114 00:06:51,780 --> 00:06:55,440 the capstone of this course, thereafter followed by the CS50 fair, 115 00:06:55,440 --> 00:06:58,020 which will be a campus wide exhibition for students, faculty, 116 00:06:58,020 --> 00:07:02,660 and staff across campus of your very own final projects, be it your very own web 117 00:07:02,660 --> 00:07:07,170 app or mobile app or anything else you decide to create by term's end. 118 00:07:07,170 --> 00:07:10,980 And indeed, the objective at the end of the day, 119 00:07:10,980 --> 00:07:13,190 truly with that final project in particular, 120 00:07:13,190 --> 00:07:16,480 is going to be to create for yourselves, for your classmates, 121 00:07:16,480 --> 00:07:20,590 for attendees, something that we didn't even teach you how to do. 122 00:07:20,590 --> 00:07:23,970 And indeed, that will signal ultimately that you're indeed on your way 123 00:07:23,970 --> 00:07:25,170 and ready. 124 00:07:25,170 --> 00:07:29,258 Toward that end, thought we would give you a sense of CS50's past 125 00:07:29,258 --> 00:07:31,050 by way of this short video, if we could dim 126 00:07:31,050 --> 00:07:37,591 the lights, that paints a picture of all that awaits here and beyond. 127 00:07:37,591 --> 00:07:40,076 [VIDEO PLAYBACK] 128 00:07:40,076 --> 00:07:43,058 [MUSIC PLAYING] 129 00:07:43,058 --> 00:08:33,752 130 00:08:33,752 --> 00:08:34,780 [END PLAYBACK] 131 00:08:34,780 --> 00:08:38,400 DAVID MALAN: All right, so welcome aboard to CS50 and computer science 132 00:08:38,400 --> 00:08:38,900 itself. 133 00:08:38,900 --> 00:08:40,280 So what is computer science? 134 00:08:40,280 --> 00:08:42,980 Well, put simply, it's the study of information. 135 00:08:42,980 --> 00:08:45,563 Like, how do you represent it and how do you process it. 136 00:08:45,563 --> 00:08:47,980 But more fundamentally, what we'll teach you in this class 137 00:08:47,980 --> 00:08:49,490 is computational thinking. 138 00:08:49,490 --> 00:08:52,780 That is to say, the application of ideas from computer science 139 00:08:52,780 --> 00:08:56,710 to problems of interest to us within the class and problems of interest to you 140 00:08:56,710 --> 00:08:57,830 after the class. 141 00:08:57,830 --> 00:09:00,730 And so at the end of the day, what computer science really 142 00:09:00,730 --> 00:09:04,940 is is about problem solving, ergo that sort of global applicability. 143 00:09:04,940 --> 00:09:07,850 And by problem solving, we mean something quite simple. 144 00:09:07,850 --> 00:09:10,550 In fact, we can distill it as follows with this mental image. 145 00:09:10,550 --> 00:09:12,080 This is problem solving. 146 00:09:12,080 --> 00:09:14,830 You've got some problem to solve, thus known as the input 147 00:09:14,830 --> 00:09:15,890 that you want to solve. 148 00:09:15,890 --> 00:09:17,740 And the goal, of course, to problem solving 149 00:09:17,740 --> 00:09:19,580 is to actually produce a solution. 150 00:09:19,580 --> 00:09:22,030 So the output in this model would be the solution. 151 00:09:22,030 --> 00:09:26,620 The interesting part ultimately is going to be in how you process that input 152 00:09:26,620 --> 00:09:30,320 and turn it into that output, ergo solving problems. 153 00:09:30,320 --> 00:09:33,680 But before we can do that, we all kind of have to agree how to represent 154 00:09:33,680 --> 00:09:35,810 sent these inputs and outputs, especially 155 00:09:35,810 --> 00:09:39,750 if we want to do it in a standardized global way using literally computers, 156 00:09:39,750 --> 00:09:42,560 be them laptops, desktops, phones, or most any other kind 157 00:09:42,560 --> 00:09:44,460 of electronic device nowadays. 158 00:09:44,460 --> 00:09:45,840 So how can we do that? 159 00:09:45,840 --> 00:09:48,690 Well, there's different ways to represent information in the world. 160 00:09:48,690 --> 00:09:51,680 For instance, if I were to take attendance old school style, maybe 161 00:09:51,680 --> 00:09:55,890 in a smaller room, I might do 1, 2, 3, 4, 5, 6, 7, 162 00:09:55,890 --> 00:09:58,800 and so forth and just count people on my human hands. 163 00:09:58,800 --> 00:10:02,330 That's actually known as unary notation, otherwise mathematically known 164 00:10:02,330 --> 00:10:05,600 as base one, because you're using your fingers literally 165 00:10:05,600 --> 00:10:07,440 in this model as digits. 166 00:10:07,440 --> 00:10:08,880 But a little quick question. 167 00:10:08,880 --> 00:10:12,680 How high can you count with one human hand? 168 00:10:12,680 --> 00:10:18,510 Five is incorrect if you use a different base system than one. 169 00:10:18,510 --> 00:10:22,380 So it's obviously correct if you're just using unary and just counting 1, 2, 3, 170 00:10:22,380 --> 00:10:23,280 4, 5. 171 00:10:23,280 --> 00:10:27,830 But I dare say I can come up with more patterns in my human hand 172 00:10:27,830 --> 00:10:31,490 alone that would enable me, without a second hand or a couple of feet, 173 00:10:31,490 --> 00:10:33,360 to count higher than five. 174 00:10:33,360 --> 00:10:35,520 In fact, maybe for those more comfortable, 175 00:10:35,520 --> 00:10:39,950 how high could you actually count on a single human hand, perhaps? 176 00:10:39,950 --> 00:10:44,130 So 31, believe it or not, is, in fact, the correct answer. 177 00:10:44,130 --> 00:10:44,840 But why? 178 00:10:44,840 --> 00:10:47,270 Well, here I initially started pretty naively. 179 00:10:47,270 --> 00:10:49,020 1, 2, 3, 4, 5. 180 00:10:49,020 --> 00:10:52,477 And I just combined all of my fingers and counted the collective total. 181 00:10:52,477 --> 00:10:54,560 But what if I'm a little more clever and take into 182 00:10:54,560 --> 00:10:56,520 account the pattern of fingers that go up. 183 00:10:56,520 --> 00:10:58,020 So maybe this is still zero. 184 00:10:58,020 --> 00:10:59,460 This is one. 185 00:10:59,460 --> 00:11:03,870 But now maybe we just agree universally that this is two. 186 00:11:03,870 --> 00:11:06,000 Even though it's just my single pointer finger. 187 00:11:06,000 --> 00:11:09,990 Maybe we all just agree that this is three with two fingers up. 188 00:11:09,990 --> 00:11:12,740 Maybe we agree that this is often offensive with just one 189 00:11:12,740 --> 00:11:15,630 middle finger up, but this would then be four. 190 00:11:15,630 --> 00:11:17,370 This could then be five. 191 00:11:17,370 --> 00:11:19,020 This could then be six. 192 00:11:19,020 --> 00:11:20,370 This could be seven. 193 00:11:20,370 --> 00:11:23,700 And if I keep permuting my fingers in this way-- allow me to spoil it-- 194 00:11:23,700 --> 00:11:26,670 this would be, in fact, 31. 195 00:11:26,670 --> 00:11:27,960 But again, why? 196 00:11:27,960 --> 00:11:31,190 But the difference here is that we're no longer using unary or base 197 00:11:31,190 --> 00:11:34,440 one as a mathematician would say, but rather base two. 198 00:11:34,440 --> 00:11:37,190 Because if we take into account not just the total number 199 00:11:37,190 --> 00:11:42,380 of fingers that I'm using, but whether each finger is down or up being 200 00:11:42,380 --> 00:11:44,580 therefore in two potential states. 201 00:11:44,580 --> 00:11:47,240 Down, up, A, B, black, white, however you 202 00:11:47,240 --> 00:11:50,100 want to distinguish those two states of the world, 203 00:11:50,100 --> 00:11:53,390 you're now operating what's called base two, and perhaps more familiarly, 204 00:11:53,390 --> 00:11:58,020 even if you're not a computer person per se, this is the so-called binary system. 205 00:11:58,020 --> 00:12:01,140 And odds are, even if you're not a computer science person at all, 206 00:12:01,140 --> 00:12:04,370 you probably generally know that computers only understand or speak 207 00:12:04,370 --> 00:12:07,310 what alphabet, so to speak? 208 00:12:07,310 --> 00:12:11,680 So ones and zeros, zeros and ones, otherwise known as the binary system. 209 00:12:11,680 --> 00:12:14,180 And in fact, there's a term of art here that's worth noting. 210 00:12:14,180 --> 00:12:18,540 When you're using zeros and ones, which, of course, are a total of two digits, 211 00:12:18,540 --> 00:12:21,930 you have binary digits, so to speak-- bi implying two, 212 00:12:21,930 --> 00:12:24,330 which means there's two possibilities, zero or one. 213 00:12:24,330 --> 00:12:27,990 If we actually get rid of some of these letters and then join these two phrases, 214 00:12:27,990 --> 00:12:30,365 here you have a technical term that is a bit. 215 00:12:30,365 --> 00:12:35,220 A bit is just a binary digit, which is to say it's a zero or one. 216 00:12:35,220 --> 00:12:38,090 And this is in contrast, of course, with the system 217 00:12:38,090 --> 00:12:40,340 you and I know as the decimal system. 218 00:12:40,340 --> 00:12:42,770 Dec implying 10, because in the real world 219 00:12:42,770 --> 00:12:46,530 you and I daily use zero through nine, which is 10 possibilities. 220 00:12:46,530 --> 00:12:50,090 Computers only use zero and one, that is to say two bits, 221 00:12:50,090 --> 00:12:52,170 to represent information instead. 222 00:12:52,170 --> 00:12:54,980 So how do we represent this information, especially when at the end 223 00:12:54,980 --> 00:12:59,250 of the day what we're using are indeed computers and electronic devices? 224 00:12:59,250 --> 00:13:04,130 Well, if I want to represent zero, I can actually think of this as analogous 225 00:13:04,130 --> 00:13:05,190 to the physical world. 226 00:13:05,190 --> 00:13:08,600 Maybe I have a light bulb that's off or on controlled 227 00:13:08,600 --> 00:13:10,800 by a switch that turns it off or on. 228 00:13:10,800 --> 00:13:15,110 So you can think of a binary digit that is a zero as really 229 00:13:15,110 --> 00:13:17,220 just being a light bulb that is off. 230 00:13:17,220 --> 00:13:22,350 By contrast, if you think of a one in the digital world as, of course, 231 00:13:22,350 --> 00:13:24,140 being the second of two possibilities, you 232 00:13:24,140 --> 00:13:27,870 can think of that in the human or analog world, the physical world, 233 00:13:27,870 --> 00:13:30,030 as being a light bulb that is on. 234 00:13:30,030 --> 00:13:33,210 And in fact, what's inside of your Mac, your PC, your Android phone, 235 00:13:33,210 --> 00:13:36,360 your iPhone are millions of tiny little light switches 236 00:13:36,360 --> 00:13:41,080 known as transistors that just can be turned on or off, on or off. 237 00:13:41,080 --> 00:13:44,710 And essentially, you can use those transistors to store information 238 00:13:44,710 --> 00:13:47,710 because if you want to store a zero, you turn one of those switches off. 239 00:13:47,710 --> 00:13:50,920 If you want to store a one, you turn one of those switches on. 240 00:13:50,920 --> 00:13:53,440 Of course, that sort of invites the question, well, 241 00:13:53,440 --> 00:13:56,370 how do we count higher than zero or one? 242 00:13:56,370 --> 00:13:59,490 Well, we would seem to need to use more than just 243 00:13:59,490 --> 00:14:02,080 maybe a single bit, a single light bulb. 244 00:14:02,080 --> 00:14:05,800 So if we wanted to count higher than, for instance, zero or one, 245 00:14:05,800 --> 00:14:07,900 why don't we go ahead and maybe do this? 246 00:14:07,900 --> 00:14:10,030 So just so I have some place to put these, 247 00:14:10,030 --> 00:14:12,390 let me borrow some of our actual physical light bulbs 248 00:14:12,390 --> 00:14:14,560 here from the stage. 249 00:14:14,560 --> 00:14:20,680 And let me propose that now, with three bits on the stage, three light switches, 250 00:14:20,680 --> 00:14:23,790 three transistors, whatever metaphor you're most comfortable with, this 251 00:14:23,790 --> 00:14:28,210 is how a computer would represent a zero, because all of them are off. 252 00:14:28,210 --> 00:14:30,240 So it's off, off, off. 253 00:14:30,240 --> 00:14:34,550 But if a computer wanted to count to one, we could do naively this. 254 00:14:34,550 --> 00:14:35,650 We could turn this on. 255 00:14:35,650 --> 00:14:39,320 If the computer wanted to turn represent two, we could do this. 256 00:14:39,320 --> 00:14:43,400 And if a computer wanted to represent three, we could do this. 257 00:14:43,400 --> 00:14:46,090 But I'm kind of painting myself into a corner 258 00:14:46,090 --> 00:14:49,720 and not using these light bulbs as cleverly as I could, 259 00:14:49,720 --> 00:14:52,250 because at the moment I've only counted as high as three. 260 00:14:52,250 --> 00:14:54,470 So if I want to count to four, to five, to six, 261 00:14:54,470 --> 00:14:56,330 I'm going to need more and more light bulbs. 262 00:14:56,330 --> 00:14:57,770 Can we be a little more clever? 263 00:14:57,770 --> 00:15:01,150 Well, again, someone else who's among those more comfortable, what's 264 00:15:01,150 --> 00:15:02,060 the spoiler here? 265 00:15:02,060 --> 00:15:06,070 How high using binary zeros and ones could I 266 00:15:06,070 --> 00:15:08,740 count with three light bulbs total? 267 00:15:08,740 --> 00:15:09,590 In back? 268 00:15:09,590 --> 00:15:10,600 Yeah. 269 00:15:10,600 --> 00:15:12,798 So seven here is the answer. 270 00:15:12,798 --> 00:15:14,590 And if that, too, you're sort of wondering, 271 00:15:14,590 --> 00:15:16,730 how are people figuring out 31 and 7? 272 00:15:16,730 --> 00:15:18,200 That's the goal at hand here. 273 00:15:18,200 --> 00:15:19,250 So let me do this. 274 00:15:19,250 --> 00:15:23,950 Let me turn all of these off again so that my three light bulbs or switches 275 00:15:23,950 --> 00:15:25,490 again represent zero. 276 00:15:25,490 --> 00:15:26,690 And the first one's easy. 277 00:15:26,690 --> 00:15:29,450 This is how a computer would represent the number one. 278 00:15:29,450 --> 00:15:32,630 It would be on, off, off. 279 00:15:32,630 --> 00:15:35,000 How, though, is a computer going to represent two? 280 00:15:35,000 --> 00:15:37,490 Well, just like my proposed finger example. 281 00:15:37,490 --> 00:15:38,270 Let's do this. 282 00:15:38,270 --> 00:15:41,470 Let's turn this one off and this one on. 283 00:15:41,470 --> 00:15:43,720 That is how a computer would represent two. 284 00:15:43,720 --> 00:15:46,670 By saying off, on, off. 285 00:15:46,670 --> 00:15:50,540 In other words, 010 would be the way to pronounce it digitally. 286 00:15:50,540 --> 00:15:52,460 What if I instead want to represent three? 287 00:15:52,460 --> 00:15:54,980 That's how on my finger I did this, with two fingers. 288 00:15:54,980 --> 00:15:56,840 Well, I'm going to turn this one on. 289 00:15:56,840 --> 00:15:58,010 This is three. 290 00:15:58,010 --> 00:16:00,980 Now, this will, for those less comfortable, be non-obvious. 291 00:16:00,980 --> 00:16:06,190 This now is how I would represent the number four. 292 00:16:06,190 --> 00:16:09,200 This is how I would represent five. 293 00:16:09,200 --> 00:16:12,860 This is how I would represent six. 294 00:16:12,860 --> 00:16:17,090 And this, as per the spoiler, is how I would represent seven. 295 00:16:17,090 --> 00:16:20,560 So perhaps very non-obvious what it was I just did 296 00:16:20,560 --> 00:16:22,400 or why I chose those patterns. 297 00:16:22,400 --> 00:16:26,570 But I dare say if you rewind in your mind's eye or literally later on video, 298 00:16:26,570 --> 00:16:31,910 you'll find that I actually did show you eight distinct patterns of light bulbs. 299 00:16:31,910 --> 00:16:33,440 The first one was off, off, off. 300 00:16:33,440 --> 00:16:35,150 The last one was, on, on, on. 301 00:16:35,150 --> 00:16:37,840 And there were another six total in between then. 302 00:16:37,840 --> 00:16:39,170 Well, wait, why seven? 303 00:16:39,170 --> 00:16:42,980 Well, if you start counting at zero and I claim there's eight possibilities, 304 00:16:42,980 --> 00:16:46,250 you can only count from zero to seven, as we will soon see. 305 00:16:46,250 --> 00:16:49,000 So how are these patterns coming about and what is it 306 00:16:49,000 --> 00:16:50,930 that our computers are actually doing? 307 00:16:50,930 --> 00:16:55,280 Well, it's actually doing something a little like this, quite like in decimal. 308 00:16:55,280 --> 00:16:57,070 So in the human world, you and I are very 309 00:16:57,070 --> 00:17:00,700 much in the habit of using base 10, zero through nine, a.k.a. 310 00:17:00,700 --> 00:17:01,490 Decimal. 311 00:17:01,490 --> 00:17:05,089 Well, how do we use it instinctively as humans? 312 00:17:05,089 --> 00:17:08,230 Well, what's this number obviously on the screen? 313 00:17:08,230 --> 00:17:09,490 123. 314 00:17:09,490 --> 00:17:11,369 But why is it 123? 315 00:17:11,369 --> 00:17:13,119 Like, for years you haven't really thought 316 00:17:13,119 --> 00:17:17,150 about why this pattern of symbols or digits on the screen, 317 00:17:17,150 --> 00:17:20,650 one, two, three, represents mathematically this number 318 00:17:20,650 --> 00:17:22,940 that you know obviously as 123. 319 00:17:22,940 --> 00:17:25,400 But if you rewind to grade school, odds are, 320 00:17:25,400 --> 00:17:28,820 like me, you were taught that the rightmost digit is in the ones column, 321 00:17:28,820 --> 00:17:32,840 this second digit is in the tens column, this digit is in the hundreds column, 322 00:17:32,840 --> 00:17:33,650 and so forth. 323 00:17:33,650 --> 00:17:36,590 So even though none of us have to do this math explicitly, 324 00:17:36,590 --> 00:17:39,910 what you're instantly doing is 100 times 1 plus 10 times 325 00:17:39,910 --> 00:17:44,410 2 plus 1 times 3, which gives you 100 plus 20 plus 3. 326 00:17:44,410 --> 00:17:50,590 Oh, that's why it is 123, because these digits in this order 327 00:17:50,590 --> 00:17:52,490 have that significance. 328 00:17:52,490 --> 00:17:54,740 The digits to the left have more weight, so to speak, 329 00:17:54,740 --> 00:17:56,570 than the digits to the right. 330 00:17:56,570 --> 00:17:58,610 So what can we take away from this? 331 00:17:58,610 --> 00:18:01,607 Well, let's generalize it first as just any three digit number. 332 00:18:01,607 --> 00:18:02,690 So number, number, number. 333 00:18:02,690 --> 00:18:05,530 The ones column, the tens column, the hundreds column. 334 00:18:05,530 --> 00:18:08,780 But there's some math going on there, and it's not particularly sophisticated. 335 00:18:08,780 --> 00:18:10,910 Those are actually powers of 10. 336 00:18:10,910 --> 00:18:16,610 So 10 to the 0, 10 to the 1, 10 to the 2, and there's your decimal system. 337 00:18:16,610 --> 00:18:20,020 Because the base in this value is a 10, that's 338 00:18:20,020 --> 00:18:23,870 because there's 10 possibilities for each of those placeholders, 339 00:18:23,870 --> 00:18:25,310 zero through nine. 340 00:18:25,310 --> 00:18:28,510 But in the binary world, in the world of computers where all they 341 00:18:28,510 --> 00:18:30,080 have are zeros and ones, why? 342 00:18:30,080 --> 00:18:32,450 Because all they have physically is transistors. 343 00:18:32,450 --> 00:18:35,180 Tiny, tiny, tiny light bulbs that can be off or on. 344 00:18:35,180 --> 00:18:37,860 If you only have two digits to play with, 345 00:18:37,860 --> 00:18:41,160 the 10 base should, of course, become a two base. 346 00:18:41,160 --> 00:18:45,570 And now if we do some math here, 2 to the 0, 2 to the 1, and 2 to the 2, 347 00:18:45,570 --> 00:18:49,440 you get the ones column, the twos column, the fours column. 348 00:18:49,440 --> 00:18:53,750 And if we keep going 8, 16, 32, 64, 128 and so forth, 349 00:18:53,750 --> 00:18:56,760 its powers of 2 instead of powers of 10. 350 00:18:56,760 --> 00:19:00,080 But this is to say computers represent information 351 00:19:00,080 --> 00:19:03,600 in exactly the same way you and I have since childhood, 352 00:19:03,600 --> 00:19:06,330 but they have fewer digits at their disposal, 353 00:19:06,330 --> 00:19:09,000 so these columns need to be weighted differently. 354 00:19:09,000 --> 00:19:12,900 So we can still count from zero all the way up toward infinity. 355 00:19:12,900 --> 00:19:14,400 So what does this mean? 356 00:19:14,400 --> 00:19:18,630 Well, here we have three bits on the screen, 000. 357 00:19:18,630 --> 00:19:22,680 If we were to convert this now mentally or on paper pencil to decimal, 358 00:19:22,680 --> 00:19:23,670 how do we do it? 359 00:19:23,670 --> 00:19:26,820 Well, 4 times 0 plus 2 times 0 plus 1 times 0. 360 00:19:26,820 --> 00:19:29,670 That gives us the mathematical number you and I know as zero. 361 00:19:29,670 --> 00:19:31,470 That was three light bulbs. 362 00:19:31,470 --> 00:19:33,140 Off, off, off. 363 00:19:33,140 --> 00:19:36,770 Well, what if we turn on one light bulb all the way on the right? 364 00:19:36,770 --> 00:19:42,790 What decimal number does this binary number, 001 represent? 365 00:19:42,790 --> 00:19:46,512 Just one, because it's 4 times 0, 2 times 0, 1 times 1. 366 00:19:46,512 --> 00:19:48,220 Here's where things got more interesting, 367 00:19:48,220 --> 00:19:51,970 even if non-obvious in light bulb form or even physical hand form. 368 00:19:51,970 --> 00:19:55,630 010 in binary is what in decimal? 369 00:19:55,630 --> 00:19:58,310 Two, because it's 2 times 1 and that's it. 370 00:19:58,310 --> 00:20:02,270 011 in binary is, of course now three. 371 00:20:02,270 --> 00:20:03,890 This is now four. 372 00:20:03,890 --> 00:20:05,210 This is now five. 373 00:20:05,210 --> 00:20:06,910 This is now six and seven. 374 00:20:06,910 --> 00:20:11,970 On, on, on or 111 is the highest we can count with these three bits. 375 00:20:11,970 --> 00:20:12,470 All right. 376 00:20:12,470 --> 00:20:16,120 So how might a computer intuitively count as high as eight? 377 00:20:16,120 --> 00:20:18,613 What do you need to do, presumably? 378 00:20:18,613 --> 00:20:20,030 You're going to need to add a bit. 379 00:20:20,030 --> 00:20:22,100 So you need another light bulb, another switch. 380 00:20:22,100 --> 00:20:24,650 You need more memory, so to speak, to use nomenclature 381 00:20:24,650 --> 00:20:26,150 with which you're probably familiar. 382 00:20:26,150 --> 00:20:28,670 So in fact, if we change all of those to zero 383 00:20:28,670 --> 00:20:31,460 but we give ourself one more bit for a total of four, 384 00:20:31,460 --> 00:20:33,710 that's got to be the eighth place because there's just 385 00:20:33,710 --> 00:20:34,890 another power of two. 386 00:20:34,890 --> 00:20:38,700 So 1000 is the decimal number eight. 387 00:20:38,700 --> 00:20:40,680 You don't say 1,000 in binary. 388 00:20:40,680 --> 00:20:42,990 You literally say 1000. 389 00:20:42,990 --> 00:20:45,063 But that is the number you and I know as eight. 390 00:20:45,063 --> 00:20:46,730 And you can keep going up and up and up. 391 00:20:46,730 --> 00:20:51,140 And how then computers with Excel or any kind of number crunching software 392 00:20:51,140 --> 00:20:54,180 count really high and keep track of really big numbers? 393 00:20:54,180 --> 00:20:58,040 The computer just throws more and more transistors at it, more and more 394 00:20:58,040 --> 00:21:02,680 bits to count higher and higher and higher than this. 395 00:21:02,680 --> 00:21:05,480 It turns out, though, one bit, three bits, even four bits 396 00:21:05,480 --> 00:21:08,660 aren't that useful in practice because literally you can count 397 00:21:08,660 --> 00:21:12,420 as high as seven or maybe 15 or 31. 398 00:21:12,420 --> 00:21:18,620 So more commonly, as is commonly known, is to use a byte of bits instead. 399 00:21:18,620 --> 00:21:21,270 How many bits is in a byte, for those familiar? 400 00:21:21,270 --> 00:21:22,080 So it's just eight. 401 00:21:22,080 --> 00:21:22,710 Why eight? 402 00:21:22,710 --> 00:21:26,760 It's just more useful than one or two or three or some other number. 403 00:21:26,760 --> 00:21:29,150 And as an aside, it happens to be a power of two, which 404 00:21:29,150 --> 00:21:31,350 is just useful electronically as well. 405 00:21:31,350 --> 00:21:33,570 So a byte then is just 8 bits. 406 00:21:33,570 --> 00:21:36,900 And here are those columns I rattled off off the top of my head. 407 00:21:36,900 --> 00:21:40,320 Here is how a computer would represent zero in decimal, 408 00:21:40,320 --> 00:21:43,510 but using eight binary digits or bits. 409 00:21:43,510 --> 00:21:44,460 Little trivia. 410 00:21:44,460 --> 00:21:46,710 And again, this is not what computer science is about, 411 00:21:46,710 --> 00:21:49,640 but it helps to know the lower bounds and the upper bounds 412 00:21:49,640 --> 00:21:51,030 of these kinds of values. 413 00:21:51,030 --> 00:21:57,760 How high can you count with 8 bits or 1 byte if this is zero? 414 00:21:57,760 --> 00:21:58,390 Yeah. 415 00:21:58,390 --> 00:22:00,560 So it's actually 255. 416 00:22:00,560 --> 00:22:02,830 So if I were to change all of these zeros to ones 417 00:22:02,830 --> 00:22:07,420 and then do some quick mental or calculator math, 128 plus 64 418 00:22:07,420 --> 00:22:13,390 plus 32, 16, 8, 4, 2, and 1 would actually give me 255 total. 419 00:22:13,390 --> 00:22:18,110 Plus 0, which gives me 256 total possibilities. 420 00:22:18,110 --> 00:22:21,100 So this is only to say-- this is not, again, the kind of math 421 00:22:21,100 --> 00:22:23,080 will frequently do, but you'll commonly see 422 00:22:23,080 --> 00:22:26,410 in the computer world and programming world powers of two, 423 00:22:26,410 --> 00:22:28,730 numbers like 255, 256. 424 00:22:28,730 --> 00:22:29,230 Why? 425 00:22:29,230 --> 00:22:34,150 Because these are the common units of measures that systems tend to use. 426 00:22:34,150 --> 00:22:38,410 So let me pause here and see, with respect to binary, zeros, 427 00:22:38,410 --> 00:22:46,408 and ones, transistors and the like, any questions or confusion we can clear up? 428 00:22:46,408 --> 00:22:47,450 Oh, really good question. 429 00:22:47,450 --> 00:22:54,310 Why are bits just on or off instead of maybe sort of 0%, 50%, 100% 430 00:22:54,310 --> 00:22:55,460 by playing with voltages? 431 00:22:55,460 --> 00:22:58,430 So the voltage inference of yours is actually correct. 432 00:22:58,430 --> 00:23:00,020 That's what computers typically do. 433 00:23:00,020 --> 00:23:07,480 Maybe they use 0-ish volts to represent 0, maybe 5-ish volts to represent 1. 434 00:23:07,480 --> 00:23:11,140 It turns out it's just really easy to do extremes in computers. 435 00:23:11,140 --> 00:23:14,200 If you start to split that range of voltage levels, 436 00:23:14,200 --> 00:23:16,280 for those who remember any of their electricity, 437 00:23:16,280 --> 00:23:18,410 it just gets harder and harder to be exact. 438 00:23:18,410 --> 00:23:20,390 And if you get things a little too murky, 439 00:23:20,390 --> 00:23:24,160 you might mistake a zero for a one or a two or a three. 440 00:23:24,160 --> 00:23:27,650 So it turns out it's just simpler to use the binary system. 441 00:23:27,650 --> 00:23:31,360 But there do exist computers known as ternary computers that actually 442 00:23:31,360 --> 00:23:35,570 use three values, zero, one, and two, which is somewhere, of course, 443 00:23:35,570 --> 00:23:36,757 between binary and decimal. 444 00:23:36,757 --> 00:23:38,090 But you can do different things. 445 00:23:38,090 --> 00:23:39,350 It's just simple on and off. 446 00:23:39,350 --> 00:23:42,600 In case in point, I don't want to really be dramatic and turn off my computer, 447 00:23:42,600 --> 00:23:46,340 but if I pulled out the power plug, that could be off, literally, a.k.a. 448 00:23:46,340 --> 00:23:46,950 zero. 449 00:23:46,950 --> 00:23:48,510 Plug it back in, that's a one. 450 00:23:48,510 --> 00:23:52,100 There's just a cleanliness and simplicity to that. 451 00:23:52,100 --> 00:23:57,680 Other questions or confusion that we can clear up? 452 00:23:57,680 --> 00:23:58,220 No? 453 00:23:58,220 --> 00:23:59,310 OK. 454 00:23:59,310 --> 00:24:04,620 So if you're in agreement for the moment that, OK, using just zeros and ones, 455 00:24:04,620 --> 00:24:08,120 we can represent any number we want from zero on up, let 456 00:24:08,120 --> 00:24:11,270 me propose that we do more useful things with our computers and our pockets 457 00:24:11,270 --> 00:24:14,210 and desktops and laps like represent letters, 458 00:24:14,210 --> 00:24:18,470 for the sake of Google Docs, Microsoft Word, or any kind of text 459 00:24:18,470 --> 00:24:20,130 that we might want to write. 460 00:24:20,130 --> 00:24:24,590 So knowing now that computers only contain or only use zeros and ones, 461 00:24:24,590 --> 00:24:28,080 and therefore only contain hardware like transistors, 462 00:24:28,080 --> 00:24:30,980 how could you represent something like a capital letter 463 00:24:30,980 --> 00:24:34,050 A in English inside of a computer? 464 00:24:34,050 --> 00:24:37,262 Which, of course, is not a number anymore. 465 00:24:37,262 --> 00:24:38,220 Like, what could we do? 466 00:24:38,220 --> 00:24:38,720 Yeah? 467 00:24:38,720 --> 00:24:41,095 AUDIENCE: We could use the alphabet and then use numbers. 468 00:24:41,095 --> 00:24:42,012 DAVID MALAN: OK, yeah. 469 00:24:42,012 --> 00:24:44,470 So we could take the alphabet A through Z in English 470 00:24:44,470 --> 00:24:46,670 and we could just assign each letter A number. 471 00:24:46,670 --> 00:24:48,860 And honestly, that is not only the correct answer, 472 00:24:48,860 --> 00:24:50,172 it's really the only answer. 473 00:24:50,172 --> 00:24:51,880 Because at the end of the day, if all you 474 00:24:51,880 --> 00:24:54,560 have are zeros and ones available to you, 475 00:24:54,560 --> 00:24:59,540 that is the entirety of the potential solution to this problem. 476 00:24:59,540 --> 00:25:02,830 So it turns out that, yes, capital letter A, some years ago, 477 00:25:02,830 --> 00:25:06,190 was decided by a bunch of people in a room shall be represented 478 00:25:06,190 --> 00:25:08,260 with this pattern of zeros and ones. 479 00:25:08,260 --> 00:25:11,920 01000001. 480 00:25:11,920 --> 00:25:14,740 And now, trained as you are to do a bit of quick binary 481 00:25:14,740 --> 00:25:19,090 math, what decimal number is used to represent apparently capital A? 482 00:25:19,090 --> 00:25:24,140 So 65, because that's 64 plus 1 plus 1 times 1 is 65. 483 00:25:24,140 --> 00:25:24,890 What is B? 484 00:25:24,890 --> 00:25:26,060 Turns out it's 66. 485 00:25:26,060 --> 00:25:26,570 What is C? 486 00:25:26,570 --> 00:25:27,310 67. 487 00:25:27,310 --> 00:25:29,390 So they kept things simple there on out. 488 00:25:29,390 --> 00:25:32,900 Might have been nice if A were zero or maybe a were one. 489 00:25:32,900 --> 00:25:34,940 But nope, we're stuck with 65 instead. 490 00:25:34,940 --> 00:25:37,820 But everything after that is very much predictable. 491 00:25:37,820 --> 00:25:39,880 And in fact, for lowercase letters, there's 492 00:25:39,880 --> 00:25:46,110 a whole other set of numbers such as lowercase A happens to be 97, 493 00:25:46,110 --> 00:25:48,840 lowercase B happens to be 98, and so forth. 494 00:25:48,840 --> 00:25:50,910 But again, this is like CS trivia. 495 00:25:50,910 --> 00:25:54,530 But what's important here is that there are indeed contiguous from 65 496 00:25:54,530 --> 00:25:56,910 to 66 to 67 on up. 497 00:25:56,910 --> 00:26:00,750 That's something we're going to be able to leverage beyond the letter A alone. 498 00:26:00,750 --> 00:26:01,762 What is this system? 499 00:26:01,762 --> 00:26:03,720 What is this mapping that you yourself propose? 500 00:26:03,720 --> 00:26:06,840 It's ASCII, the American Standard Code for Information Interchange. 501 00:26:06,840 --> 00:26:10,680 And indeed, it was a bunch of Americans years ago who came up with this system. 502 00:26:10,680 --> 00:26:17,360 Unfortunately, at the time, they only allocated 7 and eventually 8 bits total 503 00:26:17,360 --> 00:26:21,860 for representing letters, both uppercase and lowercase, numbers 504 00:26:21,860 --> 00:26:24,540 on the keyboard as well, punctuation symbols as well. 505 00:26:24,540 --> 00:26:29,100 And so per our conversation a moment ago, if the Americans in this room, 506 00:26:29,100 --> 00:26:34,130 so to speak, only used 8 bits total, how many different characters can we 507 00:26:34,130 --> 00:26:37,760 represent with a computer in this story? 508 00:26:37,760 --> 00:26:43,140 So only 255, technically 256 if we, again, start counting from zero. 509 00:26:43,140 --> 00:26:47,340 So that's not nearly enough to represent all human languages, 510 00:26:47,340 --> 00:26:50,940 but it is enough to represent at least English, among some others. 511 00:26:50,940 --> 00:26:54,000 So here, for instance, is a chart of the ASCII mapping. 512 00:26:54,000 --> 00:26:58,010 And sure enough, if we zoom in on this column here, 65 is capital A, 513 00:26:58,010 --> 00:27:03,890 66 is capital B, dot, dot, dot 72 is H, 73 is I, and so forth. 514 00:27:03,890 --> 00:27:08,010 So there is a standardized mapping for at least all of these English letters. 515 00:27:08,010 --> 00:27:11,870 Well, suppose you were to receive an email or a text message 516 00:27:11,870 --> 00:27:15,930 or like a Google Doc containing this pattern of zeros and ones. 517 00:27:15,930 --> 00:27:20,310 So 01001000 and so forth and so forth. 518 00:27:20,310 --> 00:27:22,100 So 3 bytes worth. 519 00:27:22,100 --> 00:27:24,060 Three sets of 8 bits. 520 00:27:24,060 --> 00:27:30,650 That is to say 3 bytes, each of which represents a single letter in ASCII. 521 00:27:30,650 --> 00:27:33,092 What message have you received? 522 00:27:33,092 --> 00:27:35,300 Well, I'll do the math this time so we don't have to. 523 00:27:35,300 --> 00:27:40,940 Suppose what you really received was decimal 72, 73, 33. 524 00:27:40,940 --> 00:27:44,000 What message did you just receive? 525 00:27:44,000 --> 00:27:46,100 If you recall the previous chart. 526 00:27:46,100 --> 00:27:47,910 Hi was in fact correct. 527 00:27:47,910 --> 00:27:48,410 Why? 528 00:27:48,410 --> 00:27:50,790 Because H is 72, I is 73. 529 00:27:50,790 --> 00:27:51,950 And wait a minute, 33. 530 00:27:51,950 --> 00:27:56,420 So here's H. Here's I. 33, if we highlight it instead, 531 00:27:56,420 --> 00:27:58,260 happens to be an exclamation point. 532 00:27:58,260 --> 00:28:01,980 So that is literally what is going on underneath the hood, so to speak, 533 00:28:01,980 --> 00:28:04,850 when you get a text message today that literally says in all caps 534 00:28:04,850 --> 00:28:06,810 and an exclamation point, HI! 535 00:28:06,810 --> 00:28:10,400 Your phone has received at least three bytes, each of which 536 00:28:10,400 --> 00:28:12,143 represents a letter of the alphabet. 537 00:28:12,143 --> 00:28:14,060 Your computer is quickly doing the mental math 538 00:28:14,060 --> 00:28:16,430 to figure out exactly what numbers those are and then 539 00:28:16,430 --> 00:28:20,970 looking up in the so-called ASCII chart in its memories, in some sense, 540 00:28:20,970 --> 00:28:24,630 what letter should you actually see on the screen there. 541 00:28:24,630 --> 00:28:28,910 And so if you were to then display that message, 542 00:28:28,910 --> 00:28:33,920 you would see it indeed in English as opposed to those numeric equivalents. 543 00:28:33,920 --> 00:28:37,170 How else might we use this then? 544 00:28:37,170 --> 00:28:38,540 Well, here again is that chart. 545 00:28:38,540 --> 00:28:40,880 And maybe just to vary things, maybe take a little pressure off of me 546 00:28:40,880 --> 00:28:43,100 here, why don't we try spelling something else? 547 00:28:43,100 --> 00:28:47,050 This time a different three letter word, but maybe eight volunteers. 548 00:28:47,050 --> 00:28:48,800 Could we get a bytes' worth of volunteers? 549 00:28:48,800 --> 00:28:51,840 And I can sweeten the deal with some stress balls in exchange. 550 00:28:51,840 --> 00:28:54,480 You just have to be comfortable coming up on stage and being on the internet. 551 00:28:54,480 --> 00:28:54,980 So yes. 552 00:28:54,980 --> 00:28:56,490 One, two. 553 00:28:56,490 --> 00:28:58,080 How about three, four. 554 00:28:58,080 --> 00:29:01,050 How about five, six, seven. 555 00:29:01,050 --> 00:29:02,250 And how about eight. 556 00:29:02,250 --> 00:29:03,120 Come on up. 557 00:29:03,120 --> 00:29:05,040 A round of applause for our volunteers. 558 00:29:05,040 --> 00:29:05,678 Yep. 559 00:29:05,678 --> 00:29:08,018 [APPLAUSE] 560 00:29:08,018 --> 00:29:11,770 561 00:29:11,770 --> 00:29:12,288 All right. 562 00:29:12,288 --> 00:29:14,080 So what I'm going to have each of you do is 563 00:29:14,080 --> 00:29:17,300 represent a bit in a particular order. 564 00:29:17,300 --> 00:29:19,360 So if you want to just, in any order, line 565 00:29:19,360 --> 00:29:21,380 yourselves up here facing the audience. 566 00:29:21,380 --> 00:29:23,860 Come on over. 567 00:29:23,860 --> 00:29:24,562 All right. 568 00:29:24,562 --> 00:29:27,520 And we will have you represent-- well, we got to see who ends up where. 569 00:29:27,520 --> 00:29:29,050 Scooch this way a little bit. 570 00:29:29,050 --> 00:29:30,660 This way, this way. 571 00:29:30,660 --> 00:29:31,160 All right. 572 00:29:31,160 --> 00:29:34,700 So you shall be the ones place and just hold that in front of you. 573 00:29:34,700 --> 00:29:36,460 Twos place. 574 00:29:36,460 --> 00:29:37,510 Threes. 575 00:29:37,510 --> 00:29:39,010 Fours place. 576 00:29:39,010 --> 00:29:40,720 Eights place. 577 00:29:40,720 --> 00:29:46,940 16, 32, 64, and 128. 578 00:29:46,940 --> 00:29:49,430 And just compress yourselves a little bit if you could. 579 00:29:49,430 --> 00:29:53,780 So each of these folks represents a bit in a particular place. 580 00:29:53,780 --> 00:29:54,650 And let's say this. 581 00:29:54,650 --> 00:29:57,770 If you're just standing there uncomfortably without any hand raise, 582 00:29:57,770 --> 00:30:00,380 we'll assume that you're representing a zero, quite simply. 583 00:30:00,380 --> 00:30:02,230 If your hand goes up, though, the audience 584 00:30:02,230 --> 00:30:04,280 should assume that you're representing a one. 585 00:30:04,280 --> 00:30:06,863 And therefore, what we'll do is spell out a three letter word, 586 00:30:06,863 --> 00:30:10,190 and on each round of this, you'll either stay, stay like this, 587 00:30:10,190 --> 00:30:11,730 or you'll raise your hand. 588 00:30:11,730 --> 00:30:14,330 But first, let's actually meet some of our volunteers 589 00:30:14,330 --> 00:30:18,180 here, starting with position number one, if you'd like to say your name, 590 00:30:18,180 --> 00:30:20,010 perhaps where you're from and/or studying. 591 00:30:20,010 --> 00:30:20,580 AUDIENCE: Hi. 592 00:30:20,580 --> 00:30:21,630 My name is Brooke. 593 00:30:21,630 --> 00:30:26,420 I'm from Indiana, and I'm studying biology and computer science. 594 00:30:26,420 --> 00:30:27,170 DAVID MALAN: Nice. 595 00:30:27,170 --> 00:30:28,190 Welcome. 596 00:30:28,190 --> 00:30:29,670 AUDIENCE: Hi, I'm Becca. 597 00:30:29,670 --> 00:30:34,695 I'm from, like, Maryland, DC area, and I'm studying electrical engineering. 598 00:30:34,695 --> 00:30:35,570 DAVID MALAN: Welcome. 599 00:30:35,570 --> 00:30:36,840 AUDIENCE: Hi, I'm Addison. 600 00:30:36,840 --> 00:30:37,920 I'm from Maryland. 601 00:30:37,920 --> 00:30:40,520 I'm studying engineering. 602 00:30:40,520 --> 00:30:41,130 AUDIENCE: Hi. 603 00:30:41,130 --> 00:30:41,790 I'm Sharon. 604 00:30:41,790 --> 00:30:44,265 I'm from Rwanda and I'm studying CS and math. 605 00:30:44,265 --> 00:30:45,140 DAVID MALAN: Welcome. 606 00:30:45,140 --> 00:30:46,470 AUDIENCE: Hi, I'm Grace. 607 00:30:46,470 --> 00:30:49,995 I'm from Alabama and I'm studying electrical engineering. 608 00:30:49,995 --> 00:30:50,870 DAVID MALAN: Welcome. 609 00:30:50,870 --> 00:30:51,930 AUDIENCE: Hi, I'm Angelina. 610 00:30:51,930 --> 00:30:52,680 I'm from Maryland. 611 00:30:52,680 --> 00:30:53,970 And also, I stay in Matthews. 612 00:30:53,970 --> 00:30:54,720 DAVID MALAN: Nice. 613 00:30:54,720 --> 00:30:55,730 Matthews. 614 00:30:55,730 --> 00:30:56,580 Nice. 615 00:30:56,580 --> 00:30:58,030 [APPLAUSE] 616 00:30:58,030 --> 00:31:00,530 AUDIENCE: And I'm studying applied math and econ, as well as 617 00:31:00,530 --> 00:31:02,610 environmental science and public policy. 618 00:31:02,610 --> 00:31:03,810 DAVID MALAN: Welcome. 619 00:31:03,810 --> 00:31:07,395 AUDIENCE: I'm Owen Bells and I'm from rural Virginia and I'm studying CS. 620 00:31:07,395 --> 00:31:08,520 DAVID MALAN: Nice, welcome. 621 00:31:08,520 --> 00:31:09,398 And? 622 00:31:09,398 --> 00:31:10,440 AUDIENCE: My name is Max. 623 00:31:10,440 --> 00:31:11,520 I'm from London. 624 00:31:11,520 --> 00:31:14,750 I'm also staying in Matthews and I'm studying computer science 625 00:31:14,750 --> 00:31:15,690 and neuroscience. 626 00:31:15,690 --> 00:31:16,190 Thank you. 627 00:31:16,190 --> 00:31:17,670 DAVID MALAN: Welcome aboard as well. 628 00:31:17,670 --> 00:31:19,010 If you're wondering, too, why I was wearing 629 00:31:19,010 --> 00:31:21,843 these glasses at the start-- so very common on the internet nowadays 630 00:31:21,843 --> 00:31:22,800 as these POV videos. 631 00:31:22,800 --> 00:31:25,070 So it turns out these Ray-Bans actually record video footage, 632 00:31:25,070 --> 00:31:27,350 and we have a couple of them, and we'd thought we would offer them 633 00:31:27,350 --> 00:31:28,433 to a couple of volunteers. 634 00:31:28,433 --> 00:31:32,150 If anyone wants to record their point of view for everyone here. 635 00:31:32,150 --> 00:31:34,830 And Vlad here is going to help make sure they're recording. 636 00:31:34,830 --> 00:31:35,790 Second volunteer. 637 00:31:35,790 --> 00:31:36,960 Yes, number two. 638 00:31:36,960 --> 00:31:37,740 All right. 639 00:31:37,740 --> 00:31:41,880 So as Vlad gets those set, on the backs of your pieces of paper 640 00:31:41,880 --> 00:31:44,280 you have instructions for the following three rounds. 641 00:31:44,280 --> 00:31:45,660 Each round represents a letter. 642 00:31:45,660 --> 00:31:47,480 The audience participation part of this is 643 00:31:47,480 --> 00:31:51,620 to actually do the mental math to figure out what number these volunteers are 644 00:31:51,620 --> 00:31:52,500 representing. 645 00:31:52,500 --> 00:31:58,070 So go ahead and execute round one, either keeping your hand down or raising 646 00:31:58,070 --> 00:32:01,810 it appropriately. 647 00:32:01,810 --> 00:32:02,350 OK. 648 00:32:02,350 --> 00:32:05,486 What number are our volunteers here representing? 649 00:32:05,486 --> 00:32:06,220 AUDIENCE: 66. 650 00:32:06,220 --> 00:32:09,280 DAVID MALAN: 66, because we have a 64 plus a 2. 651 00:32:09,280 --> 00:32:11,150 That then maps to what ASCII letter? 652 00:32:11,150 --> 00:32:11,650 AUDIENCE: B. 653 00:32:11,650 --> 00:32:13,290 DAVID MALAN: B was the first letter. 654 00:32:13,290 --> 00:32:14,240 OK, hands down. 655 00:32:14,240 --> 00:32:16,750 Round two, go. 656 00:32:16,750 --> 00:32:18,050 A little harder. 657 00:32:18,050 --> 00:32:21,734 What's now being represented? 658 00:32:21,734 --> 00:32:23,098 AUDIENCE: 79. 659 00:32:23,098 --> 00:32:24,640 DAVID MALAN: I'm starting to hear it. 660 00:32:24,640 --> 00:32:27,980 79 is in fact correct. 661 00:32:27,980 --> 00:32:32,360 79, because we have a 64 and an 8 and 4 and 2 and a 1. 662 00:32:32,360 --> 00:32:36,230 So if it's a 79, we have the ASCII letter O. 663 00:32:36,230 --> 00:32:39,160 So we've got BO, and then lastly, third round. 664 00:32:39,160 --> 00:32:39,660 Go. 665 00:32:39,660 --> 00:32:42,530 666 00:32:42,530 --> 00:32:47,360 We have 01010111. 667 00:32:47,360 --> 00:32:48,490 What number is this? 668 00:32:48,490 --> 00:32:49,453 AUDIENCE: 87. 669 00:32:49,453 --> 00:32:50,120 DAVID MALAN: 87. 670 00:32:50,120 --> 00:32:51,680 Which spells the letter? 671 00:32:51,680 --> 00:32:52,460 AUDIENCE: W. 672 00:32:52,460 --> 00:32:54,650 DAVID MALAN: W. Which spells the word? 673 00:32:54,650 --> 00:32:55,250 AUDIENCE: Bow. 674 00:32:55,250 --> 00:32:56,220 DAVID MALAN: Not bow. 675 00:32:56,220 --> 00:32:57,850 Take a bow if you could. 676 00:32:57,850 --> 00:32:58,350 All right. 677 00:32:58,350 --> 00:33:00,475 A round of applause for our volunteers here. 678 00:33:00,475 --> 00:33:01,610 [APPLAUSE] 679 00:33:01,610 --> 00:33:06,850 And come on off this way and help yourself to a CS50 stress ball. 680 00:33:06,850 --> 00:33:08,072 Thank you to our volunteers. 681 00:33:08,072 --> 00:33:11,030 So this is only to say we've now agreed on how we can represent numbers 682 00:33:11,030 --> 00:33:12,060 from zero on up. 683 00:33:12,060 --> 00:33:14,100 We've agreed on how we can represent letters. 684 00:33:14,100 --> 00:33:17,390 But at least letters using ASCII, and in fact, these 685 00:33:17,390 --> 00:33:18,660 are more than just decoration. 686 00:33:18,660 --> 00:33:21,570 In fact, it's a little bit of trivia by lecture's end. 687 00:33:21,570 --> 00:33:24,090 If you to come up for your very own CS50 stress ball, 688 00:33:24,090 --> 00:33:27,480 turns out there are 64 light bulbs at the foot of the stage here. 689 00:33:27,480 --> 00:33:31,400 If you break them down into 8 byte or single-- 690 00:33:31,400 --> 00:33:35,060 8 bit or single byte chunks, there's an eight letter word 691 00:33:35,060 --> 00:33:38,430 that happens to be spelled out today using this here ASCII chart. 692 00:33:38,430 --> 00:33:42,550 So today's mystery is what exactly is that there word. 693 00:33:42,550 --> 00:33:46,380 But of course, if you have only 8 bits, you can only represent, like, 694 00:33:46,380 --> 00:33:51,240 256 characters, which sounds like plenty for English, and indeed, it is. 695 00:33:51,240 --> 00:33:54,250 Zero through nine, A through B, capital and lowercase, 696 00:33:54,250 --> 00:33:56,320 uppercase and lowercase, as well as punctuation. 697 00:33:56,320 --> 00:33:58,770 But there's so many other human languages 698 00:33:58,770 --> 00:34:00,910 in the world that have other characters. 699 00:34:00,910 --> 00:34:03,420 For instance, we have not just the English alphabet 700 00:34:03,420 --> 00:34:05,620 we might see here on a US English keyboard. 701 00:34:05,620 --> 00:34:09,070 We have accented characters, we have various Asian languages 702 00:34:09,070 --> 00:34:10,330 have even many more glyphs. 703 00:34:10,330 --> 00:34:13,900 We need more than 256 possible characters. 704 00:34:13,900 --> 00:34:18,520 And so nowadays computers do not just use 7 or even 8 bits. 705 00:34:18,520 --> 00:34:22,150 They might use 8 bits for some letters, like all of the English letters. 706 00:34:22,150 --> 00:34:25,630 They might use 16 bits for certain other languages. 707 00:34:25,630 --> 00:34:28,330 Maybe even 24 or 32 bits. 708 00:34:28,330 --> 00:34:30,532 And fun fact, if you have 32 bits-- 709 00:34:30,532 --> 00:34:32,199 and we have more than that on the stage. 710 00:34:32,199 --> 00:34:35,040 If you've got 32 bits, you can actually represent 711 00:34:35,040 --> 00:34:40,260 as many as 4 billion possible characters, which is quite a bit. 712 00:34:40,260 --> 00:34:41,440 No pun intended. 713 00:34:41,440 --> 00:34:43,510 So what else can we represent? 714 00:34:43,510 --> 00:34:46,659 Well, the goal of this system, not just ASCII, 715 00:34:46,659 --> 00:34:49,449 but something known as Unicode, which is a newer standard, 716 00:34:49,449 --> 00:34:51,489 is to be backwards compatible with ASCII. 717 00:34:51,489 --> 00:34:56,500 So humans left all of those other numbers alone, 65, 66, 67 and so forth, 718 00:34:56,500 --> 00:34:59,880 but they added to it a super set of representations 719 00:34:59,880 --> 00:35:03,040 that maybe are 16, 24, or 32 bits. 720 00:35:03,040 --> 00:35:08,410 The goal being to be able digitally to represent all human languages, past, 721 00:35:08,410 --> 00:35:12,690 present, and future, and even through pictograms, things 722 00:35:12,690 --> 00:35:17,760 like smiley faces and the like, even people, places, things and emotions that 723 00:35:17,760 --> 00:35:19,750 transcend human language. 724 00:35:19,750 --> 00:35:23,220 And in fact, odds are within the past few minutes or hours, most of you 725 00:35:23,220 --> 00:35:27,810 have used one or more of these here emoji, these pictograms, which it turns 726 00:35:27,810 --> 00:35:30,790 out are just characters on a keyboard. 727 00:35:30,790 --> 00:35:34,000 You might have to hit a special button to pull up that form of the keyboard, 728 00:35:34,000 --> 00:35:36,310 but these are just here characters. 729 00:35:36,310 --> 00:35:38,828 And so these emoji have exploded in popularity 730 00:35:38,828 --> 00:35:41,370 for a number of reasons, one of which is, my God, what are we 731 00:35:41,370 --> 00:35:44,800 going to do with 4 billion possible patterns of zeros and ones? 732 00:35:44,800 --> 00:35:47,340 We can start to have some fun with it and represent things 733 00:35:47,340 --> 00:35:50,530 beyond English and human languages alone. 734 00:35:50,530 --> 00:35:53,590 Now, as an aside, when it comes to Unicode, 735 00:35:53,590 --> 00:35:58,800 it turns out Unicode, years ago, standardized this pattern of 32 zeros 736 00:35:58,800 --> 00:36:02,110 and ones to represent just one of those emoji. 737 00:36:02,110 --> 00:36:04,650 So emoji tend to use even more bits here. 738 00:36:04,650 --> 00:36:08,070 Anyone know what decimal number this is? 739 00:36:08,070 --> 00:36:09,820 This is not a fun mathematical exercise. 740 00:36:09,820 --> 00:36:17,610 The spoiler is 4,036,991,106 is the decimal number that actually represents, 741 00:36:17,610 --> 00:36:21,370 as of present, the most popular emoji in the world. 742 00:36:21,370 --> 00:36:27,196 Does anyone want to hazard a guess what emoji this here number represents? 743 00:36:27,196 --> 00:36:27,863 AUDIENCE: Heart. 744 00:36:27,863 --> 00:36:28,654 DAVID MALAN: Heart? 745 00:36:28,654 --> 00:36:29,230 Hearts? 746 00:36:29,230 --> 00:36:33,800 No, but it's actually this so-called face with tears of joy. 747 00:36:33,800 --> 00:36:36,550 So perhaps think about the frequency with which you send that one. 748 00:36:36,550 --> 00:36:39,790 And even though it's obviously a picture on the screen, sure, 749 00:36:39,790 --> 00:36:43,120 it actually is more like a font, because underneath the hood, 750 00:36:43,120 --> 00:36:45,900 it's indeed just a pattern of zeros and ones or a decimal number 751 00:36:45,900 --> 00:36:47,260 that the computer is storing. 752 00:36:47,260 --> 00:36:51,790 But the computer, be it Mac OS or Windows or iOS or Android, 753 00:36:51,790 --> 00:36:54,793 know to display that pattern as this here picture. 754 00:36:54,793 --> 00:36:57,460 But the pictures might look different depending on the hardware. 755 00:36:57,460 --> 00:36:57,960 Why? 756 00:36:57,960 --> 00:37:01,260 Because there's companies like Google and Microsoft and Meta and others 757 00:37:01,260 --> 00:37:04,780 that have their own artists on staff as employees, 758 00:37:04,780 --> 00:37:07,320 and those artists interpret the descriptions 759 00:37:07,320 --> 00:37:09,700 like face with tears of joy differently. 760 00:37:09,700 --> 00:37:13,350 So those of you with an Android phone actually see face with tears of joy 761 00:37:13,350 --> 00:37:14,953 looking a little something like this. 762 00:37:14,953 --> 00:37:17,620 And if you have Telegram, for instance, installed on your phone, 763 00:37:17,620 --> 00:37:19,510 it's even more animated than that. 764 00:37:19,510 --> 00:37:23,860 It's this here emoji using the same pattern of zeros and ones. 765 00:37:23,860 --> 00:37:27,400 So different artists render these here emoji in different ways, 766 00:37:27,400 --> 00:37:29,770 but all they are here are patterns. 767 00:37:29,770 --> 00:37:34,030 Now, for all of the other answers, save one that was shouted out a moment ago, 768 00:37:34,030 --> 00:37:38,790 this is a sort of cloud diagram of the most popular emoji as 769 00:37:38,790 --> 00:37:42,400 of a couple of years ago per Unicode, whereby the size of the emoji 770 00:37:42,400 --> 00:37:44,450 indicates its relative popularity. 771 00:37:44,450 --> 00:37:46,720 So heart, I did here over here, is indeed 772 00:37:46,720 --> 00:37:49,260 one of the most popular ones as well. 773 00:37:49,260 --> 00:37:50,531 Question? 774 00:37:50,531 --> 00:37:53,240 AUDIENCE: Why do certain emojis show up [INAUDIBLE]? 775 00:37:53,240 --> 00:37:54,823 DAVID MALAN: Oh, really good question. 776 00:37:54,823 --> 00:37:57,740 Why do certain emoji not show up on one device or another? 777 00:37:57,740 --> 00:37:59,970 It depends on how recent the software is. 778 00:37:59,970 --> 00:38:03,350 Pretty much every year the humans behind the Unicode consortium 779 00:38:03,350 --> 00:38:04,650 release new emoji. 780 00:38:04,650 --> 00:38:07,400 Which is to say they decide that this other pattern will 781 00:38:07,400 --> 00:38:10,800 represent this new emoji, this other pattern will represent this new emoji. 782 00:38:10,800 --> 00:38:13,200 And unless you update your phone, your laptop, 783 00:38:13,200 --> 00:38:17,060 your desktop to the very latest software and the manufacturer of that device 784 00:38:17,060 --> 00:38:20,120 or software also updates by hiring an artist 785 00:38:20,120 --> 00:38:23,550 to draw those pictures in their own fonts, in their own style, 786 00:38:23,550 --> 00:38:26,900 you're going to see usually just an empty black square or maybe just 787 00:38:26,900 --> 00:38:29,402 a black and white heart instead of something more colorful. 788 00:38:29,402 --> 00:38:31,610 Really just placeholders, because, it's as though you 789 00:38:31,610 --> 00:38:33,860 don't have the right font installed or really, 790 00:38:33,860 --> 00:38:36,870 you have an older version of that same font installed. 791 00:38:36,870 --> 00:38:40,040 But it's become sort of an annual tradition that new and more emoji 792 00:38:40,040 --> 00:38:44,450 are released every year, which is among the reasons why these updates contain 793 00:38:44,450 --> 00:38:45,510 more and more. 794 00:38:45,510 --> 00:38:46,040 Yeah? 795 00:38:46,040 --> 00:38:49,047 AUDIENCE: How do you represent color in bytes? 796 00:38:49,047 --> 00:38:50,630 DAVID MALAN: That is an amazing segue. 797 00:38:50,630 --> 00:38:53,800 How do you represent color in bytes? 798 00:38:53,800 --> 00:38:57,700 Well, you use RGB, which happens to be, by coincidence, the next slide. 799 00:38:57,700 --> 00:38:59,010 So let's again, recap. 800 00:38:59,010 --> 00:39:00,570 We know how to represent letters. 801 00:39:00,570 --> 00:39:02,020 We know how to represent numbers. 802 00:39:02,020 --> 00:39:03,220 We can even represent emoji. 803 00:39:03,220 --> 00:39:05,280 But those emoji technically on the screen 804 00:39:05,280 --> 00:39:07,960 are, of course, composed of colors, like a whole bunch of yellow 805 00:39:07,960 --> 00:39:09,430 for that there smiley face? 806 00:39:09,430 --> 00:39:13,955 How do computers, then, using only zeros and ones, represent colors? 807 00:39:13,955 --> 00:39:16,830 Well, by convention, they typically use a system that, by an acronym, 808 00:39:16,830 --> 00:39:17,820 is called RGB. 809 00:39:17,820 --> 00:39:19,750 Red, green, blue. 810 00:39:19,750 --> 00:39:23,980 And this is to say that a computer, to represent a single dot on the screen-- 811 00:39:23,980 --> 00:39:28,890 maybe this one, this one, this one-- will allocate some number of bits 812 00:39:28,890 --> 00:39:33,600 or some number of bytes to represent the color of just that, their dot, otherwise 813 00:39:33,600 --> 00:39:34,810 known as a pixel. 814 00:39:34,810 --> 00:39:37,890 You can actually see pixels on your phone or even on your TV or monitor. 815 00:39:37,890 --> 00:39:40,840 If you go really close, especially if it's an older monitor, 816 00:39:40,840 --> 00:39:42,880 you can see the tiny little squares. 817 00:39:42,880 --> 00:39:47,710 Each of those has some number of bits telling the device what color to use. 818 00:39:47,710 --> 00:39:53,360 In particular, these devices typically use three numbers in total, three bytes. 819 00:39:53,360 --> 00:39:56,750 So that is to say 24 bits per pixel. 820 00:39:56,750 --> 00:39:57,765 And they do this. 821 00:39:57,765 --> 00:40:00,640 If you were to represent a single dot on the screen using these three 822 00:40:00,640 --> 00:40:04,010 numbers, just by intent here, this is 72, 73, 823 00:40:04,010 --> 00:40:07,280 33, which in the context of a text message, an email, 824 00:40:07,280 --> 00:40:11,240 a Google Doc represents, of course, hi, textually. 825 00:40:11,240 --> 00:40:13,480 What if a computer uses that same pattern 826 00:40:13,480 --> 00:40:17,170 of zeros and ones, that is the same pattern of decimal digits, 827 00:40:17,170 --> 00:40:19,400 to represent the color on a screen? 828 00:40:19,400 --> 00:40:22,698 Which is germane if you're opening an image using Photoshop. 829 00:40:22,698 --> 00:40:24,490 So using a different piece of software that 830 00:40:24,490 --> 00:40:27,400 knows about colors and images and not just text. 831 00:40:27,400 --> 00:40:30,940 Well, this would imply that you want that dot on the screen 832 00:40:30,940 --> 00:40:34,000 to have a medium amount of red, a medium amount of green, 833 00:40:34,000 --> 00:40:35,510 and a little bit of blue. 834 00:40:35,510 --> 00:40:37,190 Why do I say medium and little? 835 00:40:37,190 --> 00:40:41,840 Well, again, if each of these numbers is using 8 bits or 1 byte, 836 00:40:41,840 --> 00:40:45,260 the highest we can count, as we discovered, was 255. 837 00:40:45,260 --> 00:40:46,850 So I'm kind of averaging here. 838 00:40:46,850 --> 00:40:50,650 So 72 at a 255 feels to me like a medium amount of red. 839 00:40:50,650 --> 00:40:53,290 33 feels relatively little blue. 840 00:40:53,290 --> 00:40:58,190 But if now the computer combines those wavelengths of light, so to speak, 841 00:40:58,190 --> 00:41:00,320 a medium amount of red, medium amount of green, 842 00:41:00,320 --> 00:41:03,830 a little bit of blue, what you get is the color code for a single dot. 843 00:41:03,830 --> 00:41:07,840 And does anyone want to guess what color roughly this represents, 844 00:41:07,840 --> 00:41:10,143 these three bytes? 845 00:41:10,143 --> 00:41:10,810 AUDIENCE: White. 846 00:41:10,810 --> 00:41:11,100 AUDIENCE: Purple. 847 00:41:11,100 --> 00:41:12,558 DAVID MALAN: Not white, not purple. 848 00:41:12,558 --> 00:41:13,230 AUDIENCE: Brown. 849 00:41:13,230 --> 00:41:14,320 DAVID MALAN: Not brown. 850 00:41:14,320 --> 00:41:14,580 AUDIENCE: Yellow. 851 00:41:14,580 --> 00:41:16,660 DAVID MALAN: Yellow, in fact, is the answer. 852 00:41:16,660 --> 00:41:21,520 So it represents in a single pixel roughly this shade here of yellow. 853 00:41:21,520 --> 00:41:25,110 Which is to say, if we look back at any of those emoji, which, again, 854 00:41:25,110 --> 00:41:27,760 are represented by patterns of zeros and ones, 855 00:41:27,760 --> 00:41:31,150 but you and I as humans perceive them as images on the screen-- 856 00:41:31,150 --> 00:41:33,900 let me actually go ahead and zoom and zoom in further 857 00:41:33,900 --> 00:41:35,598 to one such sample emoji. 858 00:41:35,598 --> 00:41:38,890 And when you zoom in far enough or you put the phone close enough to your face, 859 00:41:38,890 --> 00:41:41,610 you can actually see all of those little dots known 860 00:41:41,610 --> 00:41:43,630 as pixels, all of the little squares. 861 00:41:43,630 --> 00:41:46,180 And given that so many of these pixels are yellow, 862 00:41:46,180 --> 00:41:49,450 that is to say that that pattern of three bytes, 863 00:41:49,450 --> 00:41:54,010 72, 73, 33, is used to represent this pixel. 864 00:41:54,010 --> 00:41:58,570 Another 3 identical bytes are used to represent this pixel, this one, 865 00:41:58,570 --> 00:41:59,920 this one, and so forth. 866 00:41:59,920 --> 00:42:02,710 So now if you've taken digital photos on your phone or a camera, 867 00:42:02,710 --> 00:42:05,460 you're probably generally familiar from the internet and hardware 868 00:42:05,460 --> 00:42:10,330 today that a photograph is, what, 1 megabyte, 10 megabytes depending 869 00:42:10,330 --> 00:42:12,050 on the resolution of it? 870 00:42:12,050 --> 00:42:14,480 Well, megabyte means millions of bytes. 871 00:42:14,480 --> 00:42:17,470 Where are all of these bytes inside of these photographs 872 00:42:17,470 --> 00:42:19,760 or these images you're taking or downloading? 873 00:42:19,760 --> 00:42:24,620 They correspond to every one of the single pixels on the screen. 874 00:42:24,620 --> 00:42:30,050 There's at least three bytes being used to represent every one of those dots. 875 00:42:30,050 --> 00:42:32,710 As an aside, bit of a white lie because nowadays there's 876 00:42:32,710 --> 00:42:36,590 fancy compression software that can use fewer than that many bytes. 877 00:42:36,590 --> 00:42:40,570 But in general, that's where all of those bytes, those millions of bytes 878 00:42:40,570 --> 00:42:41,980 are coming from. 879 00:42:41,980 --> 00:42:45,380 So how is that for an answer to how do we represent colors? 880 00:42:45,380 --> 00:42:46,010 Thank you. 881 00:42:46,010 --> 00:42:50,590 So if we agreed now that there's this way and perhaps 882 00:42:50,590 --> 00:42:52,960 others to represent colors, well, how do we 883 00:42:52,960 --> 00:42:55,610 represent not just images, but videos? 884 00:42:55,610 --> 00:42:59,960 Well, videos once upon a time, or movies, were called motion pictures. 885 00:42:59,960 --> 00:43:02,120 So motion pictures with motion. 886 00:43:02,120 --> 00:43:02,870 Why is that? 887 00:43:02,870 --> 00:43:04,790 Well, it's analogous to growing up. 888 00:43:04,790 --> 00:43:06,980 If you ever played with one of these picture books-- and in fact, 889 00:43:06,980 --> 00:43:09,397 there's memes nowadays that have made these popular again, 890 00:43:09,397 --> 00:43:14,540 whereby why you have a whole bunch of images on individual sheets of paper. 891 00:43:14,540 --> 00:43:16,850 And if you flip through them fast enough, 892 00:43:16,850 --> 00:43:20,690 your human mind and eyes perceive it as actual motion, 893 00:43:20,690 --> 00:43:24,020 even though you're just seeing image, image, image, image, image, image. 894 00:43:24,020 --> 00:43:26,030 But it's so fast, it looks like motion. 895 00:43:26,030 --> 00:43:28,610 That's all a video is on your screen. 896 00:43:28,610 --> 00:43:30,950 That's all a film is on your TV. 897 00:43:30,950 --> 00:43:33,140 It is not in fact, continuous motion. 898 00:43:33,140 --> 00:43:37,780 It's maybe 30 frames or images per second, maybe 24 frames 899 00:43:37,780 --> 00:43:39,200 or images per second. 900 00:43:39,200 --> 00:43:41,755 Which is to say, we know how to represent numbers, 901 00:43:41,755 --> 00:43:43,630 we know how to represent letters, we know how 902 00:43:43,630 --> 00:43:45,410 to represent colors and thus images. 903 00:43:45,410 --> 00:43:49,580 Now we kind of get videos for free because it's just more of the same. 904 00:43:49,580 --> 00:43:51,560 Use more and more of those patterns. 905 00:43:51,560 --> 00:43:53,390 Why are videos so darn large? 906 00:43:53,390 --> 00:43:56,330 Why are they gigabytes to download, billions of bytes? 907 00:43:56,330 --> 00:43:58,280 Because there's so many darn images. 908 00:43:58,280 --> 00:44:02,570 30 some odd images per second in those kinds of videos. 909 00:44:02,570 --> 00:44:05,600 And maybe lastly, just to top off our multimedia, 910 00:44:05,600 --> 00:44:08,090 how could you represent sound? 911 00:44:08,090 --> 00:44:10,090 Maybe musicians in the room. 912 00:44:10,090 --> 00:44:13,630 How, using only zeros and ones, could you represent something 913 00:44:13,630 --> 00:44:17,010 as sonorous as music? 914 00:44:17,010 --> 00:44:18,580 Something analog as digital. 915 00:44:18,580 --> 00:44:19,080 Yeah? 916 00:44:19,080 --> 00:44:21,090 AUDIENCE: So each number corresponds to a frequency. 917 00:44:21,090 --> 00:44:21,700 DAVID MALAN: Yeah. 918 00:44:21,700 --> 00:44:23,580 So each number that we store in the computer 919 00:44:23,580 --> 00:44:25,830 could correspond to a certain frequency, which 920 00:44:25,830 --> 00:44:29,455 has a direct relationship to the sound or the pitch of a note. 921 00:44:29,455 --> 00:44:32,080 For instance, in the world of piano and many other instruments, 922 00:44:32,080 --> 00:44:35,830 you've got like your A, your B, your C, maybe you have sharps and flats as well. 923 00:44:35,830 --> 00:44:37,590 We could just agree, like the ASCII people 924 00:44:37,590 --> 00:44:41,130 did years ago, to represent the musical note A, let's use 925 00:44:41,130 --> 00:44:45,310 this pattern, musical note A-sharp, let's use this pattern and so forth. 926 00:44:45,310 --> 00:44:48,280 But maybe pitch alone or frequency alone is not enough. 927 00:44:48,280 --> 00:44:51,150 Maybe we need that number, but maybe a second number 928 00:44:51,150 --> 00:44:54,030 for the volume, the sort of digital equivalent of how hard 929 00:44:54,030 --> 00:44:55,630 are you hitting the key on the piano. 930 00:44:55,630 --> 00:44:59,860 Maybe a third number for how long are you holding the key down. 931 00:44:59,860 --> 00:45:05,110 So maybe the pitch and the volume and the duration, kind of like RGB, 932 00:45:05,110 --> 00:45:10,210 we could use three bytes to represent every musical note in some piece. 933 00:45:10,210 --> 00:45:13,050 And if we wanted to keep track of what instrument should 934 00:45:13,050 --> 00:45:16,670 be played by the computer to sound that music, 935 00:45:16,670 --> 00:45:19,880 well, maybe that's just a fourth byte or something else as well. 936 00:45:19,880 --> 00:45:21,850 Which is to say, at the end of the day, all 937 00:45:21,850 --> 00:45:26,440 we have are these zeros and ones to throw at the problem. 938 00:45:26,440 --> 00:45:29,825 So for now, that's it for representing information. 939 00:45:29,825 --> 00:45:31,700 We've got our numbers, we've got our letters, 940 00:45:31,700 --> 00:45:35,470 we've got our colors and images, our videos, and now sound. 941 00:45:35,470 --> 00:45:40,000 Any questions on how computers, then, are representing, as promised, 942 00:45:40,000 --> 00:45:45,500 those inputs and outputs using just zeros and ones? 943 00:45:45,500 --> 00:45:47,570 Yeah, in the middle. 944 00:45:47,570 --> 00:45:49,550 AUDIENCE: The computer is taking it as input. 945 00:45:49,550 --> 00:45:50,190 DAVID MALAN: Correct. 946 00:45:50,190 --> 00:45:53,190 So the computer is taking as input at the end of the day, zeros and ones 947 00:45:53,190 --> 00:45:54,750 and is outputting zeros and ones. 948 00:45:54,750 --> 00:45:58,590 However, as we'll learn in this class, by writing software, 949 00:45:58,590 --> 00:46:02,430 by writing code that understands those zeros and ones we will enjoy 950 00:46:02,430 --> 00:46:04,470 not just literally seeing zeros and ones, 951 00:46:04,470 --> 00:46:08,310 we will see A, B, C, we will see colors, we will see video, 952 00:46:08,310 --> 00:46:12,350 we will hear sounds so long as we write the code to interpret those zeros 953 00:46:12,350 --> 00:46:12,990 and ones. 954 00:46:12,990 --> 00:46:15,210 And indeed, it's worth noting now that same pattern 955 00:46:15,210 --> 00:46:19,530 I keep using for an example, 72, 73, 33, how does a computer know? 956 00:46:19,530 --> 00:46:21,090 Is that the message hi? 957 00:46:21,090 --> 00:46:22,710 Is that the color yellow? 958 00:46:22,710 --> 00:46:25,100 Is it a dot in a video alone? 959 00:46:25,100 --> 00:46:26,910 Just depends on the context. 960 00:46:26,910 --> 00:46:30,230 Simply put, if you're opening that pattern of zeros and ones 961 00:46:30,230 --> 00:46:33,560 with Excel or a calculator program, odds are 962 00:46:33,560 --> 00:46:37,500 the software will interpret those three bytes as numbers, of course. 963 00:46:37,500 --> 00:46:40,590 If, though, you open that same pattern in a text messaging program, 964 00:46:40,590 --> 00:46:43,250 Google Docs, Microsoft Word, that same pattern 965 00:46:43,250 --> 00:46:46,187 will be interpreted as a sequence of letters. 966 00:46:46,187 --> 00:46:48,270 Instead, if you open Photoshop, that same pattern, 967 00:46:48,270 --> 00:46:52,300 you'll probably see a single dot that happens to be yellow. 968 00:46:52,300 --> 00:46:56,770 Conversely, once you yourself are a programmer or even better programmer, 969 00:46:56,770 --> 00:47:00,810 you will be able to write in code how you want the computer to treat 970 00:47:00,810 --> 00:47:02,560 these patterns of zeros and ones. 971 00:47:02,560 --> 00:47:07,020 You can essentially tell the computer, use this to store a number or a letter 972 00:47:07,020 --> 00:47:09,160 or a color or something else. 973 00:47:09,160 --> 00:47:14,010 That's the power the programmer themselves have at the end of the day. 974 00:47:14,010 --> 00:47:19,380 Other questions on representing things with bits? 975 00:47:19,380 --> 00:47:19,880 No? 976 00:47:19,880 --> 00:47:20,880 All right. 977 00:47:20,880 --> 00:47:25,050 So lastly, then, in this middle of this black box, so to speak, 978 00:47:25,050 --> 00:47:27,630 is the sort of secret sauce that solves problems, 979 00:47:27,630 --> 00:47:31,050 that converts those inputs to outputs, those problems to solutions. 980 00:47:31,050 --> 00:47:32,250 So what is an algorithm? 981 00:47:32,250 --> 00:47:36,540 It's really just step by step instructions for solving some problem. 982 00:47:36,540 --> 00:47:39,270 And indeed, I think back to my own first time in CS50 983 00:47:39,270 --> 00:47:42,120 where we learned the same from Professor Brian Kernighan. 984 00:47:42,120 --> 00:47:45,680 And as luck would have it, just had my 25th reunion where we pulled some video 985 00:47:45,680 --> 00:47:47,430 footage from 1996. 986 00:47:47,430 --> 00:47:52,130 And so we're actually fortunate to have the very first few minutes of CS50 987 00:47:52,130 --> 00:47:55,020 over 25 years ago when I myself took it. 988 00:47:55,020 --> 00:47:59,010 But the lessons back then, as today, are fundamentally the same. 989 00:47:59,010 --> 00:48:03,780 And what's important, indeed, is to not only express yourself correctly, 990 00:48:03,780 --> 00:48:06,870 but precisely, as we'll explore today. 991 00:48:06,870 --> 00:48:09,050 This then is Professor Brian Kernighan, who, 992 00:48:09,050 --> 00:48:13,760 years ago, very memorably introduced us and my classmates to algorithms 993 00:48:13,760 --> 00:48:17,520 by actually, in class, shaving his beard. 994 00:48:17,520 --> 00:48:19,760 If we could dim the lights here for Brian. 995 00:48:19,760 --> 00:48:20,670 [VIDEO PLAYBACK] 996 00:48:20,670 --> 00:48:22,290 - The other thing that we're going to talk about in this class 997 00:48:22,290 --> 00:48:24,060 is the notion of an algorithm. 998 00:48:24,060 --> 00:48:27,160 An algorithm is a very precise description of how to do something. 999 00:48:27,160 --> 00:48:29,130 And the operative word there is precise. 1000 00:48:29,130 --> 00:48:31,990 It has to be very, very, very, very precise. 1001 00:48:31,990 --> 00:48:33,990 And the task that I'm going to do is that I'm 1002 00:48:33,990 --> 00:48:36,995 going to trim my beard, which has gotten out of whack. 1003 00:48:36,995 --> 00:48:40,868 1004 00:48:40,868 --> 00:48:43,288 [APPLAUSE] 1005 00:48:43,288 --> 00:48:45,230 1006 00:48:45,230 --> 00:48:56,820 And I brought a variety of things which one might use to trim beards with. 1007 00:48:56,820 --> 00:48:59,295 [LAUGHTER] 1008 00:48:59,295 --> 00:49:01,275 1009 00:49:01,275 --> 00:49:03,750 [APPLAUSE] 1010 00:49:03,750 --> 00:49:12,165 1011 00:49:12,165 --> 00:49:13,273 [END PLAYBACK] 1012 00:49:13,273 --> 00:49:15,940 DAVID MALAN: So suffice it to say, I don't have much of a beard. 1013 00:49:15,940 --> 00:49:20,168 But I do have this here other technology known once upon a time as a phone book. 1014 00:49:20,168 --> 00:49:22,960 And these phone books, of course, have lots of information in them. 1015 00:49:22,960 --> 00:49:25,380 Happen to be storing numbers and letters in particular. 1016 00:49:25,380 --> 00:49:29,340 For those unfamiliar, they are storing human's names from A to Z 1017 00:49:29,340 --> 00:49:32,050 here in English and associated with everyone's name is a number. 1018 00:49:32,050 --> 00:49:35,220 So even if you've never had occasion to physically use this kind of device, 1019 00:49:35,220 --> 00:49:39,060 turns out it's pretty much equivalent to the contacts or the address book app 1020 00:49:39,060 --> 00:49:41,200 on your iOS phone or your Android phone as well. 1021 00:49:41,200 --> 00:49:41,700 Why? 1022 00:49:41,700 --> 00:49:43,700 Because if you pull up your contacts, of course, 1023 00:49:43,700 --> 00:49:47,140 you see some familiar names here alphabetized by first name or last name. 1024 00:49:47,140 --> 00:49:49,170 And if you click on any of those names, you 1025 00:49:49,170 --> 00:49:52,300 reach the person you're presumably trying to call or text. 1026 00:49:52,300 --> 00:49:55,201 Pictured here then is John Harvard's, whose number here is plus 1027 00:49:55,201 --> 00:49:59,860 1-949-468-2750, which you're welcome to call or text at your leisure. 1028 00:49:59,860 --> 00:50:04,030 But here is John Harvard that's partway through the phone book digitally. 1029 00:50:04,030 --> 00:50:06,460 Well, it turns out that physically in the phone book, 1030 00:50:06,460 --> 00:50:09,670 we might use an algorithm, step by step instructions, 1031 00:50:09,670 --> 00:50:14,690 for finding John Harvard in pretty much the same way as iOS, Android, 1032 00:50:14,690 --> 00:50:17,870 Mac OS, Windows, or other operating systems themselves use. 1033 00:50:17,870 --> 00:50:21,770 So I could, when looking for John Harvard, first name, starting with J, 1034 00:50:21,770 --> 00:50:23,770 I could start at the beginning of the phone book 1035 00:50:23,770 --> 00:50:28,490 and start looking page by page by page for John Harvard. 1036 00:50:28,490 --> 00:50:30,140 And if he's there, I can call. 1037 00:50:30,140 --> 00:50:31,290 This is an algorithm. 1038 00:50:31,290 --> 00:50:33,370 It's indeed step by step, but that was a bug. 1039 00:50:33,370 --> 00:50:34,540 A few pages turned. 1040 00:50:34,540 --> 00:50:38,410 But is this algorithm correct? 1041 00:50:38,410 --> 00:50:41,440 Step by step, assuming I'm paying attention. 1042 00:50:41,440 --> 00:50:44,860 So yes, if John Harvard is in here, I will eventually 1043 00:50:44,860 --> 00:50:46,470 find him once I get to the J section. 1044 00:50:46,470 --> 00:50:47,720 Now, this is a little tedious. 1045 00:50:47,720 --> 00:50:48,865 It's going to take a while. 1046 00:50:48,865 --> 00:50:50,240 A few dozen, a few hundred pages. 1047 00:50:50,240 --> 00:50:55,250 So maybe I could do things a little smarter from grade school, like 2, 4, 6, 1048 00:50:55,250 --> 00:51:00,140 8, 10, 12, 14, 16, and so forth, going twice as fast. 1049 00:51:00,140 --> 00:51:01,940 Is that algorithm correct? 1050 00:51:01,940 --> 00:51:03,403 So no, but why? 1051 00:51:03,403 --> 00:51:04,570 AUDIENCE: You could miss it. 1052 00:51:04,570 --> 00:51:05,660 DAVID MALAN: I could miss him, right? 1053 00:51:05,660 --> 00:51:08,035 I could just get unlucky, really, with 50/50 probability, 1054 00:51:08,035 --> 00:51:11,930 because John Harvard could be sandwiched between two pages. 1055 00:51:11,930 --> 00:51:14,510 Now, this isn't a complete loss, this algorithm. 1056 00:51:14,510 --> 00:51:18,770 Maybe what I could do is if I get past the J section to K, 1057 00:51:18,770 --> 00:51:21,610 I could double back at least one page just 1058 00:51:21,610 --> 00:51:23,290 to make sure I didn't miss John Harvard. 1059 00:51:23,290 --> 00:51:26,830 So I can still go twice as fast plus an extra step just 1060 00:51:26,830 --> 00:51:28,340 to make sure I didn't mess up. 1061 00:51:28,340 --> 00:51:30,918 So the first algorithm might take as many pages 1062 00:51:30,918 --> 00:51:32,210 as there are in the phone book. 1063 00:51:32,210 --> 00:51:35,980 So if this phone book has a thousand pages, in the worst case, if I'm not 1064 00:51:35,980 --> 00:51:38,860 looking for John Harvard, but someone whose name starts with Z, 1065 00:51:38,860 --> 00:51:41,780 might take me a thousand pages to actually get there. 1066 00:51:41,780 --> 00:51:43,610 Second algorithm, twice as fast. 1067 00:51:43,610 --> 00:51:46,420 Literally, it might take me 500 plus one step 1068 00:51:46,420 --> 00:51:48,410 to get there because I'm going two at a time, 1069 00:51:48,410 --> 00:51:50,102 so as long as I indeed fix that bug. 1070 00:51:50,102 --> 00:51:52,060 But what we used to do back in the day and what 1071 00:51:52,060 --> 00:51:55,910 your phone is doing today, albeit digitally, 1072 00:51:55,910 --> 00:51:59,560 is going roughly to the middle of the phone book, looking down and realizing, 1073 00:51:59,560 --> 00:52:03,410 oh, I'm accidentally in the M section, so halfway through the phone book. 1074 00:52:03,410 --> 00:52:05,600 But what do I now know about John Harvard? 1075 00:52:05,600 --> 00:52:07,750 Is he to the left or to the right? 1076 00:52:07,750 --> 00:52:11,080 So he's obviously to the left, because J comes before M. So what 1077 00:52:11,080 --> 00:52:13,330 I can do literally and what your computer does 1078 00:52:13,330 --> 00:52:17,380 figuratively is tear the problem in half, throw half of the problem 1079 00:52:17,380 --> 00:52:21,140 away, leaving us now with the same fundamental problem, 1080 00:52:21,140 --> 00:52:22,430 but it's half as big. 1081 00:52:22,430 --> 00:52:26,740 So I've gone from a thousand pages suddenly to 500 pages. 1082 00:52:26,740 --> 00:52:33,260 And compare this to the other two, 1,000 pages, 999, 998, versus 1,000 pages, 1083 00:52:33,260 --> 00:52:36,080 998, 996, 994. 1084 00:52:36,080 --> 00:52:37,310 That's still slow. 1085 00:52:37,310 --> 00:52:41,540 I went from 1,000 to 500 in just one step of this algorithm. 1086 00:52:41,540 --> 00:52:42,327 What do I do next? 1087 00:52:42,327 --> 00:52:43,660 I go roughly to the middle here. 1088 00:52:43,660 --> 00:52:45,080 Oh, I went a little too far. 1089 00:52:45,080 --> 00:52:46,670 I'm in the E section now. 1090 00:52:46,670 --> 00:52:49,180 So is John Harvard to the left or right now? 1091 00:52:49,180 --> 00:52:50,270 So he's to the right. 1092 00:52:50,270 --> 00:52:53,870 So I can again tear the problem in half, throw the left half away, 1093 00:52:53,870 --> 00:52:56,830 knowing now that John Harvard must alphabetically be in here. 1094 00:52:56,830 --> 00:53:01,750 And I can divide and divide and divide and conquer this problem again and again 1095 00:53:01,750 --> 00:53:04,610 by using that heuristic of going left or going right. 1096 00:53:04,610 --> 00:53:06,580 And I dare say, if I do this correctly, I'll 1097 00:53:06,580 --> 00:53:10,830 eventually be left with one single page on which John Harvard's number actually 1098 00:53:10,830 --> 00:53:11,330 is. 1099 00:53:11,330 --> 00:53:14,720 Or maybe he's not in the phone book at all. 1100 00:53:14,720 --> 00:53:19,340 So how many steps maximally might that third and final algorithm take? 1101 00:53:19,340 --> 00:53:20,360 It's not a thousand. 1102 00:53:20,360 --> 00:53:22,940 It's not even 500 or 501. 1103 00:53:22,940 --> 00:53:27,490 How many times can you divide a thousand pages in half again and again 1104 00:53:27,490 --> 00:53:30,182 and again, roughly? 1105 00:53:30,182 --> 00:53:31,390 AUDIENCE: I want to say nine. 1106 00:53:31,390 --> 00:53:32,750 DAVID MALAN: So 9, 10. 1107 00:53:32,750 --> 00:53:34,610 So typically 10 times, give or take. 1108 00:53:34,610 --> 00:53:37,610 There's a bit of rounding there because it's not a perfect power of two, 1109 00:53:37,610 --> 00:53:39,220 but roughly 10 times. 1110 00:53:39,220 --> 00:53:43,150 Like, that is fundamentally better than both of the two algorithms 1111 00:53:43,150 --> 00:53:48,490 because I go from a thousand pages to 500 to 250 to 125 and so 1112 00:53:48,490 --> 00:53:52,280 forth, literally halving the problem again and again. 1113 00:53:52,280 --> 00:53:56,000 So we can actually appreciate and see this even more so graphically. 1114 00:53:56,000 --> 00:53:58,480 And this is among the things we'll do later in the term 1115 00:53:58,480 --> 00:54:01,250 when we speak to not only writing correct code, 1116 00:54:01,250 --> 00:54:03,620 but is your code well designed? 1117 00:54:03,620 --> 00:54:06,080 Is it better than your previous code? 1118 00:54:06,080 --> 00:54:07,970 Is it better than someone else's code? 1119 00:54:07,970 --> 00:54:09,710 Is it better than some other product? 1120 00:54:09,710 --> 00:54:13,190 If you have given more thought to the algorithms and the quality thereof, 1121 00:54:13,190 --> 00:54:17,090 you can perhaps minimize the time required to solve problems 1122 00:54:17,090 --> 00:54:18,470 but no less correctly. 1123 00:54:18,470 --> 00:54:21,842 So if we have a simple xy plot here, on the y-axis or vertical 1124 00:54:21,842 --> 00:54:23,800 is the amount of time to solve in whatever unit 1125 00:54:23,800 --> 00:54:26,560 of measure, seconds, pages, however you want to count. 1126 00:54:26,560 --> 00:54:30,700 On the horizontal or x-axis is the size of the problem measured 1127 00:54:30,700 --> 00:54:32,700 in, for instance, numbers of pages. 1128 00:54:32,700 --> 00:54:35,250 So this would mean zero pages in the phone book. 1129 00:54:35,250 --> 00:54:37,320 This would mean a lot of pages in the phone book. 1130 00:54:37,320 --> 00:54:39,120 This would mean no time to solve. 1131 00:54:39,120 --> 00:54:40,980 This would mean a lot of time to solve. 1132 00:54:40,980 --> 00:54:43,740 What's the relationship then, among those three algorithms? 1133 00:54:43,740 --> 00:54:47,190 Well, the first one is essentially a straight line, a slope of one. 1134 00:54:47,190 --> 00:54:50,660 And if the phone book has n pages in it, we'll 1135 00:54:50,660 --> 00:54:54,650 describe the slope here as essentially 1 over 1 for the algorithm 1136 00:54:54,650 --> 00:54:58,470 with the first algorithm, turning page by page by page. 1137 00:54:58,470 --> 00:55:02,880 Which is to say, if we were to add one more page to the phone book next year, 1138 00:55:02,880 --> 00:55:05,280 first algorithm is going to take one more step. 1139 00:55:05,280 --> 00:55:07,890 But the second algorithm is definitely better. 1140 00:55:07,890 --> 00:55:10,410 It's definitely faster, but it's still a straight line. 1141 00:55:10,410 --> 00:55:14,060 So it's going to take roughly n over 2 steps on average, because you only 1142 00:55:14,060 --> 00:55:15,770 have to go through half of the phone book 1143 00:55:15,770 --> 00:55:17,478 because you're going two pages at a time, 1144 00:55:17,478 --> 00:55:19,860 instead of the whole phone book in the worst case, 1145 00:55:19,860 --> 00:55:23,370 if someone's name is Z, to go through every page in total. 1146 00:55:23,370 --> 00:55:26,460 So if we actually compare these-- let me just draw some dashed lines. 1147 00:55:26,460 --> 00:55:29,330 Suppose that you have this many pages in the phone book. 1148 00:55:29,330 --> 00:55:31,980 If you just draw this vertical white line here, 1149 00:55:31,980 --> 00:55:35,250 it's going to take this much time in red using the first algorithm, 1150 00:55:35,250 --> 00:55:37,460 but it's going to literally take half as much time 1151 00:55:37,460 --> 00:55:39,918 in yellow for the second algorithm because you're literally 1152 00:55:39,918 --> 00:55:41,280 going two pages at once. 1153 00:55:41,280 --> 00:55:45,510 But the third and final algorithm is a fundamentally different shape. 1154 00:55:45,510 --> 00:55:48,030 It instead looks a little something like this. 1155 00:55:48,030 --> 00:55:51,180 It looks like it's flatter and flatter and flatter. 1156 00:55:51,180 --> 00:55:52,620 It's always increasing. 1157 00:55:52,620 --> 00:55:54,300 It never gets perfectly flat. 1158 00:55:54,300 --> 00:55:59,580 But it grows so much more slowly as a function of phone book size. 1159 00:55:59,580 --> 00:56:02,460 And for those who recall their logarithms, 1160 00:56:02,460 --> 00:56:04,845 this would be described as log base 2 of n. 1161 00:56:04,845 --> 00:56:06,720 And in fact, that's where the math came from. 1162 00:56:06,720 --> 00:56:10,560 Log base 2 of 1,000 is roughly 10 in total, 1163 00:56:10,560 --> 00:56:12,960 even if you need a calculator to confirm as much. 1164 00:56:12,960 --> 00:56:15,570 But this shape is fundamentally different. 1165 00:56:15,570 --> 00:56:16,070 Why? 1166 00:56:16,070 --> 00:56:18,360 Well, suppose that Cambridge, where we are, and Allston, 1167 00:56:18,360 --> 00:56:21,193 the town across the street next year, combine their two phone books. 1168 00:56:21,193 --> 00:56:25,333 And they go from a thousand pages each to one phone book with 2,000 pages. 1169 00:56:25,333 --> 00:56:27,500 The first algorithm is going to literally take twice 1170 00:56:27,500 --> 00:56:29,130 as many steps or pages. 1171 00:56:29,130 --> 00:56:33,200 Second algorithm is going to take half as many or 50% more 1172 00:56:33,200 --> 00:56:34,680 because you're going two at a time. 1173 00:56:34,680 --> 00:56:37,560 But the third algorithm is going to barely miss a beat. 1174 00:56:37,560 --> 00:56:38,060 Why? 1175 00:56:38,060 --> 00:56:42,750 Because if this is a thousand pages here and 2,000 pages is over there, 1176 00:56:42,750 --> 00:56:45,030 just inferring from the shape of the green line, 1177 00:56:45,030 --> 00:56:50,270 it's not going to be much higher on the vertical axis than the other two were. 1178 00:56:50,270 --> 00:56:55,710 So more specifically, if you have a 2,000 page phone book next year, 1179 00:56:55,710 --> 00:57:00,170 how many more steps will it take you using that third and final algorithm? 1180 00:57:00,170 --> 00:57:03,830 Just one, because you'll divide and conquer a 2,000 page phone book 1181 00:57:03,830 --> 00:57:07,950 into a 1,000 page phone book, and then you're back at the original story. 1182 00:57:07,950 --> 00:57:10,710 And that's the sort of power of learning algorithms. 1183 00:57:10,710 --> 00:57:14,060 That's the power of learning computer science and learning how to program 1184 00:57:14,060 --> 00:57:17,070 is to be able to navigate big data, so to speak. 1185 00:57:17,070 --> 00:57:20,990 Things the size of google, things the size of artificial intelligence training 1186 00:57:20,990 --> 00:57:23,750 data sets using better and better, more clever 1187 00:57:23,750 --> 00:57:26,127 algorithms that perform faster, and therefore 1188 00:57:26,127 --> 00:57:27,960 not only make the software more competitive, 1189 00:57:27,960 --> 00:57:32,390 but also make it more usable and more favorable for people like you and me 1190 00:57:32,390 --> 00:57:34,380 when using that software. 1191 00:57:34,380 --> 00:57:39,120 So when it comes to implementing algorithms as programmers, 1192 00:57:39,120 --> 00:57:42,590 as computer scientists, what you're really doing is taking these algorithms, 1193 00:57:42,590 --> 00:57:45,750 which might be expressed in English conceptually as we just did, 1194 00:57:45,750 --> 00:57:48,180 but really just translating them to code, 1195 00:57:48,180 --> 00:57:54,020 be it C or C++ or Python or R or Ruby or any number of other languages that exist 1196 00:57:54,020 --> 00:57:54,720 in the world. 1197 00:57:54,720 --> 00:57:57,200 But for now, let's consider how we might implement 1198 00:57:57,200 --> 00:58:01,140 that algorithm using something that's literally still English, but pseudocode. 1199 00:58:01,140 --> 00:58:05,660 Something that is still correct, but precise and finite, 1200 00:58:05,660 --> 00:58:07,850 as per Professor Kernighan's advice, which 1201 00:58:07,850 --> 00:58:10,430 is to say use your own vernacular of English and just 1202 00:58:10,430 --> 00:58:12,480 say what you mean but very succinctly. 1203 00:58:12,480 --> 00:58:14,400 There's no one way to write pseudocode. 1204 00:58:14,400 --> 00:58:15,870 It's not some formal language. 1205 00:58:15,870 --> 00:58:19,340 I'm just going to convert the steps I did intuitively to step 1206 00:58:19,340 --> 00:58:21,660 by step instructions as follows. 1207 00:58:21,660 --> 00:58:25,340 Step one, what I did was pretty much pick up the phone book. 1208 00:58:25,340 --> 00:58:28,850 Step two, what I did was pretty much open to middle of phone book 1209 00:58:28,850 --> 00:58:30,120 for the third algorithm. 1210 00:58:30,120 --> 00:58:31,790 Step three, look at page. 1211 00:58:31,790 --> 00:58:37,830 Step four, if person is on page, then, step five, call person. 1212 00:58:37,830 --> 00:58:40,880 If that does not prove to be the case, step six, 1213 00:58:40,880 --> 00:58:44,180 else if the person is earlier in the book, then 1214 00:58:44,180 --> 00:58:51,650 open to the middle of the left half of the book and then go back to line three. 1215 00:58:51,650 --> 00:58:55,970 Then, else if the person is later in the book, 1216 00:58:55,970 --> 00:59:00,830 open to the middle of the right half of the book and, again, go to line three. 1217 00:59:00,830 --> 00:59:03,150 Else, there's a fourth and final case. 1218 00:59:03,150 --> 00:59:06,180 If the person like John Harvard is not on the page, is not earlier, 1219 00:59:06,180 --> 00:59:10,110 is not later, what's the fourth scenario we'd best consider? 1220 00:59:10,110 --> 00:59:11,540 He's just not there. 1221 00:59:11,540 --> 00:59:13,950 Else we should do something specific like quit. 1222 00:59:13,950 --> 00:59:16,170 Now, as an aside, everyone in this room has probably 1223 00:59:16,170 --> 00:59:18,170 had one of these stupid technical support issues 1224 00:59:18,170 --> 00:59:20,670 where your phone or your laptop or your desktop computer 1225 00:59:20,670 --> 00:59:24,730 just freeze all of a sudden, or maybe it spontaneously reboots for no reason. 1226 00:59:24,730 --> 00:59:29,440 Odds are that's because not you but some other human made a mistake. 1227 00:59:29,440 --> 00:59:32,400 They probably wrote code working at Microsoft or Apple 1228 00:59:32,400 --> 00:59:34,860 or Google or somewhere else, and they didn't actually 1229 00:59:34,860 --> 00:59:38,310 anticipate that, oh, there could be a fourth scenario that 1230 00:59:38,310 --> 00:59:40,120 could happen in the real world. 1231 00:59:40,120 --> 00:59:42,570 But if there's no code that tells the computer what 1232 00:59:42,570 --> 00:59:45,335 to do in that fourth and final scenario, who 1233 00:59:45,335 --> 00:59:46,960 knows what the computer is going to do? 1234 00:59:46,960 --> 00:59:48,490 It might, by default, reboot. 1235 00:59:48,490 --> 00:59:50,100 It might, by default, freeze. 1236 00:59:50,100 --> 00:59:54,040 That's just a hint of the bugs, the mistakes in software to come. 1237 00:59:54,040 --> 00:59:58,420 But even though this is just one way to write this code, 1238 00:59:58,420 --> 01:00:01,590 a.k.a. pseudocode, there are some salient characteristics 1239 01:00:01,590 --> 01:00:03,420 that we'll use throughout today. 1240 01:00:03,420 --> 01:00:06,540 One, there are these verbs, these actions. 1241 01:00:06,540 --> 01:00:09,610 And henceforth, as aspiring computer scientists or programmers, 1242 01:00:09,610 --> 01:00:12,880 we're going to start to call these by what a more and more technical audience 1243 01:00:12,880 --> 01:00:13,380 would. 1244 01:00:13,380 --> 01:00:14,930 These are functions. 1245 01:00:14,930 --> 01:00:17,450 A function is an action or a verb. 1246 01:00:17,450 --> 01:00:20,600 It's like a bite-sized task that a computer can do for you. 1247 01:00:20,600 --> 01:00:23,540 Those then are functions in this here pseudocode. 1248 01:00:23,540 --> 01:00:26,060 But there's other types of code in here. 1249 01:00:26,060 --> 01:00:27,440 There are these things here. 1250 01:00:27,440 --> 01:00:29,240 If else if else if else. 1251 01:00:29,240 --> 01:00:32,180 Those are examples of what we're going to start calling conditionals. 1252 01:00:32,180 --> 01:00:35,900 These are sort of proverbial forks in the road where maybe you go this way, 1253 01:00:35,900 --> 01:00:40,850 maybe you go this way, but you decide which way to go based on a question. 1254 01:00:40,850 --> 01:00:43,420 The questions that you ask are what we'll technically 1255 01:00:43,420 --> 01:00:47,150 call Boolean expressions named after mathematician Boole. 1256 01:00:47,150 --> 01:00:51,170 A Boolean expression is a question with a yes or no answer, 1257 01:00:51,170 --> 01:00:56,180 a true or false answer, a black or white answer, a one or zero answer. 1258 01:00:56,180 --> 01:00:59,840 There's two possibilities, and there's a hint of the binary underneath. 1259 01:00:59,840 --> 01:01:03,250 A Boolean expression is going to tell you yes or no, you 1260 01:01:03,250 --> 01:01:06,070 should go down that fork in the road. 1261 01:01:06,070 --> 01:01:11,330 Notice what's important here is that indentation mattered as a result. 1262 01:01:11,330 --> 01:01:16,000 Notice that on line four when I first asked if the person is on page question 1263 01:01:16,000 --> 01:01:20,450 mark, so to speak, I should only do line five per its indentation 1264 01:01:20,450 --> 01:01:23,758 if the answer is yes or true, I should only 1265 01:01:23,758 --> 01:01:26,800 open to the middle of the left half of the book and go back to line three 1266 01:01:26,800 --> 01:01:29,930 if person is instead earlier in the book. 1267 01:01:29,930 --> 01:01:33,220 So indentation in pseudocode and in many programming languages 1268 01:01:33,220 --> 01:01:35,090 has logical significance. 1269 01:01:35,090 --> 01:01:37,580 It tells you whether to do things or not. 1270 01:01:37,580 --> 01:01:39,800 But there's another construct in here. 1271 01:01:39,800 --> 01:01:40,660 Go back to. 1272 01:01:40,660 --> 01:01:44,380 Go back to, which literally makes me go back to line three, potentially 1273 01:01:44,380 --> 01:01:47,380 again and again and again, creating some kind of cycle 1274 01:01:47,380 --> 01:01:50,750 or what we'll typically call a loop instead. 1275 01:01:50,750 --> 01:01:54,320 So even in this relatively simple real world algorithm, 1276 01:01:54,320 --> 01:01:57,520 we have these four fundamental characteristics of most computer 1277 01:01:57,520 --> 01:01:59,360 programs that we will write in this class, 1278 01:01:59,360 --> 01:02:03,010 and you might write beyond this class, that we have some technical jargon now 1279 01:02:03,010 --> 01:02:04,040 to describe them. 1280 01:02:04,040 --> 01:02:08,270 But what's important to note is that line 8 and line 11, 1281 01:02:08,270 --> 01:02:11,720 even though they're saying go back to line three, go back to line three, 1282 01:02:11,720 --> 01:02:13,960 you might think you're running the risk of what 1283 01:02:13,960 --> 01:02:17,350 we'll call an infinite loop where you literally get stuck in a loop 1284 01:02:17,350 --> 01:02:20,118 forever, which doesn't sound like a good thing if, at some point, 1285 01:02:20,118 --> 01:02:22,910 you want to turn your computer off, even though it's still working. 1286 01:02:22,910 --> 01:02:26,060 But these will not induce infinite loops. 1287 01:02:26,060 --> 01:02:26,560 Why? 1288 01:02:26,560 --> 01:02:30,460 What is happening in this particular algorithm every time we go back to line 1289 01:02:30,460 --> 01:02:35,930 three that guarantees eventually we will stop going back to line three? 1290 01:02:35,930 --> 01:02:37,972 AUDIENCE: The person is on the page, you call it. 1291 01:02:37,972 --> 01:02:38,847 DAVID MALAN: Exactly. 1292 01:02:38,847 --> 01:02:41,540 If the person is on the page, we will call them or we will quit. 1293 01:02:41,540 --> 01:02:45,380 But more importantly, because we keep dividing and conquering the problem, 1294 01:02:45,380 --> 01:02:48,200 in this case, having the phone book, having the phone book, 1295 01:02:48,200 --> 01:02:51,860 eventually we're going to run out of phone book, in which case, indeed, 1296 01:02:51,860 --> 01:02:55,150 John Harvard is either on that page or not And we will call 1297 01:02:55,150 --> 01:02:56,830 or we will quit instead. 1298 01:02:56,830 --> 01:02:58,070 So we'll see in time. 1299 01:02:58,070 --> 01:02:59,780 And in fact, allow me to promise. 1300 01:02:59,780 --> 01:03:01,690 Odds are at some point you will write code 1301 01:03:01,690 --> 01:03:04,318 that seems to take control over the computer for you, 1302 01:03:04,318 --> 01:03:06,860 where it's doing something, doing something, doing something, 1303 01:03:06,860 --> 01:03:08,870 and it literally won't respond to you anymore. 1304 01:03:08,870 --> 01:03:11,680 That's just going to be because of a mistake, a so-called bug 1305 01:03:11,680 --> 01:03:16,310 that you yourself will invariably have added to your code accidentally. 1306 01:03:16,310 --> 01:03:20,503 But we'll show you ways for terminating it or breaking out of those conditions. 1307 01:03:20,503 --> 01:03:22,420 And indeed, what we'll do in just a little bit 1308 01:03:22,420 --> 01:03:26,540 after a break for today's lecture is explore not just these concepts, 1309 01:03:26,540 --> 01:03:29,530 but some of the ways you can use them to solve real and very 1310 01:03:29,530 --> 01:03:31,335 visual and audio problems. 1311 01:03:31,335 --> 01:03:33,460 But for now, let's at least connect it to something 1312 01:03:33,460 --> 01:03:37,150 that's been all too germane in recent months, the past few years, namely 1313 01:03:37,150 --> 01:03:39,740 artificial intelligence, which is a topic we'll come back to 1314 01:03:39,740 --> 01:03:41,900 at the end of the course, too, to give you 1315 01:03:41,900 --> 01:03:44,270 a sense of what the connection is with what everyone's 1316 01:03:44,270 --> 01:03:46,610 been talking about in the world of AI and what 1317 01:03:46,610 --> 01:03:50,480 it is we're going to spend the next few weeks building up to by writing code. 1318 01:03:50,480 --> 01:03:54,090 If you were to try to implement something like a chat bot, 1319 01:03:54,090 --> 01:03:56,270 for instance, that just answers questions and has 1320 01:03:56,270 --> 01:03:59,300 a conversation with you, you could do that using pseudocode, 1321 01:03:59,300 --> 01:04:03,470 and as we'll soon see, you can use C, Python, any number of other languages 1322 01:04:03,470 --> 01:04:04,010 too. 1323 01:04:04,010 --> 01:04:07,470 That pseudocode might look like this when implementing a chat bot. 1324 01:04:07,470 --> 01:04:10,470 You could tell the chat bot, if the student says hello to you, 1325 01:04:10,470 --> 01:04:12,450 then say hello back. 1326 01:04:12,450 --> 01:04:16,370 And the indentation, as per earlier, implies this is conditional. 1327 01:04:16,370 --> 01:04:20,700 Else if the student says goodbye to you, say goodbye to the student. 1328 01:04:20,700 --> 01:04:24,420 Else if the student asks you how you are, say you are well. 1329 01:04:24,420 --> 01:04:27,170 So you can just enumerate question after question after question 1330 01:04:27,170 --> 01:04:30,930 and just handle all of these conditional possibilities. 1331 01:04:30,930 --> 01:04:32,990 But things kind of escalate quickly, especially 1332 01:04:32,990 --> 01:04:35,150 with the tools of today like ChatGPT. 1333 01:04:35,150 --> 01:04:37,970 Are we really going to have the wherewithal as programmers 1334 01:04:37,970 --> 01:04:41,660 to write another conditional like else if the student asks why 111 in binary 1335 01:04:41,660 --> 01:04:43,910 is 7 in decimal-- like, this kind of hints at, 1336 01:04:43,910 --> 01:04:45,890 oh my God, there's an infinite number of things 1337 01:04:45,890 --> 01:04:47,810 this human could ask the chat bot. 1338 01:04:47,810 --> 01:04:51,660 Do we really have to write an infinite number of conditionals? 1339 01:04:51,660 --> 01:04:53,372 That's just not possible. 1340 01:04:53,372 --> 01:04:55,080 Like, there's not enough time in the day, 1341 01:04:55,080 --> 01:04:57,230 there's not enough lines of code available. 1342 01:04:57,230 --> 01:05:00,770 Artificial intelligence surely needs to be able to figure some of this 1343 01:05:00,770 --> 01:05:01,950 out instead. 1344 01:05:01,950 --> 01:05:05,490 And so indeed, this is not how you implement AI, 1345 01:05:05,490 --> 01:05:08,360 but rather how you implement an AI like a chat bot 1346 01:05:08,360 --> 01:05:12,630 is you typically train it based on lots and lots of data. 1347 01:05:12,630 --> 01:05:15,680 You give it lots of inputs, lots of inputs, training data, 1348 01:05:15,680 --> 01:05:20,730 and let it figure out what it should say in response to certain questions. 1349 01:05:20,730 --> 01:05:22,650 And it boils down to a lot of probability, 1350 01:05:22,650 --> 01:05:26,310 a lot of statistics, otherwise known now as large language models, 1351 01:05:26,310 --> 01:05:29,480 which, if we really peek under the hood, are actually typically implemented 1352 01:05:29,480 --> 01:05:32,730 with what are called neural networks inspired by the world of biology, 1353 01:05:32,730 --> 01:05:35,510 whereby we humans have all of these neurons that 1354 01:05:35,510 --> 01:05:38,840 transmit electrical signals such that my brain tells my hand to move 1355 01:05:38,840 --> 01:05:40,590 this way, this way, and this other way. 1356 01:05:40,590 --> 01:05:43,640 And so what computer scientists have been doing over 1357 01:05:43,640 --> 01:05:47,990 the past many years is implementing in software using literally zeros 1358 01:05:47,990 --> 01:05:51,405 and ones, graphs or networks, neural networks, that 1359 01:05:51,405 --> 01:05:54,780 look a little something like this, where each of the circles represents a neuron, 1360 01:05:54,780 --> 01:05:57,620 each of the arrows represents a pathway between them, 1361 01:05:57,620 --> 01:06:01,460 and provides as input to these networks huge amounts 1362 01:06:01,460 --> 01:06:04,820 of data like all of the internet, all of Wikipedia, all of the books 1363 01:06:04,820 --> 01:06:06,540 that it might consume as input. 1364 01:06:06,540 --> 01:06:11,030 And then the goal of this neural network, as per this single final neuron 1365 01:06:11,030 --> 01:06:14,160 right here, is to produce an answer to a question. 1366 01:06:14,160 --> 01:06:18,200 Maybe it's simple like, yes, no, or maybe it's something like the answer 1367 01:06:18,200 --> 01:06:23,340 to the 111 question or how are you or goodbye or hello or the like. 1368 01:06:23,340 --> 01:06:26,870 And what these neural networks do is use statistics and probability 1369 01:06:26,870 --> 01:06:31,550 and try to output the most probabilistically likely answer 1370 01:06:31,550 --> 01:06:36,450 to this question that's been asked and really just hope that it is correct. 1371 01:06:36,450 --> 01:06:38,600 There is no programmer at OpenAI or Google 1372 01:06:38,600 --> 01:06:42,200 or Microsoft that's trying to anticipate every one of these questions 1373 01:06:42,200 --> 01:06:46,500 we might ask, not only in English but in other languages as well. 1374 01:06:46,500 --> 01:06:49,500 So you might be wondering why there's this 8 foot duck on the stage. 1375 01:06:49,500 --> 01:06:54,185 So the persona that CS50's own AI takes is in fact that of a rubber duck, 1376 01:06:54,185 --> 01:06:56,060 because it turns out in programming circles-- 1377 01:06:56,060 --> 01:06:58,050 and this is true long before CS50-- 1378 01:06:58,050 --> 01:07:01,280 it has often been recommended to students and aspiring programmers 1379 01:07:01,280 --> 01:07:04,800 that you keep literally a physical rubber duck on your desk. 1380 01:07:04,800 --> 01:07:08,630 The idea being, in the absence of a friend, family member, colleague, 1381 01:07:08,630 --> 01:07:12,290 TA who could answer technical questions for you, if you're alone 1382 01:07:12,290 --> 01:07:17,240 in your room in Mather at night, you can talk to the duck, maybe door closed, 1383 01:07:17,240 --> 01:07:20,340 and ask the duck your questions, or, more importantly, 1384 01:07:20,340 --> 01:07:23,180 talk the duck through what confusion you're having. 1385 01:07:23,180 --> 01:07:26,640 And the mere act of talking through the problem, 1386 01:07:26,640 --> 01:07:29,970 explaining logically what you're trying to do, what you're actually doing, 1387 01:07:29,970 --> 01:07:32,340 and what the error actually is, invariably, 1388 01:07:32,340 --> 01:07:36,090 that sort of proverbial light bulb goes off and you realize, oh, I'm an idiot. 1389 01:07:36,090 --> 01:07:38,910 I hear in my own words where I've gone awry. 1390 01:07:38,910 --> 01:07:42,820 And even though this duck will never say anything back to you, that alone, 1391 01:07:42,820 --> 01:07:47,530 rubber duck debugging or rubber ducking, tends to be a valuable programming 1392 01:07:47,530 --> 01:07:49,070 technique, believe it or not. 1393 01:07:49,070 --> 01:07:51,080 But thanks to these large language models, 1394 01:07:51,080 --> 01:07:53,780 we have not only physical but virtual ducks as well. 1395 01:07:53,780 --> 01:07:57,670 And so available to you will be in this class not tools like ChatGPT 1396 01:07:57,670 --> 01:08:00,470 and the like, which are, through policy, disallowed. 1397 01:08:00,470 --> 01:08:03,560 It is not reasonable to use ChatGPT and the like. 1398 01:08:03,560 --> 01:08:08,020 But you are allowed and encouraged to use CS50's own AI based 1399 01:08:08,020 --> 01:08:12,010 tools, which resemble those same tools but know something about CS50 1400 01:08:12,010 --> 01:08:15,610 and aspire to behave akin to a good teaching fellow guiding you 1401 01:08:15,610 --> 01:08:19,160 to solutions as opposed to handing you something outright. 1402 01:08:19,160 --> 01:08:21,399 So this is a tool that will be available at literally 1403 01:08:21,399 --> 01:08:22,939 this URL throughout the course. 1404 01:08:22,939 --> 01:08:24,130 CS50.ai. 1405 01:08:24,130 --> 01:08:27,250 It will also be embedded in the programming environment you'll soon 1406 01:08:27,250 --> 01:08:31,069 meet, which is called Visual Studio Code, a cloud based version thereof. 1407 01:08:31,069 --> 01:08:35,590 The duck will live in that environment as well, as well as on stage from time 1408 01:08:35,590 --> 01:08:39,140 to time, which is to say we'll not only talk about, but use 1409 01:08:39,140 --> 01:08:42,380 throughout the course this thing known as AI. 1410 01:08:42,380 --> 01:08:46,066 But this is ultimately code that we're going to start writing next week. 1411 01:08:46,066 --> 01:08:47,899 And unfortunately, this code here is written 1412 01:08:47,899 --> 01:08:51,050 in a language called C. This is essentially the program that I lost 1413 01:08:51,050 --> 01:08:53,870 two points on some 25 plus years ago. 1414 01:08:53,870 --> 01:08:56,810 It does look admittedly cryptic. 1415 01:08:56,810 --> 01:09:01,020 That's why today what we'll focus on is not what this code looks like, 1416 01:09:01,020 --> 01:09:06,229 nor the zeros and ones that that code gets converted to so your computer 1417 01:09:06,229 --> 01:09:08,910 can understand as input what you want it to do. 1418 01:09:08,910 --> 01:09:12,000 We're going to focus on a much more visual incarnation of this. 1419 01:09:12,000 --> 01:09:13,670 But I know thus far this has been a lot. 1420 01:09:13,670 --> 01:09:16,410 So let's go ahead and take a five minute break here, 1421 01:09:16,410 --> 01:09:19,710 and when we come back in five, we'll do some actual programming. 1422 01:09:19,710 --> 01:09:21,859 So see you in five. 1423 01:09:21,859 --> 01:09:24,380 All right. 1424 01:09:24,380 --> 01:09:29,910 So it's now time to solve with actual code some actual problems, 1425 01:09:29,910 --> 01:09:32,430 albeit in a fun and visual and audio way. 1426 01:09:32,430 --> 01:09:34,970 But recall that where we left off was this. 1427 01:09:34,970 --> 01:09:38,670 Starting next week, you'll be writing code that ultimately looks like this, 1428 01:09:38,670 --> 01:09:41,370 but thankfully, you will not be writing zeros and ones, 1429 01:09:41,370 --> 01:09:43,370 and no normal person, myself included, can 1430 01:09:43,370 --> 01:09:46,747 understand what all of these zeros and ones are at a glance. 1431 01:09:46,747 --> 01:09:48,830 We could take out some paper, pencil, and probably 1432 01:09:48,830 --> 01:09:50,609 figure it out very tediously. 1433 01:09:50,609 --> 01:09:52,770 But this is exactly the point. 1434 01:09:52,770 --> 01:09:56,360 Computers only understand this stuff, but what we as programmers 1435 01:09:56,360 --> 01:10:01,525 will start writing today and beyond is code at a higher level. 1436 01:10:01,525 --> 01:10:02,900 And indeed, this is going to be-- 1437 01:10:02,900 --> 01:10:05,390 this is going to be frequent within computer science 1438 01:10:05,390 --> 01:10:08,695 where there's different levels of abstraction that we operate at. 1439 01:10:08,695 --> 01:10:11,570 And the lowest level, the nittiest gritty, is like the zeros and ones 1440 01:10:11,570 --> 01:10:12,780 that computer understand. 1441 01:10:12,780 --> 01:10:14,850 That's it in this class for zeros and ones. 1442 01:10:14,850 --> 01:10:16,790 Hopefully you at least have wrapped your mind 1443 01:10:16,790 --> 01:10:20,570 around why zeros and ones can be used in triples 1444 01:10:20,570 --> 01:10:23,940 and as bytes to represent higher and higher numbers. 1445 01:10:23,940 --> 01:10:27,240 But let's just now agree that computers can do that. 1446 01:10:27,240 --> 01:10:30,170 Let's abstract away from that detail and focus 1447 01:10:30,170 --> 01:10:34,285 on higher level languages than zeros and ones, namely a language like this. 1448 01:10:34,285 --> 01:10:36,410 So this is an example of the very first programming 1449 01:10:36,410 --> 01:10:40,130 language I learned back in the day as per that homework in a language called 1450 01:10:40,130 --> 01:10:40,710 C. 1451 01:10:40,710 --> 01:10:44,480 It's an older language, but it remains one of the most popular languages 1452 01:10:44,480 --> 01:10:48,710 in omnipresent languages nowadays because it's incredibly fast 1453 01:10:48,710 --> 01:10:53,520 and it's particularly good at making devices operate quickly. 1454 01:10:53,520 --> 01:10:56,090 For us pedagogically, the value of C is not 1455 01:10:56,090 --> 01:10:59,630 that you're probably in Silicon Valley and other such jobs 1456 01:10:59,630 --> 01:11:02,780 going to be using C yourself that much, but because it's 1457 01:11:02,780 --> 01:11:06,620 going to provide a conceptual foundation on top of which we introduce 1458 01:11:06,620 --> 01:11:10,290 other languages, like Python, which is newer and improved, so to speak, 1459 01:11:10,290 --> 01:11:14,580 that gives you more and more functionality for free out of the box 1460 01:11:14,580 --> 01:11:19,198 by abstracting away some of the stuff we'll focus on in the coming days first. 1461 01:11:19,198 --> 01:11:20,990 So at the end of the day, you should better 1462 01:11:20,990 --> 01:11:23,930 understand languages like Python and JavaScript 1463 01:11:23,930 --> 01:11:29,160 and SQL because of your underlying understanding of a language like C. 1464 01:11:29,160 --> 01:11:30,922 But this is too much for the first day. 1465 01:11:30,922 --> 01:11:33,630 Many of you will think that this is too much for the second week. 1466 01:11:33,630 --> 01:11:37,280 But in fact, C is really only sort of scary 1467 01:11:37,280 --> 01:11:40,798 looking because all of this darn punctuation and syntax, the semicolon, 1468 01:11:40,798 --> 01:11:43,590 the parentheses, the double quotes, the curly braces, and the like. 1469 01:11:43,590 --> 01:11:44,310 And I concur. 1470 01:11:44,310 --> 01:11:48,200 This is intellectually uninteresting, and a lot of the challenges early 1471 01:11:48,200 --> 01:11:51,410 on when learning programming is you just don't have the muscle memory 1472 01:11:51,410 --> 01:11:54,620 that I or some of the teaching fellows might for knowing 1473 01:11:54,620 --> 01:11:56,460 what symbol should be where. 1474 01:11:56,460 --> 01:12:00,720 But that's going to come with time and practice, I guarantee it. 1475 01:12:00,720 --> 01:12:03,050 What we'll do for today, though, is just throw away 1476 01:12:03,050 --> 01:12:08,580 all of that intellectually uninteresting detail and focus really on ideas. 1477 01:12:08,580 --> 01:12:10,610 And some of you might be in your comfort zone 1478 01:12:10,610 --> 01:12:12,350 here because if back in middle school you 1479 01:12:12,350 --> 01:12:14,850 were playing with a programming language called Scratch, 1480 01:12:14,850 --> 01:12:18,420 you were probably using at the time just to have fun in class or out of class, 1481 01:12:18,420 --> 01:12:20,880 making games, animations, interactive art. 1482 01:12:20,880 --> 01:12:24,240 What you probably didn't use it for, at least in middle school, 1483 01:12:24,240 --> 01:12:27,870 was to consider and explore programming languages themselves. 1484 01:12:27,870 --> 01:12:31,370 But what's wonderful about Scratch, which is this graphical programming 1485 01:12:31,370 --> 01:12:34,830 language from down the street at MIT, where it was invented some years ago, 1486 01:12:34,830 --> 01:12:37,880 is you can program not by using your keyboard per se, 1487 01:12:37,880 --> 01:12:41,860 but by dragging and dropping puzzle pieces, otherwise known as blocks, 1488 01:12:41,860 --> 01:12:45,350 that will snap together if it makes logical sense to do so. 1489 01:12:45,350 --> 01:12:48,850 And what you won't have to deal with is parentheses and double quotes 1490 01:12:48,850 --> 01:12:51,830 and semicolons and all of that, at least until next week. 1491 01:12:51,830 --> 01:12:54,580 But the nice thing about Scratch is that after this week 1492 01:12:54,580 --> 01:12:56,450 and after the so-called problem set zero, 1493 01:12:56,450 --> 01:12:58,580 the first assignment in which you'll use Scratch, 1494 01:12:58,580 --> 01:13:02,890 you'll have a mental model via which it will be easier to pick up 1495 01:13:02,890 --> 01:13:05,240 all of the subsequent syntax as well. 1496 01:13:05,240 --> 01:13:08,140 So let's see how we can start programming in Scratch 1497 01:13:08,140 --> 01:13:10,550 by making the simplest of programs first. 1498 01:13:10,550 --> 01:13:13,707 You can do this at scratch.mit.edu. 1499 01:13:13,707 --> 01:13:15,290 You needn't do this now in the moment. 1500 01:13:15,290 --> 01:13:17,810 Problem set zero will walk you through all of these steps. 1501 01:13:17,810 --> 01:13:23,170 But what I've done here is opened up at scratch.mit.edu, precisely 1502 01:13:23,170 --> 01:13:26,030 the default web page therein. 1503 01:13:26,030 --> 01:13:28,240 This is after having clicked the Create button 1504 01:13:28,240 --> 01:13:31,100 in Scratch, which is going to allow me to create my first program. 1505 01:13:31,100 --> 01:13:34,000 But first, a tour of the user interface here, 1506 01:13:34,000 --> 01:13:36,620 and what is ultimately available to you. 1507 01:13:36,620 --> 01:13:39,200 Well, within the Scratch environment, we'll 1508 01:13:39,200 --> 01:13:41,160 see a few different regions of the screen. 1509 01:13:41,160 --> 01:13:44,580 One, we have this palette of puzzle pieces at left. 1510 01:13:44,580 --> 01:13:48,450 The blue ones relate to motion, the purple ones relate to looks, 1511 01:13:48,450 --> 01:13:50,880 the pink ones relate to sound, and so forth. 1512 01:13:50,880 --> 01:13:53,480 So the color of the blocks just roughly categorizes 1513 01:13:53,480 --> 01:13:55,797 what that block's purpose in life is. 1514 01:13:55,797 --> 01:13:58,880 We're going to be able to use those puzzle pieces by dragging and dropping 1515 01:13:58,880 --> 01:14:00,353 them from left to right. 1516 01:14:00,353 --> 01:14:02,270 In the right here, in the middle of the screen 1517 01:14:02,270 --> 01:14:04,350 is where I'm going to write my actual programs. 1518 01:14:04,350 --> 01:14:06,558 This is where I'll drag and drop these puzzle pieces, 1519 01:14:06,558 --> 01:14:09,330 lock them together, and actually write my code. 1520 01:14:09,330 --> 01:14:11,220 What am I going to be coding? 1521 01:14:11,220 --> 01:14:13,470 Well, I'm going to be controlling one or more sprites. 1522 01:14:13,470 --> 01:14:16,553 Much like in the world of games are familiar, a sprite is like a character 1523 01:14:16,553 --> 01:14:17,970 that you might see on the screen. 1524 01:14:17,970 --> 01:14:20,000 The default character in the world of Scratch 1525 01:14:20,000 --> 01:14:22,230 is, in fact, a cat that looks like this. 1526 01:14:22,230 --> 01:14:24,840 And if in this case, I have just one cat, 1527 01:14:24,840 --> 01:14:28,980 I can then make that cat do things in his own little world at top right 1528 01:14:28,980 --> 01:14:31,470 by making the cat move up, down, left, right, 1529 01:14:31,470 --> 01:14:33,647 spinning around, or doing other things as well. 1530 01:14:33,647 --> 01:14:35,480 But if you want to introduce a dog or a bird 1531 01:14:35,480 --> 01:14:38,780 or any number of other custom characters, you just add more sprites 1532 01:14:38,780 --> 01:14:41,940 and they get their own place in that same world. 1533 01:14:41,940 --> 01:14:43,982 As for how to think about movement in this world, 1534 01:14:43,982 --> 01:14:45,857 it's actually pretty familiar, even though it 1535 01:14:45,857 --> 01:14:47,490 gets a little numeric for a moment. 1536 01:14:47,490 --> 01:14:52,530 If Scratch at the moment is in the middle of the screen, the cat is at 0, 0 1537 01:14:52,530 --> 01:14:55,350 if you think about x, y-coordinates or latitude longitude. 1538 01:14:55,350 --> 01:14:58,590 If you move the cat all the way up, this would still be x equals 0, 1539 01:14:58,590 --> 01:15:00,510 but it would be y 180. 1540 01:15:00,510 --> 01:15:01,720 What's the 180? 1541 01:15:01,720 --> 01:15:04,710 180 pixels vertically or dots on the screen. 1542 01:15:04,710 --> 01:15:08,250 This is negative 180 pixels on the screen at the bottom. 1543 01:15:08,250 --> 01:15:11,630 By contrast, if you go left and right, your x value might change. 1544 01:15:11,630 --> 01:15:17,580 Negative 240, but y is 0, or positive 240 and y is 0 as well. 1545 01:15:17,580 --> 01:15:20,750 But most of the time you won't need to know or care about what 1546 01:15:20,750 --> 01:15:23,070 the pixel coordinates of the cat are. 1547 01:15:23,070 --> 01:15:26,120 All you're generally going to care about is the programmer, most likely, 1548 01:15:26,120 --> 01:15:29,630 is do you want the cat to go relatively up, down, left, or right, 1549 01:15:29,630 --> 01:15:32,810 and let MIT figure out the mathematics of moving this thing around 1550 01:15:32,810 --> 01:15:34,340 in most cases. 1551 01:15:34,340 --> 01:15:35,040 All right. 1552 01:15:35,040 --> 01:15:37,700 So let's go ahead and introduce the first of these programs 1553 01:15:37,700 --> 01:15:42,890 by doing something quite simple, as we did in C there, but a little more simply 1554 01:15:42,890 --> 01:15:44,520 by writing code as follows. 1555 01:15:44,520 --> 01:15:47,330 I'm going to go back to scratch.mit.edu. 1556 01:15:47,330 --> 01:15:50,070 I've already clicked, per before, the Create button. 1557 01:15:50,070 --> 01:15:53,600 And if I click on the yellow category of blocks here at left-- 1558 01:15:53,600 --> 01:15:56,790 and I'll zoom in-- we'll see a whole bunch of yellow puzzle pieces. 1559 01:15:56,790 --> 01:15:58,730 And probably the most common one you will 1560 01:15:58,730 --> 01:16:02,390 use to write code in Scratch for just this first week is literally 1561 01:16:02,390 --> 01:16:04,110 when green flag clicked. 1562 01:16:04,110 --> 01:16:04,640 Why? 1563 01:16:04,640 --> 01:16:07,470 Well, if we go back over to the cat's world at top right, 1564 01:16:07,470 --> 01:16:10,530 notice that above the cat's rectangular world, 1565 01:16:10,530 --> 01:16:12,720 there's not only a green flag for starting, 1566 01:16:12,720 --> 01:16:15,600 there's a red stop sign for stopping as well. 1567 01:16:15,600 --> 01:16:16,560 So let's do this. 1568 01:16:16,560 --> 01:16:18,150 Let me go ahead and click and drag. 1569 01:16:18,150 --> 01:16:21,420 When green flag clicked anywhere into the middle and let go. 1570 01:16:21,420 --> 01:16:25,100 And now I'm going to go to looks, and it looks 1571 01:16:25,100 --> 01:16:27,480 like there's a whole bunch of purple puzzle pieces here. 1572 01:16:27,480 --> 01:16:30,950 I'm going to choose something simple like say hello, drag it. 1573 01:16:30,950 --> 01:16:33,200 And notice if I get just close enough, it's 1574 01:16:33,200 --> 01:16:35,340 going to want to magnetically snap together. 1575 01:16:35,340 --> 01:16:37,260 So I'll just do that and it does its thing. 1576 01:16:37,260 --> 01:16:39,410 The fact that there's this white oval with text 1577 01:16:39,410 --> 01:16:44,040 means that is an input to this, say, puzzle piece. 1578 01:16:44,040 --> 01:16:46,070 I can literally then change what the input 1579 01:16:46,070 --> 01:16:49,590 is if I want to more conventionally say hello, world. 1580 01:16:49,590 --> 01:16:54,590 Which in fact, according to lore, was the very first program written in C, 1581 01:16:54,590 --> 01:16:59,190 and nowadays in most every language, including in Brian Kernighan's book. 1582 01:16:59,190 --> 01:17:01,370 So hello world is generally the first program 1583 01:17:01,370 --> 01:17:03,960 that most any programmer first writes. 1584 01:17:03,960 --> 01:17:05,900 So that's it as programs go. 1585 01:17:05,900 --> 01:17:07,350 Let me go ahead and zoom out here. 1586 01:17:07,350 --> 01:17:11,400 Let me go over to the right and click the green flag, and somewhat excitingly, 1587 01:17:11,400 --> 01:17:15,680 maybe underwhelmingly, we've now written a program that quite simply says 1588 01:17:15,680 --> 01:17:17,367 hello world on the screen. 1589 01:17:17,367 --> 01:17:19,950 Now let's make this a little more technical for just a moment. 1590 01:17:19,950 --> 01:17:22,350 What is this here puzzle piece, as I keep calling it? 1591 01:17:22,350 --> 01:17:24,850 It's actually a similar-- 1592 01:17:24,850 --> 01:17:29,220 it's an incarnation one of the ideas from our pseudocode before. 1593 01:17:29,220 --> 01:17:33,543 What did we call those actions and verbs last time in my pseudocode? 1594 01:17:33,543 --> 01:17:34,460 AUDIENCE: [INAUDIBLE]. 1595 01:17:34,460 --> 01:17:35,418 DAVID MALAN: Functions. 1596 01:17:35,418 --> 01:17:36,090 That's right. 1597 01:17:36,090 --> 01:17:40,010 So these purple puzzle pieces here are indeed functions, 1598 01:17:40,010 --> 01:17:44,610 and some functions, as we can see, take inputs, like hello comma world. 1599 01:17:44,610 --> 01:17:47,190 After all, how does Scratch know what to say? 1600 01:17:47,190 --> 01:17:49,790 You have to provide the cat with input, which 1601 01:17:49,790 --> 01:17:52,290 is to say functions can indeed take inputs like this. 1602 01:17:52,290 --> 01:17:54,620 In this case one input, but we'll see opportunities 1603 01:17:54,620 --> 01:17:56,850 for passing in more input as well. 1604 01:17:56,850 --> 01:18:00,440 What the cat is doing though, visually on the screen here at top right, 1605 01:18:00,440 --> 01:18:02,460 is what's generally called a side effect. 1606 01:18:02,460 --> 01:18:06,180 Sometimes when you call a function, it does something visually. 1607 01:18:06,180 --> 01:18:08,630 And in this case, you're seeing literally a cartoon speech 1608 01:18:08,630 --> 01:18:09,870 bubble, hello world. 1609 01:18:09,870 --> 01:18:12,180 That is the side effect of this function. 1610 01:18:12,180 --> 01:18:15,380 So if we now want to map this to our world of inputs and outputs 1611 01:18:15,380 --> 01:18:17,780 and see where this side effect is, this is the paradigm 1612 01:18:17,780 --> 01:18:21,800 I proposed at the start of class that is computer science in a nutshell 1613 01:18:21,800 --> 01:18:23,870 and will be the framework we use literally 1614 01:18:23,870 --> 01:18:26,345 throughout the class, no matter how-- 1615 01:18:26,345 --> 01:18:29,070 no matter how the languages in particular evolve. 1616 01:18:29,070 --> 01:18:31,950 So what's the input to this particular program? 1617 01:18:31,950 --> 01:18:34,680 Well, this white oval, hello world is my input. 1618 01:18:34,680 --> 01:18:37,950 The algorithm, step by step instructions for solving some problem, 1619 01:18:37,950 --> 01:18:41,760 is implemented in code, this language called Scratch 1620 01:18:41,760 --> 01:18:43,690 by way of this purple puzzle piece. 1621 01:18:43,690 --> 01:18:47,650 And the output of that function, given this input, 1622 01:18:47,650 --> 01:18:52,080 is the side effect whereby the cat indeed says hello world visually 1623 01:18:52,080 --> 01:18:54,220 on the screen in that speech bubble. 1624 01:18:54,220 --> 01:18:56,790 So the exact same paradigm with which we began today 1625 01:18:56,790 --> 01:18:59,667 governs how exactly this cat here works. 1626 01:18:59,667 --> 01:19:01,500 Well, let's actually go back to this program 1627 01:19:01,500 --> 01:19:03,660 and make it a little more interesting than that. 1628 01:19:03,660 --> 01:19:05,700 Let me go ahead and click the red stop sign. 1629 01:19:05,700 --> 01:19:09,933 And let me actually use a different type of puzzle piece, another function that 1630 01:19:09,933 --> 01:19:11,350 does something a little different. 1631 01:19:11,350 --> 01:19:13,570 First, I'm going to get rid of the say block. 1632 01:19:13,570 --> 01:19:15,330 So I'm going to not only pull it away, I'm 1633 01:19:15,330 --> 01:19:17,950 going to drag it over anywhere at left and just let go 1634 01:19:17,950 --> 01:19:20,200 and it will delete itself automatically. 1635 01:19:20,200 --> 01:19:23,260 Or I could right click or Control click, and from a little menu 1636 01:19:23,260 --> 01:19:25,510 I could also explicitly say delete. 1637 01:19:25,510 --> 01:19:28,090 And what I'm going to do now is under sensing, 1638 01:19:28,090 --> 01:19:32,080 which is a light blue shade of puzzle piece-- there's a whole bunch here, 1639 01:19:32,080 --> 01:19:33,610 but I'm going to focus on this one. 1640 01:19:33,610 --> 01:19:36,250 Ask something and wait. 1641 01:19:36,250 --> 01:19:38,193 And the default text is, what's your name? 1642 01:19:38,193 --> 01:19:38,860 And that's fine. 1643 01:19:38,860 --> 01:19:40,620 But because it's a white oval, that input 1644 01:19:40,620 --> 01:19:43,900 can be manually changed by me if I wanted to change the question. 1645 01:19:43,900 --> 01:19:45,340 I'm going to drag it over here. 1646 01:19:45,340 --> 01:19:48,130 It's going to magnetically snap together. 1647 01:19:48,130 --> 01:19:49,720 And I'm OK with that question. 1648 01:19:49,720 --> 01:19:52,365 But what do I want to say with the answer? 1649 01:19:52,365 --> 01:19:53,740 Well, let's go ahead and do this. 1650 01:19:53,740 --> 01:19:55,990 I could go to looks again. 1651 01:19:55,990 --> 01:19:59,260 I could grab another say block, let it snap in, 1652 01:19:59,260 --> 01:20:03,850 and I could say something like, hello, David. 1653 01:20:03,850 --> 01:20:06,640 But this is going to be the first of many bugs that I make, 1654 01:20:06,640 --> 01:20:08,170 intentionally or otherwise. 1655 01:20:08,170 --> 01:20:09,870 Let me click the green flag. 1656 01:20:09,870 --> 01:20:13,820 Scratch is now, just like in a web browser, prompting me for some input 1657 01:20:13,820 --> 01:20:14,320 here. 1658 01:20:14,320 --> 01:20:15,945 So let me go ahead and type in my name. 1659 01:20:15,945 --> 01:20:16,540 David. 1660 01:20:16,540 --> 01:20:17,290 Enter. 1661 01:20:17,290 --> 01:20:18,390 And voila. 1662 01:20:18,390 --> 01:20:19,370 It works. 1663 01:20:19,370 --> 01:20:20,320 Hello, David. 1664 01:20:20,320 --> 01:20:21,820 I'm kind of cheating, though, right? 1665 01:20:21,820 --> 01:20:25,240 Because if I zoom out, stop, and play again. 1666 01:20:25,240 --> 01:20:30,520 Let me type in Julia's name here, enter, and it still says hello, David. 1667 01:20:30,520 --> 01:20:33,430 So that didn't really implement the idea that I wanted. 1668 01:20:33,430 --> 01:20:35,110 All right, so how can I fix this? 1669 01:20:35,110 --> 01:20:38,020 Well, it seems that this time I want more than a side effect. 1670 01:20:38,020 --> 01:20:40,990 I want to use the value that the human types in. 1671 01:20:40,990 --> 01:20:43,770 And for this, we need another feature of functions, 1672 01:20:43,770 --> 01:20:46,270 which is that not only can they sometimes have side effects, 1673 01:20:46,270 --> 01:20:47,680 something visually happens. 1674 01:20:47,680 --> 01:20:52,650 Some functions can hand you back a value, a so-called return value, 1675 01:20:52,650 --> 01:20:57,910 that will allow you to actually reuse whatever the human typed in. 1676 01:20:57,910 --> 01:21:01,380 So a return value is something that gets virtually handed back to you 1677 01:21:01,380 --> 01:21:04,620 and you can store it in something called a variable, like x, y, and z 1678 01:21:04,620 --> 01:21:08,530 in mathematics, and you can generally reuse it one or more times. 1679 01:21:08,530 --> 01:21:10,500 So let me actually draw our attention then 1680 01:21:10,500 --> 01:21:15,430 to, at left, not only the blue puzzle piece, ask what's your name and wait, 1681 01:21:15,430 --> 01:21:18,400 but notice that there's a special puzzle piece below it, 1682 01:21:18,400 --> 01:21:22,140 this blue oval called answer, and that represents what a computer 1683 01:21:22,140 --> 01:21:24,040 scientist would call a return value. 1684 01:21:24,040 --> 01:21:27,120 So MIT has kind of bundled it together side by side 1685 01:21:27,120 --> 01:21:30,130 to make clear that one of those pieces relates to the other. 1686 01:21:30,130 --> 01:21:32,320 What it means is that I can do this. 1687 01:21:32,320 --> 01:21:37,313 I can drag this oval and use this oval as the input to the save function. 1688 01:21:37,313 --> 01:21:40,480 Now, notice it's not the same size, but it is the right shape, so that's OK. 1689 01:21:40,480 --> 01:21:44,260 Scratch will grow or shrink things to fit properly. 1690 01:21:44,260 --> 01:21:46,300 But this too isn't quite right. 1691 01:21:46,300 --> 01:21:47,940 Let me go ahead and do this. 1692 01:21:47,940 --> 01:21:50,575 Let me go ahead and stop that, click the green flag. 1693 01:21:50,575 --> 01:21:51,700 I'll type in my name again. 1694 01:21:51,700 --> 01:21:54,030 D-A-V-I-D. Enter. 1695 01:21:54,030 --> 01:21:56,410 And it's just kind of weird or rude. 1696 01:21:56,410 --> 01:21:59,590 Like, I wanted a hello at least, and it just said David on the screen. 1697 01:21:59,590 --> 01:22:00,820 OK, so I can fix that. 1698 01:22:00,820 --> 01:22:02,920 Let me stop with the red stop sign. 1699 01:22:02,920 --> 01:22:04,860 Let me just separate these temporarily. 1700 01:22:04,860 --> 01:22:06,860 And I can leave it in the middle there, but they 1701 01:22:06,860 --> 01:22:09,040 have no logical connection temporarily. 1702 01:22:09,040 --> 01:22:10,810 Let me go back up to looks. 1703 01:22:10,810 --> 01:22:14,130 Let me grab a say block, a second one, and let me go ahead 1704 01:22:14,130 --> 01:22:17,500 and say, just to be grammatical, hello, space. 1705 01:22:17,500 --> 01:22:19,630 And then I'll reconnect this here. 1706 01:22:19,630 --> 01:22:23,290 So at the moment it looks like what I want, I want a hello, comma, 1707 01:22:23,290 --> 01:22:28,060 and then the return value printed out based on whatever the human typed in. 1708 01:22:28,060 --> 01:22:29,170 So let me zoom out. 1709 01:22:29,170 --> 01:22:30,930 Let me click the green flag. 1710 01:22:30,930 --> 01:22:32,910 Again, what's your name? 1711 01:22:32,910 --> 01:22:36,450 D-A-V-I-D. And watch the cat's side effect. 1712 01:22:36,450 --> 01:22:38,140 Enter. 1713 01:22:38,140 --> 01:22:41,620 It's still not greeting me properly. 1714 01:22:41,620 --> 01:22:42,650 There's no hello. 1715 01:22:42,650 --> 01:22:44,770 And if in case it was too fast, let's do it again. 1716 01:22:44,770 --> 01:22:45,650 Green flag. 1717 01:22:45,650 --> 01:22:47,230 D-A-V-I-D. Enter. 1718 01:22:47,230 --> 01:22:49,550 It rudely just says my name, which is weird. 1719 01:22:49,550 --> 01:22:51,290 What's the bug here, though? 1720 01:22:51,290 --> 01:22:53,760 It's a little more subtle. 1721 01:22:53,760 --> 01:22:54,260 Why? 1722 01:22:54,260 --> 01:22:54,900 Yeah? 1723 01:22:54,900 --> 01:22:56,370 AUDIENCE: It's just quickly going. 1724 01:22:56,370 --> 01:22:57,120 DAVID MALAN: Yeah. 1725 01:22:57,120 --> 01:23:00,088 It's just too quickly going over the say command or the say function, 1726 01:23:00,088 --> 01:23:00,630 in this case. 1727 01:23:00,630 --> 01:23:03,630 My Mac, your PC, your phone, it's just so darn fast. 1728 01:23:03,630 --> 01:23:06,750 Both are happening, but too fast for my human eyes to even notice. 1729 01:23:06,750 --> 01:23:09,295 So we can solve this in a number of ways. 1730 01:23:09,295 --> 01:23:11,670 I could actually use a different puzzle piece altogether. 1731 01:23:11,670 --> 01:23:13,460 In fact, MIT kind of anticipated this. 1732 01:23:13,460 --> 01:23:16,970 Notice the first puzzle piece in purple is say hello 1733 01:23:16,970 --> 01:23:18,840 for a specific number of seconds, and you 1734 01:23:18,840 --> 01:23:20,840 can specify not just the message, but the number 1735 01:23:20,840 --> 01:23:25,560 of seconds, ergo two inputs, otherwise now known as arguments to a function. 1736 01:23:25,560 --> 01:23:28,460 An input to a function is just an argument now. 1737 01:23:28,460 --> 01:23:30,360 And that would be a fix here. 1738 01:23:30,360 --> 01:23:33,240 I could maybe a little more explicitly do this. 1739 01:23:33,240 --> 01:23:37,080 I could go under events, scroll down a little bit, and-- sorry, 1740 01:23:37,080 --> 01:23:40,430 under control in orange, I could grab a wait block 1741 01:23:40,430 --> 01:23:42,510 and I could kind insert it in the middle. 1742 01:23:42,510 --> 01:23:43,890 And this might actually help. 1743 01:23:43,890 --> 01:23:45,890 So I could click on the green flag. 1744 01:23:45,890 --> 01:23:47,670 D-A-V-I-D. Enter. 1745 01:23:47,670 --> 01:23:49,370 Hello, David. 1746 01:23:49,370 --> 01:23:51,810 And I could change the timing to be a little more natural. 1747 01:23:51,810 --> 01:23:55,070 But what if I want the cat to just say hello, David all in one 1748 01:23:55,070 --> 01:23:56,470 breath, so to speak. 1749 01:23:56,470 --> 01:23:59,700 Well, for that I'm going to need to use a slightly different technique as 1750 01:23:59,700 --> 01:24:00,460 follows. 1751 01:24:00,460 --> 01:24:02,380 Let me go ahead and get rid of the wait. 1752 01:24:02,380 --> 01:24:06,100 Let me get rid of the second say block and stop the cat with the stop sign. 1753 01:24:06,100 --> 01:24:10,800 Let me go under operators here and let me somewhat cleverly grab this. 1754 01:24:10,800 --> 01:24:12,790 A join block at the bottom. 1755 01:24:12,790 --> 01:24:15,250 By default, it's using apple and banana as placeholders, 1756 01:24:15,250 --> 01:24:17,440 but those are white ovals so I can change those. 1757 01:24:17,440 --> 01:24:22,110 Let me drag this over the white oval for the save function and let 1758 01:24:22,110 --> 01:24:23,890 go, and it will snap to fill. 1759 01:24:23,890 --> 01:24:28,870 Let me go ahead here and type hello, comma, space instead of apple. 1760 01:24:28,870 --> 01:24:31,450 And what should I do instead of banana? 1761 01:24:31,450 --> 01:24:32,250 AUDIENCE: Answer. 1762 01:24:32,250 --> 01:24:33,000 DAVID MALAN: Yeah. 1763 01:24:33,000 --> 01:24:35,850 So it'd be answer return value-- 1764 01:24:35,850 --> 01:24:36,880 the return value. 1765 01:24:36,880 --> 01:24:39,580 So let me go under sensing again. 1766 01:24:39,580 --> 01:24:41,230 Let me just drag another copy of it. 1767 01:24:41,230 --> 01:24:42,730 And you can use these again and again and again. 1768 01:24:42,730 --> 01:24:43,840 They don't disappear. 1769 01:24:43,840 --> 01:24:49,020 I want to drag answer over banana so that the second input to join 1770 01:24:49,020 --> 01:24:53,910 is actually, if you will, the output of the ask block, like that. 1771 01:24:53,910 --> 01:24:55,300 And it snaps to fit. 1772 01:24:55,300 --> 01:24:57,970 So now if I go ahead and click the green flag once more. 1773 01:24:57,970 --> 01:24:59,770 D-A-V-I-D. Enter. 1774 01:24:59,770 --> 01:25:03,820 Now we have the behavior aesthetically that I cared about. 1775 01:25:03,820 --> 01:25:06,090 But beyond the aesthetics of this, the goal here 1776 01:25:06,090 --> 01:25:10,480 really was to map it to, again, this same paradigm, which we'll see here. 1777 01:25:10,480 --> 01:25:14,380 The algorithm and the output and the input for this example are as follows. 1778 01:25:14,380 --> 01:25:17,500 The input to the say block was, quote, unquote, what's your name? 1779 01:25:17,500 --> 01:25:20,490 The function, of course, implementing that algorithm in code 1780 01:25:20,490 --> 01:25:22,410 was the ask and wait block. 1781 01:25:22,410 --> 01:25:27,130 The output, though, of the ask block recalls not some visual side effect. 1782 01:25:27,130 --> 01:25:30,400 It is a return value called answer, like a variable, 1783 01:25:30,400 --> 01:25:32,900 a special variable like x, y, and z in math. 1784 01:25:32,900 --> 01:25:35,320 But in this one, we generally in programming 1785 01:25:35,320 --> 01:25:38,750 describe variables with actual words, not just letters. 1786 01:25:38,750 --> 01:25:43,480 But this output of the say block, I kind of want to make room for it 1787 01:25:43,480 --> 01:25:47,282 to pass it into the say block as a second argument. 1788 01:25:47,282 --> 01:25:47,990 So let's do this. 1789 01:25:47,990 --> 01:25:51,610 Let's take one step back and propose that now for the join 1790 01:25:51,610 --> 01:25:53,510 block that I just used. 1791 01:25:53,510 --> 01:25:57,290 It takes two inputs hello, space and answer. 1792 01:25:57,290 --> 01:26:00,140 The function in question is indeed the join block. 1793 01:26:00,140 --> 01:26:03,050 The output of this had better be hello, David. 1794 01:26:03,050 --> 01:26:05,540 What do I want to do with the output of the join block? 1795 01:26:05,540 --> 01:26:07,310 Well, let me clear the screen. 1796 01:26:07,310 --> 01:26:11,680 Let me move this over, because now the output of the join block 1797 01:26:11,680 --> 01:26:15,280 is going to instantly become the input to the say block 1798 01:26:15,280 --> 01:26:18,130 so that the output now in this multistep process 1799 01:26:18,130 --> 01:26:21,320 is the side effect of hello, David. 1800 01:26:21,320 --> 01:26:26,260 So the fact that I nested these blocks on top of one another 1801 01:26:26,260 --> 01:26:27,680 was very much deliberate. 1802 01:26:27,680 --> 01:26:33,010 If I zoom in here, notice that hello and answer are on top of join, join 1803 01:26:33,010 --> 01:26:34,390 is on top of the say block. 1804 01:26:34,390 --> 01:26:36,140 And if you think back to high school math, 1805 01:26:36,140 --> 01:26:37,810 this is like when you had parentheses and you 1806 01:26:37,810 --> 01:26:41,060 had to do the things inside parentheses before the things outside parentheses. 1807 01:26:41,060 --> 01:26:44,920 It's the same idea, but I'm just visually stacking them instead. 1808 01:26:44,920 --> 01:26:49,797 But outputs can become inputs depending on what the function there expects. 1809 01:26:49,797 --> 01:26:51,880 Let me pause here and see if there's any questions 1810 01:26:51,880 --> 01:26:58,870 about not so much what the cat is doing, but how the cat is doing this. 1811 01:26:58,870 --> 01:27:02,180 Questions at hand? 1812 01:27:02,180 --> 01:27:02,790 All right. 1813 01:27:02,790 --> 01:27:06,270 Well, let's make the cat more cat-like and do this. 1814 01:27:06,270 --> 01:27:09,600 Let me throw away all the say block and just let go there. 1815 01:27:09,600 --> 01:27:12,200 And let me introduce at bottom left a nice feature of scratch 1816 01:27:12,200 --> 01:27:14,000 whereby there's also these extensions that 1817 01:27:14,000 --> 01:27:17,280 tend to use the cloud, the internet, to give you even more functionality. 1818 01:27:17,280 --> 01:27:20,520 And in fact, I'm going to click on this extension up here, text to speech. 1819 01:27:20,520 --> 01:27:23,750 And if I click on that, I suddenly get a whole new category 1820 01:27:23,750 --> 01:27:24,890 of blocks at the bottom. 1821 01:27:24,890 --> 01:27:25,950 Text to speech. 1822 01:27:25,950 --> 01:27:27,270 They happen to be green. 1823 01:27:27,270 --> 01:27:29,300 But what's nice here is that I can actually now 1824 01:27:29,300 --> 01:27:32,340 have the cat say something audibly. 1825 01:27:32,340 --> 01:27:36,180 So let me drag the speak block here instead of the say block. 1826 01:27:36,180 --> 01:27:38,280 I don't want it to just say hello. 1827 01:27:38,280 --> 01:27:39,240 Let me stop that. 1828 01:27:39,240 --> 01:27:41,340 So let me go back under operators. 1829 01:27:41,340 --> 01:27:44,430 Let me grab another join block, because I threw the other one away. 1830 01:27:44,430 --> 01:27:47,190 Let me change apple to hello, space again. 1831 01:27:47,190 --> 01:27:48,420 Let me go to sensing. 1832 01:27:48,420 --> 01:27:51,060 Let me drag answer to banana again. 1833 01:27:51,060 --> 01:27:55,880 And now let me hit the green flag and let me type in my name, D-A-V-I-D. 1834 01:27:55,880 --> 01:27:58,400 And in a moment I'll hit enter and. 1835 01:27:58,400 --> 01:27:59,822 COMPUTER: Hello, David. 1836 01:27:59,822 --> 01:28:00,780 DAVID MALAN: All right. 1837 01:28:00,780 --> 01:28:03,667 It's not exactly cat-like, but it was synthesized. 1838 01:28:03,667 --> 01:28:06,750 But it turns out under these text to speech blocks, there are some others. 1839 01:28:06,750 --> 01:28:10,570 Set voice to alto, for instance, seems to be the default. 1840 01:28:10,570 --> 01:28:11,740 But let's change this. 1841 01:28:11,740 --> 01:28:14,650 So notice that some puzzle pieces don't just take white ovals. 1842 01:28:14,650 --> 01:28:16,000 They might even have drop downs. 1843 01:28:16,000 --> 01:28:18,720 So whoever created that puzzle piece decided 1844 01:28:18,720 --> 01:28:23,315 in advance what the available choices are for that input per the dropdown. 1845 01:28:23,315 --> 01:28:25,440 So I'm going to change it to squeak, which sounds-- 1846 01:28:25,440 --> 01:28:27,640 or actually kitten sounds even more apt. 1847 01:28:27,640 --> 01:28:31,500 Let me zoom out, click the green flag, type my name. 1848 01:28:31,500 --> 01:28:33,030 D-A-V-I-D. Enter. 1849 01:28:33,030 --> 01:28:35,247 COMPUTER: Meow, meow. 1850 01:28:35,247 --> 01:28:36,580 DAVID MALAN: That's interesting. 1851 01:28:36,580 --> 01:28:38,380 So it doesn't seem to matter what I type. 1852 01:28:38,380 --> 01:28:40,620 So how about David Malan. 1853 01:28:40,620 --> 01:28:42,300 COMPUTER: Meow, meow, meow. 1854 01:28:42,300 --> 01:28:45,030 DAVID MALAN: So it seems to meow proportional to how long 1855 01:28:45,030 --> 01:28:46,980 the phrase is that I typed in. 1856 01:28:46,980 --> 01:28:48,520 It can get a little creepy quickly. 1857 01:28:48,520 --> 01:28:50,680 If I change kitten to giant. 1858 01:28:50,680 --> 01:28:52,210 Let me go ahead and hit Play. 1859 01:28:52,210 --> 01:28:54,690 D-A-V-I-D. Enter. 1860 01:28:54,690 --> 01:28:56,650 COMPUTER: Hello, David. 1861 01:28:56,650 --> 01:29:00,070 DAVID MALAN: So you can, for very non-academic ways, 1862 01:29:00,070 --> 01:29:02,260 start to have fun with this, but just playing around 1863 01:29:02,260 --> 01:29:03,843 with these various inputs and outputs. 1864 01:29:03,843 --> 01:29:06,992 But let's actually make the cat do something more cat-like and indeed meow 1865 01:29:06,992 --> 01:29:08,450 instead of saying any words at all. 1866 01:29:08,450 --> 01:29:10,090 So let me throw all of that away. 1867 01:29:10,090 --> 01:29:11,840 Let me go now under sound. 1868 01:29:11,840 --> 01:29:15,590 Let me drag the play sound until done. 1869 01:29:15,590 --> 01:29:18,760 And notice in the dropdown here, by default, you just get the cat sound. 1870 01:29:18,760 --> 01:29:20,240 You can record your own sounds. 1871 01:29:20,240 --> 01:29:21,970 There's a whole library of dogs and birds 1872 01:29:21,970 --> 01:29:24,920 and all sorts of sounds you can import into the program. 1873 01:29:24,920 --> 01:29:26,140 I'll keep it simple with cat. 1874 01:29:26,140 --> 01:29:28,360 And let me click the green flag. 1875 01:29:28,360 --> 01:29:29,092 COMPUTER: Meow. 1876 01:29:29,092 --> 01:29:30,050 DAVID MALAN: All right. 1877 01:29:30,050 --> 01:29:31,153 So the cat meowed once. 1878 01:29:31,153 --> 01:29:33,195 If I want the cat to meow again, I could do this. 1879 01:29:33,195 --> 01:29:34,540 COMPUTER: Meow. 1880 01:29:34,540 --> 01:29:37,735 DAVID MALAN: If I want the cat to meow a third time, I could again hit play. 1881 01:29:37,735 --> 01:29:38,360 COMPUTER: Meow. 1882 01:29:38,360 --> 01:29:40,930 DAVID MALAN: So this is kind of tedious if to play this game, 1883 01:29:40,930 --> 01:29:43,430 I have to keep clicking the button, keep clicking the button 1884 01:29:43,430 --> 01:29:46,050 to keep the cat alive virtually in this way. 1885 01:29:46,050 --> 01:29:48,890 So maybe I want this to happen again and again and again. 1886 01:29:48,890 --> 01:29:50,070 Well, let me just do that. 1887 01:29:50,070 --> 01:29:51,480 Let me sort of drag and drop. 1888 01:29:51,480 --> 01:29:54,140 Or I could right click or Control click and then 1889 01:29:54,140 --> 01:29:56,942 a little menu would let me Copy-Paste or duplicate blocks. 1890 01:29:56,942 --> 01:29:58,650 But I'll just keep dragging and dropping. 1891 01:29:58,650 --> 01:29:59,525 Let's do this. 1892 01:29:59,525 --> 01:30:00,150 COMPUTER: Meow. 1893 01:30:00,150 --> 01:30:00,990 Meow. 1894 01:30:00,990 --> 01:30:01,830 Meow. 1895 01:30:01,830 --> 01:30:03,760 DAVID MALAN: Cat's kind of hungry, unhappy. 1896 01:30:03,760 --> 01:30:06,610 So let's slow things down so it's adorable again. 1897 01:30:06,610 --> 01:30:09,640 So let me go under control. 1898 01:30:09,640 --> 01:30:13,150 Let me grab one of those wait one second, and I'll plop this here. 1899 01:30:13,150 --> 01:30:13,840 Another one. 1900 01:30:13,840 --> 01:30:14,950 Let me plop it here. 1901 01:30:14,950 --> 01:30:15,755 Click play again. 1902 01:30:15,755 --> 01:30:17,142 COMPUTER: Meow. 1903 01:30:17,142 --> 01:30:18,630 Meow. 1904 01:30:18,630 --> 01:30:19,530 DAVID MALAN: Cuter. 1905 01:30:19,530 --> 01:30:20,500 Less hungry. 1906 01:30:20,500 --> 01:30:21,030 Sure. 1907 01:30:21,030 --> 01:30:23,830 But this program is now, I daresay, correct 1908 01:30:23,830 --> 01:30:26,890 if my goal is to get the cat's meow three times. 1909 01:30:26,890 --> 01:30:31,210 But now, even if you've never programmed before, critique this program. 1910 01:30:31,210 --> 01:30:35,140 It is not well-designed, even though it is correct. 1911 01:30:35,140 --> 01:30:38,440 In other words, it could be better. 1912 01:30:38,440 --> 01:30:41,470 How, might you think? 1913 01:30:41,470 --> 01:30:42,042 Yeah? 1914 01:30:42,042 --> 01:30:42,750 AUDIENCE: A loop. 1915 01:30:42,750 --> 01:30:43,930 DAVID MALAN: So using a loop. 1916 01:30:43,930 --> 01:30:44,430 And why? 1917 01:30:44,430 --> 01:30:47,360 Why are you encouraging me to use a loop even though it works as is? 1918 01:30:47,360 --> 01:30:48,720 AUDIENCE: It's easier to plot. 1919 01:30:48,720 --> 01:30:49,470 DAVID MALAN: Yeah. 1920 01:30:49,470 --> 01:30:51,960 So to summarize, it's just easier to use a loop 1921 01:30:51,960 --> 01:30:55,220 because I could specify explicitly in one place how many times I want it 1922 01:30:55,220 --> 01:30:55,720 to loop. 1923 01:30:55,720 --> 01:31:00,060 And moreover, frankly, any time you are copying and pasting something in code 1924 01:31:00,060 --> 01:31:02,730 or dragging the same thing again and again, odds are you're 1925 01:31:02,730 --> 01:31:03,880 doing something foolish. 1926 01:31:03,880 --> 01:31:04,380 Why? 1927 01:31:04,380 --> 01:31:07,080 Because you're repeating yourself unnecessarily. 1928 01:31:07,080 --> 01:31:09,210 And this is a bit extreme, but suppose I want 1929 01:31:09,210 --> 01:31:13,830 to change this program later so that the cat pauses two seconds in between meows. 1930 01:31:13,830 --> 01:31:16,180 Well, obviously I can just go in here and do two. 1931 01:31:16,180 --> 01:31:17,500 But what if I forget? 1932 01:31:17,500 --> 01:31:20,710 And suppose this program isn't, like, five or six puzzle pieces. 1933 01:31:20,710 --> 01:31:23,590 Suppose it's 50 or 60 or 500 or 600. 1934 01:31:23,590 --> 01:31:26,410 Eventually I or a colleague I'm working with is going to screw up. 1935 01:31:26,410 --> 01:31:29,710 They're going to change a value in one place, forget to change it in another. 1936 01:31:29,710 --> 01:31:32,740 So why are you inviting the probability of making a mistake? 1937 01:31:32,740 --> 01:31:37,270 Just simplify things so that you only have to change inputs in one place. 1938 01:31:37,270 --> 01:31:38,440 So how can I do this? 1939 01:31:38,440 --> 01:31:39,340 Let me zoom out. 1940 01:31:39,340 --> 01:31:41,770 Let me throw most of this duplication away, 1941 01:31:41,770 --> 01:31:44,740 leaving me with just the play and the wait function. 1942 01:31:44,740 --> 01:31:48,040 Let me now, under control as well, grab one of these. 1943 01:31:48,040 --> 01:31:50,030 I could, for instance, repeat as follows. 1944 01:31:50,030 --> 01:31:51,350 Let me grab a repeat. 1945 01:31:51,350 --> 01:31:53,300 I'm going to have to move these in two parts. 1946 01:31:53,300 --> 01:31:54,592 So I'm going to move this down. 1947 01:31:54,592 --> 01:31:57,170 It's too small, but it will grow to fit the right shape. 1948 01:31:57,170 --> 01:31:59,360 Then let me reattach it up here. 1949 01:31:59,360 --> 01:32:01,670 Let me change the default 10 to a 3. 1950 01:32:01,670 --> 01:32:05,110 And now I think I've done exactly what you were encouraging, which is simplify. 1951 01:32:05,110 --> 01:32:06,560 And I click play now. 1952 01:32:06,560 --> 01:32:09,407 COMPUTER: Meow. 1953 01:32:09,407 --> 01:32:10,070 Meow. 1954 01:32:10,070 --> 01:32:11,720 DAVID MALAN: Now and. 1955 01:32:11,720 --> 01:32:12,420 COMPUTER: Meow. 1956 01:32:12,420 --> 01:32:13,170 DAVID MALAN: Yeah. 1957 01:32:13,170 --> 01:32:16,750 So still correct, but arguably better designed as a result. 1958 01:32:16,750 --> 01:32:19,830 I can keep things simple and change things now in just one place 1959 01:32:19,830 --> 01:32:21,257 and it will continue to work. 1960 01:32:21,257 --> 01:32:23,340 But this is getting a little tedious now, I claim. 1961 01:32:23,340 --> 01:32:26,700 Like, why am I implementing the idea of meowing? 1962 01:32:26,700 --> 01:32:30,480 Wouldn't MIT have been better to have just implemented 1963 01:32:30,480 --> 01:32:32,042 a meow puzzle piece for us? 1964 01:32:32,042 --> 01:32:34,000 Because the whole thing is themed around a cat. 1965 01:32:34,000 --> 01:32:35,770 Why is there not a meow puzzle piece? 1966 01:32:35,770 --> 01:32:39,910 Why do I need to go through all of this complexity to build that functionality? 1967 01:32:39,910 --> 01:32:41,888 Well, what's nice about Scratch and what's 1968 01:32:41,888 --> 01:32:43,680 nice about programming languages in general 1969 01:32:43,680 --> 01:32:47,740 is you can generally invent your own puzzle pieces, your own functions, 1970 01:32:47,740 --> 01:32:49,570 and then use and reuse them. 1971 01:32:49,570 --> 01:32:50,890 So let me go ahead and do this. 1972 01:32:50,890 --> 01:32:54,130 I'm going to go under my blocks in pink down here. 1973 01:32:54,130 --> 01:32:56,280 I'm going to go ahead and click make a block, 1974 01:32:56,280 --> 01:32:58,530 and I'm going to be prompted with this interface here. 1975 01:32:58,530 --> 01:33:01,488 And I'm going to call this block literally meow, because apparently MIT 1976 01:33:01,488 --> 01:33:03,130 forgot to implement it for us. 1977 01:33:03,130 --> 01:33:05,590 And I'm just going to go ahead and immediately click OK. 1978 01:33:05,590 --> 01:33:07,570 And what you'll see now is two things. 1979 01:33:07,570 --> 01:33:11,580 One, on the screen, I've been given this placeholder pink piece 1980 01:33:11,580 --> 01:33:14,780 that says define meow as follows. 1981 01:33:14,780 --> 01:33:17,980 So anything I attach to the bottom of that define block 1982 01:33:17,980 --> 01:33:20,840 is going to define the meaning of meowing. 1983 01:33:20,840 --> 01:33:23,900 And at top left, notice what I have under my blocks. 1984 01:33:23,900 --> 01:33:27,050 I now have a pink puzzle piece called meow 1985 01:33:27,050 --> 01:33:31,450 that is a new function that will do whatever that other block of code 1986 01:33:31,450 --> 01:33:33,102 tells the cat to do. 1987 01:33:33,102 --> 01:33:34,310 So what do I want to do here? 1988 01:33:34,310 --> 01:33:36,060 Well, I'm going to keep it simple for now. 1989 01:33:36,060 --> 01:33:40,355 I'm going to move the play sound meow until done and wait two seconds. 1990 01:33:40,355 --> 01:33:42,980 Though let's change it back to one second to move things along. 1991 01:33:42,980 --> 01:33:45,730 And now let me drag the meow puzzle piece 1992 01:33:45,730 --> 01:33:49,160 over to my loop such that now, what's it going to do? 1993 01:33:49,160 --> 01:33:50,750 It's going to meow three times. 1994 01:33:50,750 --> 01:33:54,410 And just to be dramatic, out of sight, out of mind. 1995 01:33:54,410 --> 01:33:57,090 Let me, for no technical reason, just drag 1996 01:33:57,090 --> 01:33:59,590 this all the way to the bottom of the screen and then scroll 1997 01:33:59,590 --> 01:34:04,340 back up just to make the point visually that now meowing exists. 1998 01:34:04,340 --> 01:34:07,820 That is an implementation detail that we can abstract away, 1999 01:34:07,820 --> 01:34:12,710 not caring how it exists, because I now know at a higher conceptual level, 2000 01:34:12,710 --> 01:34:16,090 if I want a meow, I just use the meow puzzle piece, and I or someone else 2001 01:34:16,090 --> 01:34:18,770 dealt with already how to implement meowing. 2002 01:34:18,770 --> 01:34:20,480 So now let me go ahead and hit play. 2003 01:34:20,480 --> 01:34:22,360 COMPUTER: Meow. 2004 01:34:22,360 --> 01:34:24,240 Meow. 2005 01:34:24,240 --> 01:34:25,180 Meow. 2006 01:34:25,180 --> 01:34:27,940 DAVID MALAN: OK, so same exact code, but arguably better 2007 01:34:27,940 --> 01:34:31,930 design because I've now given myself reusable code so I don't have 2008 01:34:31,930 --> 01:34:33,520 to Copy-Paste those several blocks. 2009 01:34:33,520 --> 01:34:35,510 I can just use meow again and again. 2010 01:34:35,510 --> 01:34:37,070 But let's make one refinement. 2011 01:34:37,070 --> 01:34:40,130 Let me actually scroll down to where I did in fact implement this. 2012 01:34:40,130 --> 01:34:42,460 Let me Control click or right click on it 2013 01:34:42,460 --> 01:34:45,250 and let me edit the pink block that I created 2014 01:34:45,250 --> 01:34:49,160 a moment ago, because I want to practice what I've been preaching about inputs. 2015 01:34:49,160 --> 01:34:51,500 So I don't want this function just to be called meow. 2016 01:34:51,500 --> 01:34:55,210 I want this function to also take an input, 2017 01:34:55,210 --> 01:34:58,810 and just for consistency with our use of n earlier, which in computer science 2018 01:34:58,810 --> 01:35:03,080 generally means number, let me meow n times. 2019 01:35:03,080 --> 01:35:06,710 And just so that this puzzle piece is even more programmer friendly, 2020 01:35:06,710 --> 01:35:10,690 let me add just a textual label that has no technical significance 2021 01:35:10,690 --> 01:35:13,780 other than to make this function read left to right 2022 01:35:13,780 --> 01:35:15,500 in a more English friendly way. 2023 01:35:15,500 --> 01:35:17,330 Meow n times. 2024 01:35:17,330 --> 01:35:18,405 Let me click OK. 2025 01:35:18,405 --> 01:35:22,150 And now notice this thing at the bottom has changed such 2026 01:35:22,150 --> 01:35:26,130 that it's not only called meow, there's explicit mention of n, 2027 01:35:26,130 --> 01:35:30,950 which is a circle, which is exactly the variable shape that we saw earlier 2028 01:35:30,950 --> 01:35:32,330 when it was called answer. 2029 01:35:32,330 --> 01:35:34,290 This is not a return value, though. 2030 01:35:34,290 --> 01:35:38,340 This is what, again, we're going to call an argument, an input to a function. 2031 01:35:38,340 --> 01:35:39,452 So let me do this. 2032 01:35:39,452 --> 01:35:41,160 I'm going to move this back up to the top 2033 01:35:41,160 --> 01:35:44,060 so I can see everything in one place, and I'm 2034 01:35:44,060 --> 01:35:47,090 going to make one modification, because my goal now 2035 01:35:47,090 --> 01:35:51,440 is to make a new and improved version of meowing that actually takes into account 2036 01:35:51,440 --> 01:35:54,150 how many times I want the cat to meow. 2037 01:35:54,150 --> 01:35:59,520 So instead of using a loop in my own program under when green flag clicked, 2038 01:35:59,520 --> 01:36:01,620 I'm going to detach this temporarily. 2039 01:36:01,620 --> 01:36:03,360 I'm going to move this away. 2040 01:36:03,360 --> 01:36:07,920 I'm going to move this code over here, and I'm going to reattach it here. 2041 01:36:07,920 --> 01:36:10,260 So focusing for the moment on just the left, 2042 01:36:10,260 --> 01:36:14,990 meow is now defined as repeating three times the following two functions. 2043 01:36:14,990 --> 01:36:16,605 Play sound and wait. 2044 01:36:16,605 --> 01:36:17,730 But that's not quite right. 2045 01:36:17,730 --> 01:36:19,260 I want to get rid of the three. 2046 01:36:19,260 --> 01:36:20,820 So what can I do? 2047 01:36:20,820 --> 01:36:25,530 Because I created this input to the meow function myself a moment ago, 2048 01:36:25,530 --> 01:36:28,140 I can actually drag a copy of it over right 2049 01:36:28,140 --> 01:36:31,530 that is change the three to be generally an n. 2050 01:36:31,530 --> 01:36:35,820 So now I have a function called meow that will meow any number of times. 2051 01:36:35,820 --> 01:36:41,250 And what's nice now is my actual program that is governed by that green flag, 2052 01:36:41,250 --> 01:36:45,960 I can type in three, I can type in 10, I can type in 100, and it will just work. 2053 01:36:45,960 --> 01:36:48,230 And henceforth, I can, again, dramatically 2054 01:36:48,230 --> 01:36:51,690 scroll this down so we don't know or care about it anymore. 2055 01:36:51,690 --> 01:36:55,400 Now my program is a single line whereby this notion of meowing 2056 01:36:55,400 --> 01:37:00,440 has been abstracted away by just defining my own function or custom 2057 01:37:00,440 --> 01:37:01,840 block. 2058 01:37:01,840 --> 01:37:04,840 Questions, then, about just this idea, this principle 2059 01:37:04,840 --> 01:37:09,580 of creating your own functions to hide implementation details once you've 2060 01:37:09,580 --> 01:37:10,430 solved a problem? 2061 01:37:10,430 --> 01:37:13,180 Therefore, you don't want to have to think about that same problem 2062 01:37:13,180 --> 01:37:13,960 ever again. 2063 01:37:13,960 --> 01:37:17,470 And that's the beauty of programming, typically. 2064 01:37:17,470 --> 01:37:22,780 Questions on what here we just did? 2065 01:37:22,780 --> 01:37:23,280 No? 2066 01:37:23,280 --> 01:37:23,920 All right. 2067 01:37:23,920 --> 01:37:24,840 Well, let's do this. 2068 01:37:24,840 --> 01:37:27,490 Let's now make this a little more interactive in code. 2069 01:37:27,490 --> 01:37:28,908 Let me go to this green flag. 2070 01:37:28,908 --> 01:37:31,200 Let me scroll down and just throw all of this hard work 2071 01:37:31,200 --> 01:37:34,408 away that we have copies on the courses website of all of these programs step 2072 01:37:34,408 --> 01:37:37,240 by step if you want to review them in slower detail. 2073 01:37:37,240 --> 01:37:37,950 Let's do this. 2074 01:37:37,950 --> 01:37:41,080 Under control, turns out there's other ways to loop. 2075 01:37:41,080 --> 01:37:44,260 There's this forever block that will just do something forever. 2076 01:37:44,260 --> 01:37:47,740 So in the forever block, there's some place for some other code. 2077 01:37:47,740 --> 01:37:50,310 And I'm going to move to the control section 2078 01:37:50,310 --> 01:37:54,280 here and grab one of these if blocks, so one of these conditionals. 2079 01:37:54,280 --> 01:37:55,630 Let's plug that in here. 2080 01:37:55,630 --> 01:38:00,480 And now notice if, and then there's this sort of trapezoid-like placeholder 2081 01:38:00,480 --> 01:38:03,000 that's going to probably fit what? 2082 01:38:03,000 --> 01:38:04,890 The if is a conditional. 2083 01:38:04,890 --> 01:38:06,780 Forever is a loop. 2084 01:38:06,780 --> 01:38:09,190 Say and so forth have been functions. 2085 01:38:09,190 --> 01:38:11,340 What was the other key term we used? 2086 01:38:11,340 --> 01:38:12,860 So a Boolean expression. 2087 01:38:12,860 --> 01:38:15,610 We need to put one of those yes, no or true, false questions here. 2088 01:38:15,610 --> 01:38:16,390 So what are those? 2089 01:38:16,390 --> 01:38:18,370 Well, I've been using Scratch for some years, 2090 01:38:18,370 --> 01:38:21,730 so I under sensing there's one of these shapes here. 2091 01:38:21,730 --> 01:38:23,840 Touching mouse pointer, question mark. 2092 01:38:23,840 --> 01:38:27,010 The question mark literally evokes the whole idea of a Boolean expression 2093 01:38:27,010 --> 01:38:27,950 being yes, no. 2094 01:38:27,950 --> 01:38:30,350 It's way too big to fit, but it is the right shape. 2095 01:38:30,350 --> 01:38:31,220 So let me drag it. 2096 01:38:31,220 --> 01:38:31,910 Let go. 2097 01:38:31,910 --> 01:38:33,440 It's going to grow to fill. 2098 01:38:33,440 --> 01:38:35,480 And now let me go to sound. 2099 01:38:35,480 --> 01:38:37,690 Let me grab that play sound, meow until done, 2100 01:38:37,690 --> 01:38:41,320 and put it inside that conditional such that what kind of program 2101 01:38:41,320 --> 01:38:44,710 have I just implemented here, arguably? 2102 01:38:44,710 --> 01:38:47,010 What will this program do when I click the green flag? 2103 01:38:47,010 --> 01:38:50,000 2104 01:38:50,000 --> 01:38:53,328 Well, nothing at the moment. 2105 01:38:53,328 --> 01:38:54,620 AUDIENCE: Not touching the cat. 2106 01:38:54,620 --> 01:38:55,680 DAVID MALAN: But I'm not touching the cat. 2107 01:38:55,680 --> 01:38:57,960 So if I move the mouse pointer to the cat. 2108 01:38:57,960 --> 01:38:59,418 COMPUTER: Meow. 2109 01:38:59,418 --> 01:39:00,210 DAVID MALAN: Again. 2110 01:39:00,210 --> 01:39:01,013 COMPUTER: Meow. 2111 01:39:01,013 --> 01:39:01,805 DAVID MALAN: Again. 2112 01:39:01,805 --> 01:39:02,610 COMPUTER: Meow. 2113 01:39:02,610 --> 01:39:05,860 DAVID MALAN: It's kind of implementing the idea of petting a cat, if you will, 2114 01:39:05,860 --> 01:39:08,380 because I'm forever just waiting and waiting and waiting. 2115 01:39:08,380 --> 01:39:12,280 Is the mouse pointer touching that sprite, touching that cat? 2116 01:39:12,280 --> 01:39:15,712 And only if so, go ahead and play that sound meow until done. 2117 01:39:15,712 --> 01:39:17,920 But now we can make things a little more interesting. 2118 01:39:17,920 --> 01:39:21,640 Let me stop this and let me do something actually completely different. 2119 01:39:21,640 --> 01:39:23,740 Let me throw all this hard work away. 2120 01:39:23,740 --> 01:39:25,390 Let me go under extensions. 2121 01:39:25,390 --> 01:39:28,690 Let me go to video sensing, because lots of laptops, my own included, 2122 01:39:28,690 --> 01:39:30,340 has a little webcam nowadays. 2123 01:39:30,340 --> 01:39:32,440 Let me approve use of that there. 2124 01:39:32,440 --> 01:39:34,030 And you can see me in the frame. 2125 01:39:34,030 --> 01:39:34,990 And let me do this. 2126 01:39:34,990 --> 01:39:38,470 Let me drag one of these when motion exceeds some measure. 2127 01:39:38,470 --> 01:39:41,770 And through trial and error, I figured out that 50 tends to work well. 2128 01:39:41,770 --> 01:39:45,340 Let me step out of frame here and program off to the side. 2129 01:39:45,340 --> 01:39:48,670 And if I go to play sound meow until done, 2130 01:39:48,670 --> 01:39:53,800 notice that this is an alternative to using when green flag clicked. 2131 01:39:53,800 --> 01:39:55,860 This is a category of block that's constantly 2132 01:39:55,860 --> 01:39:57,790 waiting for what we'll call an event. 2133 01:39:57,790 --> 01:40:01,670 An event is just something that can happen on the screen, a click, a drag, 2134 01:40:01,670 --> 01:40:03,530 a mouse movement, and so forth. 2135 01:40:03,530 --> 01:40:05,020 So let me zoom out here. 2136 01:40:05,020 --> 01:40:09,750 And now, if I can do this-- here we go. 2137 01:40:09,750 --> 01:40:12,160 No, too slow. 2138 01:40:12,160 --> 01:40:13,390 Still too slow. 2139 01:40:13,390 --> 01:40:14,410 Wait, did I click play? 2140 01:40:14,410 --> 01:40:14,910 Let's see. 2141 01:40:14,910 --> 01:40:17,700 2142 01:40:17,700 --> 01:40:20,434 Try again. 2143 01:40:20,434 --> 01:40:21,358 COMPUTER: Meow. 2144 01:40:21,358 --> 01:40:22,400 DAVID MALAN: There we go. 2145 01:40:22,400 --> 01:40:22,900 OK. 2146 01:40:22,900 --> 01:40:24,480 50 is a little too high, apparently. 2147 01:40:24,480 --> 01:40:26,650 So let's make this a little gentler. 2148 01:40:26,650 --> 01:40:27,150 10. 2149 01:40:27,150 --> 01:40:27,950 COMPUTER: Meow. 2150 01:40:27,950 --> 01:40:29,175 DAVID MALAN: OK, well. 2151 01:40:29,175 --> 01:40:29,800 COMPUTER: Meow. 2152 01:40:29,800 --> 01:40:30,842 DAVID MALAN: There we go. 2153 01:40:30,842 --> 01:40:31,504 COMPUTER: Meow. 2154 01:40:31,504 --> 01:40:32,546 DAVID MALAN: There we go. 2155 01:40:32,546 --> 01:40:33,020 COMPUTER: Meow. 2156 01:40:33,020 --> 01:40:36,270 DAVID MALAN: OK, so we've implemented now more physically the idea of actually 2157 01:40:36,270 --> 01:40:38,935 responding to petting a cat. 2158 01:40:38,935 --> 01:40:39,560 COMPUTER: Meow. 2159 01:40:39,560 --> 01:40:40,060 Meow. 2160 01:40:40,060 --> 01:40:41,300 DAVID MALAN: Oh, damn it. 2161 01:40:41,300 --> 01:40:41,800 OK. 2162 01:40:41,800 --> 01:40:42,858 COMPUTER: Meow. 2163 01:40:42,858 --> 01:40:43,358 Meow. 2164 01:40:43,358 --> 01:40:44,210 Meow. 2165 01:40:44,210 --> 01:40:44,520 DAVID MALAN: All right. 2166 01:40:44,520 --> 01:40:45,330 So this is a bug. 2167 01:40:45,330 --> 01:40:49,100 Like now-- this is MIT's fault. So it's not stopping in response 2168 01:40:49,100 --> 01:40:50,940 to the red stop sign. 2169 01:40:50,940 --> 01:40:52,200 So what do you do in doubt? 2170 01:40:52,200 --> 01:40:53,300 Most extreme, you reboot. 2171 01:40:53,300 --> 01:40:56,180 For now, I'm just going to close the window. 2172 01:40:56,180 --> 01:40:56,730 OK. 2173 01:40:56,730 --> 01:40:59,810 So now we've seen all of those primitives 2174 01:40:59,810 --> 01:41:03,395 that we saw in that pseudocode, but incarnated in this graphical programming 2175 01:41:03,395 --> 01:41:06,770 language, and again, without parentheses and semicolons and double quotes and all 2176 01:41:06,770 --> 01:41:09,060 that punctuation that we will introduce before long. 2177 01:41:09,060 --> 01:41:12,170 But for now, we have the mechanisms in place 2178 01:41:12,170 --> 01:41:14,340 where we can do some really interesting things. 2179 01:41:14,340 --> 01:41:18,060 So in fact, I thought, in the spirit of thinking back on olden times, 2180 01:41:18,060 --> 01:41:21,470 thought I'd open up the very first program I wrote when I actually took-- 2181 01:41:21,470 --> 01:41:24,140 I was cross-registered in an MIT class and took 2182 01:41:24,140 --> 01:41:27,840 a class that introduced aspiring teachers to Scratch. 2183 01:41:27,840 --> 01:41:30,420 And I implemented this program here called Oscartime, 2184 01:41:30,420 --> 01:41:33,630 which was a game that used a childhood song that I was a fan of 2185 01:41:33,630 --> 01:41:37,680 and it allows you to drag trash into a trash can. 2186 01:41:37,680 --> 01:41:40,640 But to bring this to life and perhaps in exchange for one stress ball, 2187 01:41:40,640 --> 01:41:43,760 could I get one brave volunteer who wants to come up and control this here 2188 01:41:43,760 --> 01:41:44,260 keyboard? 2189 01:41:44,260 --> 01:41:45,177 I saw your hand first. 2190 01:41:45,177 --> 01:41:46,150 Come on up. 2191 01:41:46,150 --> 01:41:49,480 Come on up. 2192 01:41:49,480 --> 01:41:53,440 And you'll see, thanks to the team, we also have this amazing lamppost here, 2193 01:41:53,440 --> 01:41:54,942 being on Quincy Street as we are. 2194 01:41:54,942 --> 01:41:56,900 Do you want to introduce yourself to the group? 2195 01:41:56,900 --> 01:41:58,960 AUDIENCE: Hi, my name is Anna. 2196 01:41:58,960 --> 01:42:01,855 I'm from Richmond, Virginia, and I'm in Weld. 2197 01:42:01,855 --> 01:42:02,605 DAVID MALAN: Nice. 2198 01:42:02,605 --> 01:42:02,920 Weld. 2199 01:42:02,920 --> 01:42:03,470 AUDIENCE: Yes! 2200 01:42:03,470 --> 01:42:05,012 DAVID MALAN: All right, come on over. 2201 01:42:05,012 --> 01:42:06,820 So here, Anna, you'll have a chance to play 2202 01:42:06,820 --> 01:42:09,520 the very first game I wrote in Scratch, which admittedly 2203 01:42:09,520 --> 01:42:11,368 is more complicated typically than we would 2204 01:42:11,368 --> 01:42:14,660 expect of a student doing this for the very first time, as in problem set zero. 2205 01:42:14,660 --> 01:42:16,610 But what I'm going to do is full screen this here. 2206 01:42:16,610 --> 01:42:18,880 I'm going to click the green flag, and what you'll see on the screen 2207 01:42:18,880 --> 01:42:19,838 are these instructions. 2208 01:42:19,838 --> 01:42:23,840 Drag as much falling trash as you can to Oscar's trashcan before his song ends. 2209 01:42:23,840 --> 01:42:26,099 And here we go. 2210 01:42:26,099 --> 01:42:28,877 [OSCAR THE GROUCH, "I LOVE TRASH"] 2211 01:42:28,877 --> 01:42:32,350 Oh, I love trash 2212 01:42:32,350 --> 01:42:36,570 Anything dirty or dingy or dusty 2213 01:42:36,570 --> 01:42:40,920 Anything ragged or rotten or rusty 2214 01:42:40,920 --> 01:42:44,620 Yes, I love trash 2215 01:42:44,620 --> 01:42:45,120 2216 01:42:45,120 --> 01:42:46,200 DAVID MALAN: There we go. 2217 01:42:46,200 --> 01:42:49,420 So as Anna continues to play, let's tease this apart a little bit. 2218 01:42:49,420 --> 01:42:51,780 So one, there's some costumes on the stage. 2219 01:42:51,780 --> 01:42:54,340 Like that lamppost is actually never going to move. 2220 01:42:54,340 --> 01:42:56,110 But there's a couple of sprites. 2221 01:42:56,110 --> 01:42:59,330 There's the trash can, which seems to be a character unto itself. 2222 01:42:59,330 --> 01:43:01,830 There's this piece of trash that keeps coming back and back. 2223 01:43:01,830 --> 01:43:02,920 That is a sprite. 2224 01:43:02,920 --> 01:43:05,710 There's now this sneaker, which is another sprite. 2225 01:43:05,710 --> 01:43:08,940 And in fact, notice that Oscar, of course, keeps popping up from his sprite 2226 01:43:08,940 --> 01:43:10,090 once in a while. 2227 01:43:10,090 --> 01:43:12,232 So Oscar seems to have multiple costumes. 2228 01:43:12,232 --> 01:43:14,940 So I offer this as an example, as you keep playing, if you would. 2229 01:43:14,940 --> 01:43:16,548 Very good job so far. 2230 01:43:16,548 --> 01:43:17,590 The song goes on forever. 2231 01:43:17,590 --> 01:43:20,340 This was a nightmare to implement, to listen to this all day long. 2232 01:43:20,340 --> 01:43:23,230 But how do we implement the rest of this? 2233 01:43:23,230 --> 01:43:26,400 Well, notice that the trash, every time she throws into the trash can, 2234 01:43:26,400 --> 01:43:28,760 does reappear somewhere different. 2235 01:43:28,760 --> 01:43:30,860 So there's some kind of randomness involved. 2236 01:43:30,860 --> 01:43:34,610 And indeed, Scratch will let you pick random numbers in a range. 2237 01:43:34,610 --> 01:43:36,610 So maybe it could be negative 240, maybe it 2238 01:43:36,610 --> 01:43:40,670 could be positive 240, at the 180 point on the top of the screen. 2239 01:43:40,670 --> 01:43:43,100 So you can randomly put things on the screen. 2240 01:43:43,100 --> 01:43:45,070 There's apparently what kind of construct 2241 01:43:45,070 --> 01:43:47,245 that makes the trash fall again and again. 2242 01:43:47,245 --> 01:43:48,620 I think no one's listening to me. 2243 01:43:48,620 --> 01:43:51,340 They're all just watching you. 2244 01:43:51,340 --> 01:43:54,910 What's making the trash fall from top to bottom? 2245 01:43:54,910 --> 01:43:58,210 So it's actually some kind of loop because there's a motion block 2246 01:43:58,210 --> 01:44:00,640 inside of a forever loop, probably, that just 2247 01:44:00,640 --> 01:44:04,340 keeps moving the trash one pixel, one pixel, one pixel, one pixel, one pixel, 2248 01:44:04,340 --> 01:44:06,680 creating the illusion, therefore, of motion. 2249 01:44:06,680 --> 01:44:09,740 And if we can crank the song a little bit more, 2250 01:44:09,740 --> 01:44:11,920 you'll see that this is all synchronized now. 2251 01:44:11,920 --> 01:44:15,362 OSCAR THE GROUCH: (SINGING) Because they're trash 2252 01:44:15,362 --> 01:44:19,180 Oh, I love trash 2253 01:44:19,180 --> 01:44:23,140 Anything dirty or dingy or dusty 2254 01:44:23,140 --> 01:44:24,560 Anything ragged-- 2255 01:44:24,560 --> 01:44:27,340 DAVID MALAN: The song keeps going forever, seemingly. 2256 01:44:27,340 --> 01:44:31,560 And now notice more and more sprites are appearing because they waited for-- 2257 01:44:31,560 --> 01:44:32,060 here we go. 2258 01:44:32,060 --> 01:44:32,620 Climax. 2259 01:44:32,620 --> 01:44:39,552 OSCAR THE GROUCH: (SINGING) I love trash 2260 01:44:39,552 --> 01:44:40,510 DAVID MALAN: All right. 2261 01:44:40,510 --> 01:44:42,670 A big round of applause for Anna. 2262 01:44:42,670 --> 01:44:43,960 Nicely done. 2263 01:44:43,960 --> 01:44:44,850 OK, here you go. 2264 01:44:44,850 --> 01:44:46,200 Here you go. 2265 01:44:46,200 --> 01:44:46,700 All right. 2266 01:44:46,700 --> 01:44:48,950 So this is an interminable song. 2267 01:44:48,950 --> 01:44:52,330 And indeed, I spent hours building that, and just listening to that song on loop 2268 01:44:52,330 --> 01:44:53,930 was not the best way to program. 2269 01:44:53,930 --> 01:44:58,630 But the goal here is to really use it as just an intellectual exercise as to how 2270 01:44:58,630 --> 01:44:59,660 that was implemented. 2271 01:44:59,660 --> 01:45:01,600 And we won't do the entire thing in detail, 2272 01:45:01,600 --> 01:45:04,175 because I will say back in the day when I was younger, 2273 01:45:04,175 --> 01:45:06,050 I didn't necessarily write the cleanest code. 2274 01:45:06,050 --> 01:45:10,480 And in fact, if we see inside this and we poke around the bottom of the screen 2275 01:45:10,480 --> 01:45:12,530 here, you can see all of my different sprites. 2276 01:45:12,530 --> 01:45:14,570 And the code is kind of complex. 2277 01:45:14,570 --> 01:45:16,430 Like, things just kind of escalated quickly. 2278 01:45:16,430 --> 01:45:19,090 But I did not set out and write all of these programs 2279 01:45:19,090 --> 01:45:20,630 all at once for each sprite. 2280 01:45:20,630 --> 01:45:22,910 I pretty much took baby steps, so to speak. 2281 01:45:22,910 --> 01:45:27,220 And so, for instance, let me open up just a few sample building blocks here 2282 01:45:27,220 --> 01:45:29,690 that speak to this that are written in advance. 2283 01:45:29,690 --> 01:45:31,040 So here's version zero. 2284 01:45:31,040 --> 01:45:33,350 Computer scientists typically start counting at zero. 2285 01:45:33,350 --> 01:45:37,430 And let me show you this example here that only has two sprites on the screen. 2286 01:45:37,430 --> 01:45:40,890 We have Oscar the trashcan and we have the piece of trash. 2287 01:45:40,890 --> 01:45:43,680 And now notice, what does Oscar do? 2288 01:45:43,680 --> 01:45:47,940 Well, let me go ahead and zoom in on this script, as it's called. 2289 01:45:47,940 --> 01:45:49,290 A program is a script. 2290 01:45:49,290 --> 01:45:53,160 When the green flag is clicked, Oscar switches his costume to Oscar one. 2291 01:45:53,160 --> 01:45:55,620 That's his default costume where the lid is closed. 2292 01:45:55,620 --> 01:45:57,870 Then Oscar does this forever. 2293 01:45:57,870 --> 01:46:03,390 If Oscar is touching the mouse pointer, change the costume to Oscar two, 2294 01:46:03,390 --> 01:46:05,880 otherwise change it back to Oscar one. 2295 01:46:05,880 --> 01:46:09,050 So that whole idea of animation where Oscar is popping in and out 2296 01:46:09,050 --> 01:46:11,360 is just like a quick costume change based 2297 01:46:11,360 --> 01:46:15,200 on a loop inside of which is a conditional waiting for the cursor, 2298 01:46:15,200 --> 01:46:18,000 like Anna did, to get near the trash can. 2299 01:46:18,000 --> 01:46:20,790 Meanwhile, if we look at the piece of trash here, 2300 01:46:20,790 --> 01:46:24,590 notice that the trash is actually not doing anything in this first version 2301 01:46:24,590 --> 01:46:27,000 because I didn't even implement falling first. 2302 01:46:27,000 --> 01:46:28,410 So let me hit the green flag. 2303 01:46:28,410 --> 01:46:30,750 Nothing is happening in this very first version. 2304 01:46:30,750 --> 01:46:35,070 But notice, if I click on the trash and drag as soon as I'm touching Oscar, 2305 01:46:35,070 --> 01:46:37,650 there comes that trash can lid. 2306 01:46:37,650 --> 01:46:42,280 And it was just the result of making this one program respond to that input. 2307 01:46:42,280 --> 01:46:42,780 All right. 2308 01:46:42,780 --> 01:46:43,950 What did I do next? 2309 01:46:43,950 --> 01:46:47,400 Well, next, after taking that single baby step, I added one other feature. 2310 01:46:47,400 --> 01:46:49,490 Let's see inside this version one. 2311 01:46:49,490 --> 01:46:52,050 Again, Oscar is behaving the exact same way. 2312 01:46:52,050 --> 01:46:55,890 But notice this time the trash is designed to do the following. 2313 01:46:55,890 --> 01:46:59,340 First, I'm telling the program that the drag mode is draggable. 2314 01:46:59,340 --> 01:47:02,730 That is, I want the trash to be movable when the user clicks on it. 2315 01:47:02,730 --> 01:47:06,410 Then I tell the piece of trash to go to a random x location. 2316 01:47:06,410 --> 01:47:10,860 x is the horizontal, so it's going somewhere between 0 and 240, 2317 01:47:10,860 --> 01:47:12,720 but all the way at the top of the screen. 2318 01:47:12,720 --> 01:47:13,710 180. 2319 01:47:13,710 --> 01:47:17,310 Then forever, the piece of trash just changes by negative one. 2320 01:47:17,310 --> 01:47:19,428 So it just moves down and down and down. 2321 01:47:19,428 --> 01:47:22,220 And without looking at the second script yet, let me just hit play. 2322 01:47:22,220 --> 01:47:25,450 And notice, without even doing anything-- and eventually, 2323 01:47:25,450 --> 01:47:27,200 once there was lots of trash falling, like 2324 01:47:27,200 --> 01:47:28,908 Anna was struggling to keep up with this. 2325 01:47:28,908 --> 01:47:33,170 It's just moving one pixel at a time forever until, thankfully, MIT 2326 01:47:33,170 --> 01:47:37,207 does stop things automatically if they hit the bottom, lest a six-year-old get 2327 01:47:37,207 --> 01:47:39,540 upset that all of a sudden their sprite is gone forever. 2328 01:47:39,540 --> 01:47:41,700 So there is some special casing there. 2329 01:47:41,700 --> 01:47:44,280 But what else is this trash doing? 2330 01:47:44,280 --> 01:47:45,740 Let me zoom in here. 2331 01:47:45,740 --> 01:47:49,710 The piece of trash also, when the green flag is clicked, 2332 01:47:49,710 --> 01:47:51,750 is forever asking this question. 2333 01:47:51,750 --> 01:47:57,590 If you are touching Oscar, then pick a new random location between 0 and 240 2334 01:47:57,590 --> 01:48:00,600 at positive 180 and go back to the top. 2335 01:48:00,600 --> 01:48:03,020 So in other words, as soon as this piece of trash 2336 01:48:03,020 --> 01:48:08,250 is dragged over to Oscar like this and I let go, it recreates itself at the top. 2337 01:48:08,250 --> 01:48:12,368 It's just sort of teleporting to the top, and thus was born this feature. 2338 01:48:12,368 --> 01:48:14,910 And I won't slog through all of the individual features here, 2339 01:48:14,910 --> 01:48:18,590 but if we do just one more and see inside this one-- so now let me go ahead 2340 01:48:18,590 --> 01:48:19,440 and hit Play. 2341 01:48:19,440 --> 01:48:21,890 Notice at the top left of the screen, there's a score. 2342 01:48:21,890 --> 01:48:23,100 Currently zero. 2343 01:48:23,100 --> 01:48:25,710 But now when I click the trash and let go, 2344 01:48:25,710 --> 01:48:28,230 notice that the score is being incremented by one. 2345 01:48:28,230 --> 01:48:30,230 And this, in fact, is how, Anna, your score kept 2346 01:48:30,230 --> 01:48:31,700 going higher and higher and higher. 2347 01:48:31,700 --> 01:48:35,790 Every time I noticed, oh, the trash is touching Oscar, let's not only teleport, 2348 01:48:35,790 --> 01:48:37,560 let's also increment a variable. 2349 01:48:37,560 --> 01:48:42,090 And we didn't see this before, but if I go to this Oscar Scratch now, 2350 01:48:42,090 --> 01:48:44,310 you'll see that it is exactly the same. 2351 01:48:44,310 --> 01:48:50,730 But if I now go to the trash piece here and we go to when green flag clicked, 2352 01:48:50,730 --> 01:48:54,990 you'll see that I'm initializing a variable in orange called score to zero. 2353 01:48:54,990 --> 01:48:57,920 But if we scroll down to the bottom, Oscar 2354 01:48:57,920 --> 01:49:00,960 is also doing another thing in parallel at the same time. 2355 01:49:00,960 --> 01:49:03,800 When the green flag is clicked, Oscar is forever checking, 2356 01:49:03,800 --> 01:49:07,940 is the piece of trash touching Oscar? 2357 01:49:07,940 --> 01:49:11,970 If so, change the score by one and then go to top, 2358 01:49:11,970 --> 01:49:14,345 which is another location on there, that screen. 2359 01:49:14,345 --> 01:49:16,970 So in other words, even though at a glance something like Oscar 2360 01:49:16,970 --> 01:49:20,070 time might look very complicated and it did take me hours, 2361 01:49:20,070 --> 01:49:21,930 the goal, especially with problem set zero, 2362 01:49:21,930 --> 01:49:24,360 is not going to be to bite off all of that at once, 2363 01:49:24,360 --> 01:49:26,340 but to take proverbial baby steps. 2364 01:49:26,340 --> 01:49:29,900 Implement one tiny feature so that you feel like you're making progress. 2365 01:49:29,900 --> 01:49:31,440 Add another feature, another. 2366 01:49:31,440 --> 01:49:33,590 And invariably you might run out of time and not 2367 01:49:33,590 --> 01:49:36,765 get to the best version of your vision, but hopefully it'll be good. 2368 01:49:36,765 --> 01:49:39,890 Hopefully it'll be better, but you'll have these sort of mental milestones, 2369 01:49:39,890 --> 01:49:41,682 hoping that you at least get to that point. 2370 01:49:41,682 --> 01:49:45,250 Because as you will soon discover, everything in the world of programming 2371 01:49:45,250 --> 01:49:48,170 unfortunately takes longer than you might expect. 2372 01:49:48,170 --> 01:49:52,430 That was true for me 25 years ago and is still true today. 2373 01:49:52,430 --> 01:49:55,430 Well, let me introduce one final set of examples here. 2374 01:49:55,430 --> 01:49:58,760 This one written by one of your own predecessors, a former student. 2375 01:49:58,760 --> 01:50:02,810 Let me go ahead and open up three baby steps, if you will, 2376 01:50:02,810 --> 01:50:07,420 toward an end of implementing a game called Ivy's Hardest Game, whereby 2377 01:50:07,420 --> 01:50:10,160 it's now more interactive, quite like Oscartime. 2378 01:50:10,160 --> 01:50:12,430 So at top right here, notice-- and I'll zoom 2379 01:50:12,430 --> 01:50:15,250 in-- we have this world that's initially very simple. 2380 01:50:15,250 --> 01:50:19,460 Two black lines, two walls, if you will, and a Harvard sprite in the middle. 2381 01:50:19,460 --> 01:50:21,880 But when you click the green flag, notice 2382 01:50:21,880 --> 01:50:25,280 that nothing happens initially except that the sprite jumps to the middle. 2383 01:50:25,280 --> 01:50:30,860 But I can hit the up key or the down key or the left key or the right key. 2384 01:50:30,860 --> 01:50:34,910 But if I try to go too far, even though it's not the edge of the world, 2385 01:50:34,910 --> 01:50:39,060 it's only touching that there black line, it's still going to stop as well. 2386 01:50:39,060 --> 01:50:42,420 So intuitively, how could you implement that type of program? 2387 01:50:42,420 --> 01:50:47,370 How could you get a sprite from what we've seen to respond to up, down, left, 2388 01:50:47,370 --> 01:50:51,727 right, but actually move when I touch my arrow keys? 2389 01:50:51,727 --> 01:50:53,060 Like, what does it mean to move? 2390 01:50:53,060 --> 01:50:54,060 Yeah? 2391 01:50:54,060 --> 01:50:55,665 AUDIENCE: Maybe if then. 2392 01:50:55,665 --> 01:50:56,540 DAVID MALAN: Exactly. 2393 01:50:56,540 --> 01:50:59,290 So much like with representing information, at the end of the day, 2394 01:50:59,290 --> 01:51:00,750 all we've got is zeros and ones. 2395 01:51:00,750 --> 01:51:02,760 When it comes to algorithms, at the moment, 2396 01:51:02,760 --> 01:51:06,080 all we have are functions and loops and conditionals 2397 01:51:06,080 --> 01:51:08,700 and Boolean expressions and soon some more things too. 2398 01:51:08,700 --> 01:51:11,250 But there's not all that much we have at our disposal. 2399 01:51:11,250 --> 01:51:14,120 So let me zoom out from this and let me actually show you 2400 01:51:14,120 --> 01:51:16,080 what the Harvard sprite is doing. 2401 01:51:16,080 --> 01:51:17,120 It's doing this. 2402 01:51:17,120 --> 01:51:21,210 When I go up to the green flag here, the Harvard sprite is going to 0, 0. 2403 01:51:21,210 --> 01:51:22,440 So dead center in the middle. 2404 01:51:22,440 --> 01:51:25,790 And then it's forever doing two things, listening for the keyboard 2405 01:51:25,790 --> 01:51:27,997 and feeling for walls, left and right. 2406 01:51:27,997 --> 01:51:30,330 Now, those are not puzzle pieces that come with Scratch. 2407 01:51:30,330 --> 01:51:35,520 I created my own custom blocks, my own functions to implement those ideas. 2408 01:51:35,520 --> 01:51:37,167 Let's not abstract away for now. 2409 01:51:37,167 --> 01:51:38,750 Let's actually look at these features. 2410 01:51:38,750 --> 01:51:41,220 And indeed, to your instincts at left here, 2411 01:51:41,220 --> 01:51:43,170 what does it mean to listen for the keyboard? 2412 01:51:43,170 --> 01:51:47,630 Well, if the up arrow key is pressed, change y by one. 2413 01:51:47,630 --> 01:51:48,420 Move up. 2414 01:51:48,420 --> 01:51:52,830 If the down arrow key is pressed, change y by negative one. 2415 01:51:52,830 --> 01:51:55,990 If the right arrow key is pressed, change x by one. 2416 01:51:55,990 --> 01:51:59,620 If the left arrow key is pressed, change x by negative one. 2417 01:51:59,620 --> 01:52:01,890 So take all the magic out of moving up, down, 2418 01:52:01,890 --> 01:52:05,650 left, right by just quantizing it as plus, minus, this, and that. 2419 01:52:05,650 --> 01:52:07,780 It's all numbers, indeed, at the end of the day. 2420 01:52:07,780 --> 01:52:09,340 But what else is it doing? 2421 01:52:09,340 --> 01:52:11,740 Notice that it did, indeed, bounce off the wall. 2422 01:52:11,740 --> 01:52:14,020 So my other custom function, which I chose, 2423 01:52:14,020 --> 01:52:17,920 feel for walls to evoke this idea, it's asking two questions. 2424 01:52:17,920 --> 01:52:21,580 If you're touching the left wall, then change x by one, 2425 01:52:21,580 --> 01:52:23,050 so bounce in the other direction. 2426 01:52:23,050 --> 01:52:27,160 Else if you're touching the right wall, bounce in the negative one direction. 2427 01:52:27,160 --> 01:52:30,247 And so what are left wall and right wall? 2428 01:52:30,247 --> 01:52:31,330 I mean, I kind of cheated. 2429 01:52:31,330 --> 01:52:32,770 I just used two more sprites. 2430 01:52:32,770 --> 01:52:35,860 These sprites are literally nothing except black lines. 2431 01:52:35,860 --> 01:52:40,270 But because they exist, I can ask that question in my conditional saying, 2432 01:52:40,270 --> 01:52:42,610 are you touching those other sprites? 2433 01:52:42,610 --> 01:52:46,200 And I could have colored them any way I want, but this is enough, if I zoom in, 2434 01:52:46,200 --> 01:52:49,350 to implement this idea of going up, down, left, and right, 2435 01:52:49,350 --> 01:52:53,810 and preventing the sprite from leaving that little world. 2436 01:52:53,810 --> 01:52:54,310 All right. 2437 01:52:54,310 --> 01:52:58,000 So if you'll agree that there's a way now to implement motion up, 2438 01:52:58,000 --> 01:53:00,660 down, left, right, let's go ahead and implement this idea 2439 01:53:00,660 --> 01:53:03,490 by adding a rival into the mix, like a Yale sprite. 2440 01:53:03,490 --> 01:53:07,207 And what the Yale sprite is going to do, if I click the green flag, is this. 2441 01:53:07,207 --> 01:53:10,290 So Harvard at the moment is still going to be movable with the arrow keys, 2442 01:53:10,290 --> 01:53:11,660 up, down, left, right. 2443 01:53:11,660 --> 01:53:13,410 But Yale, for better or for worse, is just 2444 01:53:13,410 --> 01:53:16,690 going to mindlessly bounce back and forth from left to right forever, 2445 01:53:16,690 --> 01:53:17,290 it would seem. 2446 01:53:17,290 --> 01:53:18,665 The operative word being forever. 2447 01:53:18,665 --> 01:53:19,660 So how is that working? 2448 01:53:19,660 --> 01:53:20,410 Well, let's look. 2449 01:53:20,410 --> 01:53:22,410 Here's the Yale sprite at the bottom. 2450 01:53:22,410 --> 01:53:25,240 Let's zoom in on its actual code here. 2451 01:53:25,240 --> 01:53:27,550 The Yale sprite starts at 0, 0. 2452 01:53:27,550 --> 01:53:31,270 It points in direction 90 degrees, which means left, right, essentially. 2453 01:53:31,270 --> 01:53:32,880 And then it forever does this. 2454 01:53:32,880 --> 01:53:38,260 If touching the left wall or touching the right wall, turn around 180 degrees. 2455 01:53:38,260 --> 01:53:40,590 So I don't want the Yale sprite to just stop 2456 01:53:40,590 --> 01:53:43,240 by moving it one pixel to bounce off slightly. 2457 01:53:43,240 --> 01:53:47,380 I want it to wrap around and just keep going and going and going forever. 2458 01:53:47,380 --> 01:53:48,220 And that's it. 2459 01:53:48,220 --> 01:53:49,540 Everything else is the same. 2460 01:53:49,540 --> 01:53:50,770 So one final flourish. 2461 01:53:50,770 --> 01:53:54,700 Let's add a more formidable adversary, like MIT here, 2462 01:53:54,700 --> 01:53:57,060 whereby if I zoom in and hit play, notice 2463 01:53:57,060 --> 01:54:01,535 that if I move the Harvard sprite, MIT comes chasing me now. 2464 01:54:01,535 --> 01:54:03,217 Now, how is this actually working? 2465 01:54:03,217 --> 01:54:05,800 Yale is just kind of doing its thing, bouncing back and forth. 2466 01:54:05,800 --> 01:54:10,630 Now MIT has really latched on to me and it's following me up, down, left, right. 2467 01:54:10,630 --> 01:54:12,215 So how is that logic now working? 2468 01:54:12,215 --> 01:54:14,340 Well, again, it's probably doing something forever, 2469 01:54:14,340 --> 01:54:16,270 because that's why it's continually doing it. 2470 01:54:16,270 --> 01:54:17,580 Let's click on MIT. 2471 01:54:17,580 --> 01:54:21,010 This too is pretty simple, even though it's a pretty fancy idea. 2472 01:54:21,010 --> 01:54:24,340 Initially the MIT sprite goes to a random position, 2473 01:54:24,340 --> 01:54:28,140 but thereafter, it forever points toward the Harvard logo outline, which 2474 01:54:28,140 --> 01:54:30,810 is just the long name that your predecessor or former student 2475 01:54:30,810 --> 01:54:32,890 gave the name for that sprite. 2476 01:54:32,890 --> 01:54:35,980 And then it moves one step, one step, one step. 2477 01:54:35,980 --> 01:54:38,580 So suppose this were an actual game, and in games things 2478 01:54:38,580 --> 01:54:41,410 get harder and harder, the adversary moves faster and faster. 2479 01:54:41,410 --> 01:54:46,040 How could we make MIT even faster by changing just one thing here? 2480 01:54:46,040 --> 01:54:48,610 2481 01:54:48,610 --> 01:54:51,070 Like, how do we level up? 2482 01:54:51,070 --> 01:54:54,740 Change the one to two pixels at a time, two steps at a time. 2483 01:54:54,740 --> 01:54:55,490 So let's see that. 2484 01:54:55,490 --> 01:54:56,657 Let's go ahead and zoom out. 2485 01:54:56,657 --> 01:54:57,440 Let's hit play. 2486 01:54:57,440 --> 01:55:01,380 And now notice that MIT is coming in much faster this time. 2487 01:55:01,380 --> 01:55:01,880 All right. 2488 01:55:01,880 --> 01:55:04,057 So it wasn't noticeably faster. 2489 01:55:04,057 --> 01:55:04,640 Let's do this. 2490 01:55:04,640 --> 01:55:06,170 Let's move 10 steps at a time. 2491 01:55:06,170 --> 01:55:08,200 So 10 steps faster than originally. 2492 01:55:08,200 --> 01:55:12,530 I mean, now-- and now notice it's kind of twitching back and forth in this way. 2493 01:55:12,530 --> 01:55:13,030 Why? 2494 01:55:13,030 --> 01:55:15,280 Well, probably, if we worked out the math, probably 2495 01:55:15,280 --> 01:55:18,437 the MIT sprite is touching the sprite and it's bouncing off of it, 2496 01:55:18,437 --> 01:55:20,270 but then it's realizing, oh, I went too far. 2497 01:55:20,270 --> 01:55:20,990 Let me move back. 2498 01:55:20,990 --> 01:55:21,410 Wait a minute. 2499 01:55:21,410 --> 01:55:22,130 I'm still touching it. 2500 01:55:22,130 --> 01:55:22,940 Let me move down. 2501 01:55:22,940 --> 01:55:24,815 So you can get into these perverse situations 2502 01:55:24,815 --> 01:55:27,582 where there is actually a bug, be it logical or aesthetical. 2503 01:55:27,582 --> 01:55:29,540 But in this case, we probably want to fix that. 2504 01:55:29,540 --> 01:55:32,840 So 10 is probably too fast for this to work particularly well. 2505 01:55:32,840 --> 01:55:37,300 But the final flourish here really is to show you the actual version of a game 2506 01:55:37,300 --> 01:55:40,780 that one of your predecessors, a past classmate, actually implemented. 2507 01:55:40,780 --> 01:55:43,150 Before, thereafter, we will adjourn for cake 2508 01:55:43,150 --> 01:55:45,410 in the transept, which is the CS50 tradition. 2509 01:55:45,410 --> 01:55:49,090 But can we get one more final volunteer to come on 2510 01:55:49,090 --> 01:55:51,648 up to play Ivy's Hardest Game? 2511 01:55:51,648 --> 01:55:53,690 I'm seeing your hand most enthusiastically there. 2512 01:55:53,690 --> 01:55:54,960 Yeah, come on down. 2513 01:55:54,960 --> 01:55:56,340 Very happily. 2514 01:55:56,340 --> 01:55:58,595 [APPLAUSE] 2515 01:55:58,595 --> 01:56:00,850 2516 01:56:00,850 --> 01:56:03,530 In just a moment, we will indeed adjourn. 2517 01:56:03,530 --> 01:56:06,670 But the goal here now is going to be to navigate a maze that's 2518 01:56:06,670 --> 01:56:09,100 a little more difficult than the last. 2519 01:56:09,100 --> 01:56:13,017 Let's have you first, though, introduce yourselves to your classmates in front. 2520 01:56:13,017 --> 01:56:13,850 AUDIENCE: Hi, y'all. 2521 01:56:13,850 --> 01:56:14,780 I'm Eric. 2522 01:56:14,780 --> 01:56:17,835 I'm from Philadelphia and I'm also from Hollis Hall. 2523 01:56:17,835 --> 01:56:18,920 [CHEERS] 2524 01:56:18,920 --> 01:56:20,420 DAVID MALAN: One person from Hollis. 2525 01:56:20,420 --> 01:56:20,920 Nice. 2526 01:56:20,920 --> 01:56:21,530 OK. 2527 01:56:21,530 --> 01:56:22,150 Welcome. 2528 01:56:22,150 --> 01:56:22,510 All right. 2529 01:56:22,510 --> 01:56:24,230 Eric, go ahead and take the keyboard here. 2530 01:56:24,230 --> 01:56:26,563 It, too, will be all about up, down, left, right as soon 2531 01:56:26,563 --> 01:56:28,700 as you click the green flag. 2532 01:56:28,700 --> 01:56:30,180 And if we can crank the music. 2533 01:56:30,180 --> 01:56:32,580 [MC HAMMER, "U CAN'T TOUCH THIS"] 2534 01:56:32,580 --> 01:56:33,960 You Can't touch this 2535 01:56:33,960 --> 01:56:36,990 DAVID MALAN: So notice, the black walls are a little more involved 2536 01:56:36,990 --> 01:56:38,005 than last time. 2537 01:56:38,005 --> 01:56:40,380 But the goal is to get to the sprite all the way at right 2538 01:56:40,380 --> 01:56:44,070 and just touch it, at which point you move to the next level. 2539 01:56:44,070 --> 01:56:49,170 The next level, of course, has Yale doing its thing back and forth. 2540 01:56:49,170 --> 01:56:51,060 You've made it to level three. 2541 01:56:51,060 --> 01:56:52,540 But now there's two Yale. 2542 01:56:52,540 --> 01:56:55,560 So another sprite is in the mix that's randomly 2543 01:56:55,560 --> 01:57:00,090 moving a little different in terms of direction. 2544 01:57:00,090 --> 01:57:00,630 Three Yales. 2545 01:57:00,630 --> 01:57:05,610 2546 01:57:05,610 --> 01:57:06,210 Next level. 2547 01:57:06,210 --> 01:57:07,180 MIT is in. 2548 01:57:07,180 --> 01:57:11,320 2549 01:57:11,320 --> 01:57:11,820 Nice. 2550 01:57:11,820 --> 01:57:17,080 2551 01:57:17,080 --> 01:57:18,075 The walls are now gone. 2552 01:57:18,075 --> 01:57:20,930 2553 01:57:20,930 --> 01:57:21,940 Princeton's in the mix. 2554 01:57:21,940 --> 01:57:31,880 2555 01:57:31,880 --> 01:57:34,460 Nice. 2556 01:57:34,460 --> 01:57:35,346 Two Princetons. 2557 01:57:35,346 --> 01:57:43,540 2558 01:57:43,540 --> 01:57:44,040 OK. 2559 01:57:44,040 --> 01:57:44,540 New life. 2560 01:57:44,540 --> 01:57:54,230 2561 01:57:54,230 --> 01:57:55,653 OK, another life. 2562 01:57:55,653 --> 01:57:56,153 Nice. 2563 01:57:56,153 --> 01:57:56,852 Nice. 2564 01:57:56,852 --> 01:57:57,352 Oh. 2565 01:57:57,352 --> 01:58:04,130 2566 01:58:04,130 --> 01:58:04,970 Nice. 2567 01:58:04,970 --> 01:58:06,200 Second to last level. 2568 01:58:06,200 --> 01:58:09,230 Three Princetons. 2569 01:58:09,230 --> 01:58:10,233 Last level. 2570 01:58:10,233 --> 01:58:15,450 2571 01:58:15,450 --> 01:58:15,950 Yeah! 2572 01:58:15,950 --> 01:58:18,840 2573 01:58:18,840 --> 01:58:19,810 Congratulations. 2574 01:58:19,810 --> 01:58:21,500 [APPLAUSE] 2575 01:58:21,500 --> 01:58:22,266 Thank you. 2576 01:58:22,266 --> 01:58:23,110 All right. 2577 01:58:23,110 --> 01:58:25,170 This, then, was CS50. 2578 01:58:25,170 --> 01:58:26,290 Welcome aboard. 2579 01:58:26,290 --> 01:58:29,280 Cake is now served. 2580 01:58:29,280 --> 01:58:32,030 [MUSIC PLAYING] 2581 01:58:32,030 --> 01:58:56,000 211814

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.