All language subtitles for aula_0

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
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: 1 00:01:15,020 --> 00:01:16,240 DAVID MALAN, All right. 2 00:01:17,260 --> 00:01:22,440 This is CS50, Harvard University's Introduction to the Intellectual 3 00:01:22,440 --> 00:01:26,380 of Computer Science and the Art of Programming. My name is David Malan, and 4 00:01:26,380 --> 00:01:30,460 actually took this class myself some years ago, but I almost didn't. It was 5 00:01:30,460 --> 00:01:34,500 first year in college, and my roommates and I were living in Matthews Hall, for 6 00:01:34,500 --> 00:01:35,500 those familiar. 7 00:01:35,700 --> 00:01:37,140 Nice, Matthews. 8 00:01:37,470 --> 00:01:41,190 Our claim to fame, actually, at the time was that our room was literally Matt 9 00:01:41,190 --> 00:01:42,850 Damon's just three years prior. 10 00:01:43,130 --> 00:01:44,990 So onward from that. 11 00:01:45,230 --> 00:01:49,410 But my first year, I didn't really have the nerves to set foot in this 12 00:01:49,410 --> 00:01:50,930 classroom, let alone computer science. 13 00:01:51,250 --> 00:01:54,910 In fact, for me, computer science, and CS50 in particular, was very much this 14 00:01:54,910 --> 00:01:56,210 class to beware. 15 00:01:56,630 --> 00:02:00,050 I was kind of comfortable with computers, but I certainly wasn't among 16 00:02:00,680 --> 00:02:02,200 more comfortable with computers. 17 00:02:02,540 --> 00:02:06,100 And so I shied away my first year. Instead, I actually took a whole lot of 18 00:02:06,100 --> 00:02:09,800 classes in government. In fact, in high school, I was really enjoying history. I 19 00:02:09,800 --> 00:02:13,360 loved this constitutional law class that I took my senior year of high school. 20 00:02:13,440 --> 00:02:16,220 And so I figured, okay, if that's what I liked in high school, and if that's 21 00:02:16,220 --> 00:02:20,000 where my comfort zone was, that's probably what I should be doing in 22 00:02:20,120 --> 00:02:23,520 And so I essentially declared as my concentration or major, government for 23 00:02:23,520 --> 00:02:25,080 first year, year and a half of school. 24 00:02:25,340 --> 00:02:29,520 But my sophomore year, when I was living actually in Mather House instead, 25 00:02:30,390 --> 00:02:31,710 OK, no one for Mather. 26 00:02:31,990 --> 00:02:37,350 In Mather House instead, it was, I sort of followed some friends, I think, the 27 00:02:37,350 --> 00:02:41,690 first week of class in September of that year to a class called CS50. And 28 00:02:41,690 --> 00:02:45,730 honestly, once I stepped foot in that classroom, the professor at the time was 29 00:02:45,730 --> 00:02:49,300 famous computer scientist by the name of Brian Kernaghan now at Princeton. 30 00:02:49,580 --> 00:02:54,460 I was hooked. And literally, would I go back thereafter on Friday evenings when 31 00:02:54,460 --> 00:02:58,600 at the time problem sets were released and sit down at 7, 8 PM on Friday nights 32 00:02:58,600 --> 00:03:02,820 and dive into homework, which isn't necessarily something we recommend per 33 00:03:02,940 --> 00:03:07,720 But for me, it was like this sign that, wow, I've sort of found my calling. I 34 00:03:07,720 --> 00:03:11,300 found my classmates here on campus. And that's not going to be the case for 35 00:03:11,300 --> 00:03:14,480 everyone. Certainly, we have no expectations that after taking one 36 00:03:14,480 --> 00:03:18,040 science class that you will or might want to take take others. But what's so 37 00:03:18,040 --> 00:03:22,860 empowering about computer science, and CS50 in particular, is it's so 38 00:03:22,860 --> 00:03:27,080 to the broader world, whether you're in the arts, humanities, social sciences, 39 00:03:27,160 --> 00:03:31,440 natural sciences, or beyond. It's just so applicable, the concepts and the 40 00:03:31,440 --> 00:03:35,480 practical programming skills with which you will exit a class like this. 41 00:03:35,760 --> 00:03:39,460 Now, it's going to be challenging along the way. And indeed, I put in my time 42 00:03:39,460 --> 00:03:41,760 back in the day, and even I did find it challenging. 43 00:03:42,000 --> 00:03:45,970 And here, for instance, is a photograph of a very famous MIT hack. so to speak, 44 00:03:46,030 --> 00:03:49,570 from down the road, whereby it communicates that getting an education 45 00:03:49,570 --> 00:03:54,290 is like trying to drink from a fire hose, which is to say there's going to 46 00:03:54,290 --> 00:03:58,150 much information, like so much new stuff, that certainly on any given day 47 00:03:58,150 --> 00:04:02,180 week, you're not going to be able to absorb all of it that first time around. 48 00:04:02,420 --> 00:04:06,240 But at the end of the day, it's through that challenge, putting the time in, 49 00:04:06,300 --> 00:04:10,960 that the returns are therefore just so much higher at the end of the course. 50 00:04:10,960 --> 00:04:13,920 indeed, will you walk out of the course with a much better understanding not 51 00:04:13,920 --> 00:04:17,880 only of computer science and programming, but ultimately how to teach 52 00:04:17,880 --> 00:04:20,140 new technologies and beyond. 53 00:04:20,380 --> 00:04:23,160 For the next three plus months, well, we have teaching fellows, teaching 54 00:04:23,160 --> 00:04:26,480 assistants, course assistants, and myself by your side sort of guiding you 55 00:04:26,480 --> 00:04:27,480 through the course's material. 56 00:04:27,600 --> 00:04:32,200 But the goal by term's end is to take those and leave those training wheels 57 00:04:32,200 --> 00:04:38,060 so that you're well equipped to sort of go teach yourself new ideas beyond the 58 00:04:38,060 --> 00:04:39,060 class itself. 59 00:04:39,420 --> 00:04:43,380 Take comfort, though, in knowing that most CS50 students have never taken a CS 60 00:04:43,380 --> 00:04:46,580 course before. And indeed, as per the syllabus, what ultimately matters in 61 00:04:46,580 --> 00:04:50,040 course is not so much where you end up relative to your classmates, but where 62 00:04:50,040 --> 00:04:52,840 you end up relative to yourself when you began. 63 00:04:53,100 --> 00:04:54,980 And when you began is thus today. 64 00:04:55,300 --> 00:04:59,000 And so consider just how comfortable or uncomfortable you are with computing, 65 00:04:59,120 --> 00:05:02,120 let alone computer science and programming, particularly if you've 66 00:05:02,120 --> 00:05:04,300 explored either in a classroom before. 67 00:05:04,540 --> 00:05:09,060 And consider the delta just a few months from now that will really describe how 68 00:05:09,060 --> 00:05:13,100 far you have come. And that is all that matters, not how much the student to the 69 00:05:13,100 --> 00:05:16,360 left, to the right, in front, or behind you right now knows. 70 00:05:16,920 --> 00:05:20,660 With that said, let me add some additional inspiration, if I may. 71 00:05:21,000 --> 00:05:25,840 Here's a photograph of my own very first homework assignment in CS50 from 1996. 72 00:05:26,600 --> 00:05:30,220 And I will draw your attention to the fact that even though this is a so 73 00:05:30,220 --> 00:05:35,390 Hello World program that we'll play with ourselves next week, it is pretty much 74 00:05:35,390 --> 00:05:39,770 literally the shortest, easiest program you can write in a programming language 75 00:05:39,770 --> 00:05:45,050 called C. I still somehow got minus 2 on my very first homework assignment, 76 00:05:45,190 --> 00:05:48,390 which is to say we're all going to make mistakes along the way, but the goal 77 00:05:48,390 --> 00:05:53,130 will be to learn and enjoy that process here on out. 78 00:05:53,430 --> 00:05:57,530 At the end of the day, too, like me, you'll exit with your own proudly held 79 00:05:57,530 --> 00:06:00,370 Hightook CS50 t -shirt, as is our tradition, too. 80 00:06:00,670 --> 00:06:05,150 With that said, there's so many other traditions within CS50. both on campus 81 00:06:05,150 --> 00:06:10,130 off? And in particular, do we try in CS50 to provide not only the academic 82 00:06:10,130 --> 00:06:13,190 support structure that you might want going through the class, but also a 83 00:06:13,190 --> 00:06:17,130 collective shared community experience? Which is to say, in just a few days, 84 00:06:17,230 --> 00:06:20,710 we'll start off the term formally with CS50 Puzzle Day, which is not only an 85 00:06:20,710 --> 00:06:24,990 opportunity to get together with friends with pizza and prizes and also logic 86 00:06:24,990 --> 00:06:28,650 puzzles of sorts, but really to send the message that computer science itself is 87 00:06:28,650 --> 00:06:31,830 not about programming. It's not about C. It's not about Python. It's not about 88 00:06:31,830 --> 00:06:35,660 programming languages per se. but about problem solving, ever more so 89 00:06:35,660 --> 00:06:40,180 collaboratively with other smart people by your side in this class or beyond. 90 00:06:40,380 --> 00:06:45,280 And indeed, toward the end of the semester, reinforcements of the same by 91 00:06:45,280 --> 00:06:48,600 a little something that we call the CS50 Hackathon, which will be an opportunity 92 00:06:48,600 --> 00:06:52,880 overnight to dive into your own final projects. The capstone of this course 93 00:06:52,880 --> 00:06:56,700 thereafter followed by the CS50 Fair, which will be a campus -wide exhibition 94 00:06:56,700 --> 00:07:01,240 for students, faculty, and staff across campus of your very own final projects, 95 00:07:01,360 --> 00:07:05,660 be it your very own web app or mobile app or anything else you decide to 96 00:07:05,660 --> 00:07:11,180 by term's end. And indeed, the sort of objective at the end of the day, truly 97 00:07:11,180 --> 00:07:15,020 with that final project in particular, is going to be to create for yourselves, 98 00:07:15,320 --> 00:07:19,680 for your classmates, for attendees, something that we didn't even teach you 99 00:07:19,680 --> 00:07:23,500 to do. And indeed, that will signal ultimately that you're indeed on your 100 00:07:23,500 --> 00:07:24,500 and ready. 101 00:07:25,320 --> 00:07:29,640 Toward that end, thought we would give you a sense of CS50's past by way of 102 00:07:29,640 --> 00:07:33,180 short video, if we could dim the light, that paints a picture of all that awaits 103 00:07:33,180 --> 00:07:35,220 here and beyond. 104 00:08:34,320 --> 00:08:38,110 All right DAVID J. All right, so welcome aboard to CS50 and computer science 105 00:08:38,110 --> 00:08:39,929 itself. So what is computer science? 106 00:08:40,190 --> 00:08:44,210 Well, put simply, it's the study of information, like how do you represent 107 00:08:44,210 --> 00:08:47,610 and how do you process it. But more fundamentally, what we'll teach in this 108 00:08:47,610 --> 00:08:51,870 class is computational thinking. That is to say, the application of ideas from 109 00:08:51,870 --> 00:08:55,650 computer science to problems of interest to us within the class and problems of 110 00:08:55,650 --> 00:08:57,530 interest to you after the class. 111 00:08:57,750 --> 00:09:01,810 And so at the end of the day, what computer science really is is about 112 00:09:01,810 --> 00:09:06,770 solving, ergo that sort of global applicability. And by problem solving, 113 00:09:06,770 --> 00:09:09,710 something quite simple. In fact, we can distill it as follows with this mental 114 00:09:09,710 --> 00:09:13,950 image. This is problem solving. You've got some problem to solve, thus known as 115 00:09:13,950 --> 00:09:17,810 the input that you want to solve. And the goal, of course, to problem solving 116 00:09:17,810 --> 00:09:19,050 to actually produce a solution. 117 00:09:19,270 --> 00:09:21,390 So the output in this model would be the solution. 118 00:09:22,010 --> 00:09:26,350 The interesting part, ultimately, is going to be in how you process that 119 00:09:26,350 --> 00:09:29,770 and turn it into that output, ergo solving problems. 120 00:09:30,190 --> 00:09:34,090 But before we can do that, we all kind of have to agree how to represent these 121 00:09:34,090 --> 00:09:37,650 inputs and outputs, especially if we want to do it in a standardized global 122 00:09:37,650 --> 00:09:42,250 using literally computers, be them laptops, desktops, phones, or most any 123 00:09:42,250 --> 00:09:43,610 kind of electronic device nowadays. 124 00:09:44,130 --> 00:09:48,090 So how can we do that? Well, there's different ways to represent information 125 00:09:48,090 --> 00:09:51,540 the world. For instance, if I were to take attendance, old school style, Maybe 126 00:09:51,540 --> 00:09:56,640 in a smaller room, I might do 1, 2, 3, 4, 5, 6, 7, and so forth, and just count 127 00:09:56,640 --> 00:09:58,220 people on my human hands. 128 00:09:58,520 --> 00:10:02,960 That's actually known as unary notation, otherwise mathematically known as base 129 00:10:02,960 --> 00:10:06,800 1, because you're using your fingers literally in this model as digits. 130 00:10:07,220 --> 00:10:11,400 But a little quick question. How high can you count with one human hand? 131 00:10:12,590 --> 00:10:18,910 5 is incorrect if you use a different base system than 1. So it's obviously 132 00:10:18,910 --> 00:10:22,610 correct if you're just using unary and just counting 1, 2, 3, 4, 5. 133 00:10:22,970 --> 00:10:28,510 But I dare say I can come up with more patterns in my human hand alone that 134 00:10:28,510 --> 00:10:32,430 would enable me, without a second hand or a couple of feet, to count higher 135 00:10:32,430 --> 00:10:36,950 5. In fact, maybe for those more comfortable, how high could you actually 136 00:10:36,950 --> 00:10:38,810 on a single human hand, perhaps? 137 00:10:39,790 --> 00:10:42,070 So 31, believe it or not. 138 00:10:42,360 --> 00:10:44,240 is, in fact, the correct answer. But why? 139 00:10:44,500 --> 00:10:48,380 Well, here I initially started pretty naively, 1, 2, 3, 4, 5. 140 00:10:48,640 --> 00:10:52,040 And I just kind of combined all of my fingers and counted the collective 141 00:10:52,320 --> 00:10:55,400 But what if I'm a little more clever and take into account the pattern of 142 00:10:55,400 --> 00:10:57,320 fingers that go up? So maybe this is still 0. 143 00:10:57,780 --> 00:10:58,780 This is 1. 144 00:10:59,310 --> 00:11:04,550 But now maybe we just agree universally that this is 2, even though it's just my 145 00:11:04,550 --> 00:11:05,550 single pointer finger. 146 00:11:05,630 --> 00:11:10,750 Maybe we all just agree that this is 3 with two fingers up. Maybe we agree that 147 00:11:10,750 --> 00:11:14,550 this is often offensive with just one middle finger up. But this would then be 148 00:11:14,550 --> 00:11:16,610 4. This could then be 5. 149 00:11:17,010 --> 00:11:18,350 This could then be 6. 150 00:11:18,650 --> 00:11:22,770 This could be 7. And if I keep permuting my fingers in this way, allow me to 151 00:11:22,770 --> 00:11:25,570 spoil it, this would be, in fact, 31. 152 00:11:26,600 --> 00:11:27,600 But again, why? 153 00:11:27,760 --> 00:11:31,500 But the difference here is that we're no longer using unary, or base 1, as a 154 00:11:31,500 --> 00:11:36,140 mathematician would say, but rather base 2. Because if we take into account not 155 00:11:36,140 --> 00:11:40,380 just the total number of fingers that I'm using, but whether each finger is 156 00:11:40,380 --> 00:11:46,520 or up, being therefore in two potential states, down, up, A, B, black, white, 157 00:11:46,560 --> 00:11:50,260 however you want to distinguish those two states of the world, you're now 158 00:11:50,260 --> 00:11:53,860 operating what's called base 2. And perhaps more familiarly, even if you're 159 00:11:53,860 --> 00:11:55,060 a computer person per se, 160 00:11:55,880 --> 00:11:57,440 the so -called binary system. 161 00:11:57,660 --> 00:12:01,280 And odds are, even if you're not a computer science person at all, you 162 00:12:01,280 --> 00:12:05,760 generally know that computers only understand or speak what alphabet, so to 163 00:12:05,760 --> 00:12:06,760 speak. 164 00:12:07,120 --> 00:12:11,720 So what's ones and zeros? Zeros and ones, otherwise known as the binary 165 00:12:11,880 --> 00:12:14,780 And in fact, there's a term of art here that's worth noting. When you're using 166 00:12:14,780 --> 00:12:19,380 zeros and ones, which of course are a total of two digits, you have binary 167 00:12:19,380 --> 00:12:23,040 digits, so to speak, by implying two, which means there's two possibilities, 168 00:12:23,240 --> 00:12:26,860 zero or one. If we actually get rid of some of these letters and then join 169 00:12:26,860 --> 00:12:31,360 two phrases, here you have a technical term that is a bit. A bit is just a 170 00:12:31,360 --> 00:12:34,680 binary digit, which is to say it's a zero or one. 171 00:12:34,900 --> 00:12:38,880 And this is in contrast, of course. with the system you and I know as the 172 00:12:38,880 --> 00:12:43,720 decimal system, dec implying 10, because in the real world, you and I daily use 173 00:12:43,720 --> 00:12:45,860 0 through 9, which is 10 possibilities. 174 00:12:46,360 --> 00:12:50,960 Computers only use 0 and 1, that is to say two bits, to represent information 175 00:12:50,960 --> 00:12:54,920 instead. So how do we represent this information, especially when at the end 176 00:12:54,920 --> 00:12:58,560 the day, what we're using are indeed computers and electronic devices? 177 00:12:58,920 --> 00:13:03,460 Well, if I want to represent 0, I can actually think of this as kind of 178 00:13:03,460 --> 00:13:04,800 analogous to the physical world. 179 00:13:05,930 --> 00:13:10,830 light bulb that's off or on, controlled by a switch that turns it off or on. So 180 00:13:10,830 --> 00:13:15,690 you can think of a binary digit that is a zero as really just being a light bulb 181 00:13:15,690 --> 00:13:16,690 that is off. 182 00:13:16,790 --> 00:13:22,370 By contrast, if you think of a one in the digital world as, of course, being 183 00:13:22,370 --> 00:13:26,390 second of two possibilities, you can think of that in the human or analog 184 00:13:26,610 --> 00:13:29,470 the physical world, as being a light bulb that is on. 185 00:13:29,690 --> 00:13:33,530 And in fact, what's inside of your Mac, your PC, your Android phone, your iPhone 186 00:13:33,530 --> 00:13:38,690 are millions of tiny little light switches known as transistors that just 187 00:13:38,690 --> 00:13:43,810 turned on or off, on or off. And essentially, you can use those 188 00:13:43,810 --> 00:13:44,669 store information. 189 00:13:44,670 --> 00:13:47,790 Because if you want to store a 0, you turn one of those switches off. If you 190 00:13:47,790 --> 00:13:50,170 want to store a 1, you turn one of those switches on. 191 00:13:50,710 --> 00:13:55,010 Of course, that sort of invites the question, well, how do we count higher 192 00:13:55,010 --> 00:13:56,030 0 or 1? 193 00:13:56,230 --> 00:14:00,470 Well, we would seem to need to use more than just maybe a single bit. 194 00:14:00,990 --> 00:14:05,310 a single light bulb. So if we wanted to count higher than, for instance, 0 or 1, 195 00:14:05,550 --> 00:14:07,430 why don't we go ahead and maybe do this? 196 00:14:07,690 --> 00:14:11,270 So just so I have some place to put these, let me borrow some of our actual 197 00:14:11,270 --> 00:14:13,810 physical light bulbs here from the stage. 198 00:14:14,370 --> 00:14:20,350 And let me propose that now with three bits on the stage, three light switches, 199 00:14:20,490 --> 00:14:24,210 three transistors, whatever metaphor you're most comfortable with, this is 200 00:14:24,210 --> 00:14:29,170 computer would represent a 0, because all of them are off. So it's off, off, 201 00:14:29,330 --> 00:14:30,319 off. 202 00:14:30,320 --> 00:14:33,920 But if a computer wanted to count to one, we could do naively this. 203 00:14:34,140 --> 00:14:38,160 We could turn this on. And if a computer wanted to represent two, we could do 204 00:14:38,160 --> 00:14:42,600 this. And if a computer wanted to represent three, we could do this. 205 00:14:43,240 --> 00:14:48,100 But I'm kind of painting myself into a corner and not using these light bulbs 206 00:14:48,100 --> 00:14:51,660 cleverly as I could, because at the moment, I've only counted as high as 207 00:14:51,840 --> 00:14:55,180 So if I want to count to four, to five, to six, I'm going to need more and more 208 00:14:55,180 --> 00:14:57,200 light bulbs. Can we be a little more clever? 209 00:14:57,420 --> 00:15:01,380 Well, again, someone else who's among those more comfortable, what's the 210 00:15:01,380 --> 00:15:07,840 here? How high, using binary zeros and ones, could I count with three light 211 00:15:07,840 --> 00:15:08,860 bulbs total? And back? 212 00:15:09,380 --> 00:15:12,670 Yeah. So 7 here is the answer. 213 00:15:12,910 --> 00:15:16,330 And if that's 2, you're sort of wondering, how are people figuring out 214 00:15:16,430 --> 00:15:20,170 That's the goal at hand here. So let me do this. Let me turn all of these off 215 00:15:20,170 --> 00:15:24,810 again so that my three light bulbs or switches, again, represent 0. 216 00:15:25,050 --> 00:15:28,870 And the first one's easy. This is how a computer would represent the number 1. 217 00:15:29,110 --> 00:15:31,970 It would be on, off, off. 218 00:15:32,330 --> 00:15:34,430 How, though, is the computer going to represent 2? 219 00:15:34,650 --> 00:15:38,810 Well, just like my proposed finger example, let's do this. Let's turn this 220 00:15:38,810 --> 00:15:44,690 off. and this one on, that is how a computer would represent 2, by saying 221 00:15:44,750 --> 00:15:46,070 on, off. 222 00:15:46,330 --> 00:15:49,910 In other words, 0, 1, 0 would be the way to pronounce it digitally. 223 00:15:50,210 --> 00:15:53,850 What if I instead want to represent 3? That's how on my finger I did this, with 224 00:15:53,850 --> 00:15:54,629 two fingers. 225 00:15:54,630 --> 00:15:57,310 Well, I'm going to turn this one on. This is 3. 226 00:15:57,590 --> 00:16:02,630 Now, this will, for those less comfortable, be non -obvious. This now 227 00:16:02,630 --> 00:16:05,210 would represent the number 4. 228 00:16:06,070 --> 00:16:08,230 This is how I would represent 5. 229 00:16:09,100 --> 00:16:11,720 This is how I would represent 6. 230 00:16:12,620 --> 00:16:18,200 And this, as per the spoiler, is how I would represent 7. So perhaps very non 231 00:16:18,200 --> 00:16:23,120 -obvious what it was I just did or why I chose those patterns, but I dare say if 232 00:16:23,120 --> 00:16:26,940 you sort of rewind in your mind's eye or literally later on video, you'll find 233 00:16:26,940 --> 00:16:31,940 that I actually did show you eight distinct patterns of light bulbs. The 234 00:16:31,940 --> 00:16:34,540 one was off, off, off. The last one was on, on, on. 235 00:16:34,820 --> 00:16:39,140 And there were another six total in between then. Well, wait, why seven? 236 00:16:39,180 --> 00:16:42,920 if you start counting at zero, And I claim there's eight possibilities. You 237 00:16:42,920 --> 00:16:45,640 only count from 0 to 7, as we will soon see. 238 00:16:45,880 --> 00:16:49,680 So how are these patterns coming about? And what is it that our computers are 239 00:16:49,680 --> 00:16:50,599 actually doing? 240 00:16:50,600 --> 00:16:54,700 Well, it's actually doing something a little like this, quite like in decimal. 241 00:16:54,920 --> 00:16:59,380 So in the human world, you and I are very much in the habit of using base 10, 242 00:16:59,380 --> 00:17:00,860 through 9, AKA decimal. 243 00:17:01,120 --> 00:17:04,440 Well, how do we use it instinctively as humans? 244 00:17:04,700 --> 00:17:06,540 Well, what's this number, obviously, on the screen? 245 00:17:08,119 --> 00:17:09,119 123. 246 00:17:09,450 --> 00:17:10,770 But why is it 123? 247 00:17:11,190 --> 00:17:15,530 For years, you haven't really thought about why this pattern of symbols or 248 00:17:15,530 --> 00:17:20,710 digits on the screen, 1, 2, 3, represents mathematically this number 249 00:17:20,710 --> 00:17:21,970 know, obviously, as 123. 250 00:17:22,410 --> 00:17:26,210 But if you rewind to grade school, odds are, like me, you were taught that the 251 00:17:26,210 --> 00:17:30,530 rightmost digit is in the ones column, this second digit is in the tens column, 252 00:17:30,770 --> 00:17:34,330 this digit is in the hundreds column, and so forth. So even though none of us 253 00:17:34,330 --> 00:17:38,510 have to do this math explicitly, what you're instantly doing is 100 times 1. 254 00:17:38,830 --> 00:17:44,970 plus 10 times 2 plus 1 times 3, which gives you 100 plus 20 plus 3. Oh, that's 255 00:17:44,970 --> 00:17:51,810 why it is 123, because these digits in this order have that significance. 256 00:17:52,210 --> 00:17:55,530 The digits to the left have more weight, so to speak, than the digits to the 257 00:17:55,530 --> 00:17:56,530 right. 258 00:17:56,570 --> 00:18:00,370 So what can we take away from this? Well, let's generalize it first as just 259 00:18:00,370 --> 00:18:03,670 three -digit number. So number, number, number. The ones column, the tens 260 00:18:03,670 --> 00:18:05,150 column, the hundreds column. 261 00:18:05,430 --> 00:18:08,210 But there's some math going on there. And it's not particularly sophisticated. 262 00:18:08,530 --> 00:18:10,270 Those are actually powers of 10. 263 00:18:10,510 --> 00:18:14,030 So 10 to the 0, 10 to the 1, 10 to the 2. 264 00:18:14,430 --> 00:18:19,830 And there's your decimal system. Because the base in this value is a 10, that's 265 00:18:19,830 --> 00:18:24,660 because there's 10 possibilities for each of those placeholders 0 through 9. 266 00:18:24,960 --> 00:18:29,060 But in the binary world, in the world of computers, where all they have are 0s 267 00:18:29,060 --> 00:18:33,020 and 1s, why? Because all they have physically is transistors, tiny, tiny, 268 00:18:33,020 --> 00:18:34,800 light bulbs that can be off or on. 269 00:18:35,320 --> 00:18:39,740 If you only have two digits to play with, the 10 base should, of course, 270 00:18:39,740 --> 00:18:40,740 a 2 base. 271 00:18:40,880 --> 00:18:45,820 And now if we do some math here, 2 to the 0, 2 to the 1, and 2 to the 2, you 272 00:18:45,820 --> 00:18:49,080 the ones column, the twos column, the fours column. 273 00:18:49,300 --> 00:18:55,240 And if we keep going, 8, 16, 32, 64, 128, and so forth, it's powers of 2 274 00:18:55,240 --> 00:18:56,240 of powers of 10. 275 00:18:56,580 --> 00:19:01,460 But this is to say computers represent information in exactly the same way you 276 00:19:01,460 --> 00:19:02,940 and I have since childhood. 277 00:19:03,660 --> 00:19:07,620 But they have fewer digits at their disposal, so these columns need to be 278 00:19:07,620 --> 00:19:11,600 weighted differently. So we can still count from 0 all the way up towards 279 00:19:11,600 --> 00:19:13,920 infinity. So what does this mean? 280 00:19:14,180 --> 00:19:18,040 Well, here we have three bits on the screen, 0, 0, 0. 281 00:19:18,320 --> 00:19:23,020 If we were to convert this now mentally or on paper pencil to decimal, how do we 282 00:19:23,020 --> 00:19:27,260 do it? Well, 4 times 0 plus 2 times 0 plus 1 times 0, that gives us the 283 00:19:27,260 --> 00:19:29,200 mathematical number you and I know as 0. 284 00:19:29,420 --> 00:19:32,240 That was three light bulbs off, off, off. 285 00:19:32,940 --> 00:19:36,240 Well, what if we turn on one light bulb all the way on the right? 286 00:19:36,740 --> 00:19:41,440 What decimal number does this binary number 001 represent? 287 00:19:42,680 --> 00:19:46,180 Just 1, because it's 4 times 0, 2 times 0, 1 times 1. 288 00:19:46,400 --> 00:19:49,820 Here's where things got more interesting, even if non -obvious in 289 00:19:49,820 --> 00:19:51,500 form or even physical hand form. 290 00:19:52,360 --> 00:19:54,560 010 in binary is what in decimal? 291 00:19:55,480 --> 00:19:57,780 2, because it's 2 times 1, and that's it. 292 00:19:58,540 --> 00:20:00,720 011 in binary is, of course, now. 293 00:20:01,649 --> 00:20:08,610 3, this is now 4, this is now 5, this is now 6, and 7, on, on, on, or 1, 1, 1, 294 00:20:08,710 --> 00:20:11,670 is the highest we can count with these three bits. 295 00:20:11,930 --> 00:20:15,010 All right, so how might a computer intuitively count as high as 8? 296 00:20:16,050 --> 00:20:17,390 What do you need to do, presumably? 297 00:20:18,710 --> 00:20:21,550 You're going to need to add a bit. So you need another light bulb, another 298 00:20:21,550 --> 00:20:25,110 switch. You need more memory, so to speak, to use nomenclature with which 299 00:20:25,110 --> 00:20:25,949 probably familiar. 300 00:20:25,950 --> 00:20:30,110 So in fact, if we change all of those to 0, but we give ourselves one more bit 301 00:20:30,110 --> 00:20:33,670 for a total of 4, ah, that's got to be the 8th place, because it's just another 302 00:20:33,670 --> 00:20:34,589 power of 2. 303 00:20:34,590 --> 00:20:40,570 So 1, 0, 0, 0 is the decimal number 8. You don't say 1 ,000 in binary. You 304 00:20:40,570 --> 00:20:42,370 literally say 1, 0, 0, 0. 305 00:20:42,610 --> 00:20:46,150 But that is the number you and I know as 8. And you can keep going up and up and 306 00:20:46,150 --> 00:20:47,690 up. And how, then, computers? 307 00:20:48,170 --> 00:20:52,690 Excel or any kind of number crunching software counts really high and keep 308 00:20:52,690 --> 00:20:57,150 of really big numbers, the computer just throws more and more transistors at it, 309 00:20:57,170 --> 00:21:01,930 more and more bits to count higher and higher and higher than this. 310 00:21:02,290 --> 00:21:06,590 Turns out, though, one bit, three bits, even four bits aren't that useful in 311 00:21:06,590 --> 00:21:11,710 practice because literally you can count as high as seven or maybe 15 or 31. 312 00:21:12,010 --> 00:21:16,250 So more commonly, as is commonly known, is to use a byte. 313 00:21:16,760 --> 00:21:17,760 of bits instead. 314 00:21:18,240 --> 00:21:20,420 How many bits is in a byte for those familiar? 315 00:21:20,920 --> 00:21:26,120 So it's just 8. Why 8? It's just more useful than 1 or 2 or 3 or some other 316 00:21:26,120 --> 00:21:29,480 number. And as an aside, it happens to be a power of 2, which is just useful 317 00:21:29,480 --> 00:21:30,840 electronically as well. 318 00:21:31,040 --> 00:21:32,900 So a byte, then, is just 8 bits. 319 00:21:33,200 --> 00:21:36,400 And here are those columns I rattled off off the top of my head. 320 00:21:36,620 --> 00:21:40,920 Here is how a computer would represent 0 in decimal, but using 8. 321 00:21:41,340 --> 00:21:42,680 binary digits or bits. 322 00:21:42,920 --> 00:21:46,260 A little trivia, and again, this is not what computer science is about, but it 323 00:21:46,260 --> 00:21:50,280 helps to know the lower bounds and the upper bounds of these kinds of values. 324 00:21:50,660 --> 00:21:55,200 How high can you count with 8 bits or 1 byte if this is 0? 325 00:21:57,600 --> 00:22:02,940 Yeah, so it's actually 255. So if I were to change all of these 0s to 1s and 326 00:22:02,940 --> 00:22:09,080 then do some quick mental or calculator math, 128 plus 64 plus 32, 16, 8, 4, 2, 327 00:22:09,120 --> 00:22:15,480 and 1 would actually give me 255 total, plus 0, which gives me 256 328 00:22:15,480 --> 00:22:17,400 total possibilities. 329 00:22:17,880 --> 00:22:21,500 So this is only to say, this is not, again, the kind of math we'll frequently 330 00:22:21,500 --> 00:22:25,590 do, but you'll commonly see in the computer world and programming world, 331 00:22:25,590 --> 00:22:28,850 of two, numbers like 255, 256. Why? 332 00:22:29,170 --> 00:22:33,530 Because these are the common units of measures that systems tend to use. 333 00:22:33,930 --> 00:22:40,130 So let me pause here and see, with respect to binary, zeros, ones, 334 00:22:40,190 --> 00:22:44,330 and the like, any questions or confusion we can clear up. 335 00:22:46,190 --> 00:22:52,530 A really good question. Why are bits just on or off instead of maybe 0%, 336 00:22:52,530 --> 00:22:57,590 50%, 100 % by playing with voltages? So the voltage inference of yours is 337 00:22:57,590 --> 00:22:58,289 actually correct. 338 00:22:58,290 --> 00:22:59,610 That's what computers typically do. 339 00:22:59,810 --> 00:23:05,070 Maybe they use 0 -ish volts to represent 0, maybe 5 -ish volts to represent 340 00:23:05,070 --> 00:23:10,750 1. It turns out it's just really easy to do extremes in computers. 341 00:23:10,970 --> 00:23:15,150 If you start to split that range of voltage levels, for those who remember 342 00:23:15,150 --> 00:23:17,970 of their electricity It just gets harder and harder to be exact. 343 00:23:18,230 --> 00:23:23,070 And if you get things a little too murky, you might mistake a 0 for a 1 or 344 00:23:23,070 --> 00:23:27,910 or a 3. So it turns out it's just simpler to use the binary system. But 345 00:23:27,910 --> 00:23:33,300 exist computers known as ternary computers that actually use three 346 00:23:33,480 --> 00:23:37,300 and 2, which is somewhere, of course, between binary and decimal. But you can 347 00:23:37,300 --> 00:23:40,480 different things. It's just simple on and off. And case in point, I don't want 348 00:23:40,480 --> 00:23:43,400 to really be dramatic and turn off my computer, but if I pulled out the power 349 00:23:43,400 --> 00:23:46,400 plug, that could be off, literally, aka 0. 350 00:23:46,640 --> 00:23:51,260 Plug it back in, that's a 1. There's just a cleanliness and simplicity to 351 00:23:51,880 --> 00:23:56,460 Other questions or confusion that we can clear up? 352 00:23:57,060 --> 00:23:58,500 No? OK. 353 00:23:58,800 --> 00:24:04,460 So if you're in agreement for the moment that, OK, using just zeros and ones, we 354 00:24:04,460 --> 00:24:08,880 can represent any number we want from zero on up, let me propose that we do 355 00:24:08,880 --> 00:24:12,980 useful things with our computers and our pockets and desktops and laptops, like 356 00:24:12,980 --> 00:24:16,680 represent letters for the sake of Google Docs, Microsoft Word, or any kind of 357 00:24:16,680 --> 00:24:19,460 text that we might want to write. 358 00:24:19,800 --> 00:24:25,500 So knowing now that computers only use zeros and ones and therefore only 359 00:24:26,200 --> 00:24:30,320 hardware like transistors, how could you represent something like a capital 360 00:24:30,320 --> 00:24:35,240 letter A in English inside of a computer, which, of course, is not a 361 00:24:35,240 --> 00:24:36,240 anymore? 362 00:24:36,880 --> 00:24:38,080 What could we do? Yeah. 363 00:24:41,230 --> 00:24:44,770 OK, yeah, so we could take the alphabet, A through Z in English, and we could 364 00:24:44,770 --> 00:24:48,270 just assign each letter a number. And honestly, that is not only the correct 365 00:24:48,270 --> 00:24:51,770 answer, it's really the only answer. Because at the end of the day, if all 366 00:24:51,770 --> 00:24:57,650 have are 0s and 1s available to you, that is the entirety of the potential 367 00:24:57,650 --> 00:24:59,130 solution to this problem. 368 00:24:59,390 --> 00:25:03,690 So it turns out that, yes, capital letter A, some years ago, was decided by 369 00:25:03,690 --> 00:25:08,170 bunch of people in a room, shall be represented with this pattern of 0s and 370 00:25:08,170 --> 00:25:10,630 0, 1, 0, 0, 0, 0, 0. 371 00:25:11,490 --> 00:25:15,810 And now, trained as you are to do a bit of quick binary math, what decimal 372 00:25:15,810 --> 00:25:18,130 number is used to represent, apparently, capital A? 373 00:25:18,890 --> 00:25:21,110 So 65, because it's 64 plus 1. 374 00:25:21,720 --> 00:25:23,480 plus 1 times 1 is 65. 375 00:25:23,920 --> 00:25:25,600 What is b? Turns out it's 66. 376 00:25:25,900 --> 00:25:26,900 What is c? 67. 377 00:25:27,200 --> 00:25:29,000 So they kept things simple there on out. 378 00:25:29,200 --> 00:25:33,540 It might have been nice if a were 0 or maybe a were 1. But nope, we're stuck 379 00:25:33,540 --> 00:25:34,580 with 65 instead. 380 00:25:34,800 --> 00:25:37,340 But everything after that is very much predictable. 381 00:25:37,620 --> 00:25:42,460 And in fact, for lowercase letters, there's a whole other set of numbers, 382 00:25:42,460 --> 00:25:48,800 as lowercase a happens to be 97, lowercase b happens to be 98, and so 383 00:25:48,800 --> 00:25:50,380 again, this is sort of like CS trivia. 384 00:25:51,080 --> 00:25:56,240 What's important here is that they're indeed contiguous from 65 to 66 to 67 on 385 00:25:56,240 --> 00:25:59,820 up. That's something we're going to be able to leverage beyond the letter A 386 00:25:59,820 --> 00:26:03,140 alone. What is this system? What is this mapping that you yourself proposed? 387 00:26:03,440 --> 00:26:06,860 It's ASCII, the American Standard Code for Information Interchange. And indeed, 388 00:26:06,960 --> 00:26:10,040 it was a bunch of Americans years ago who came up with this system. 389 00:26:10,520 --> 00:26:15,880 Unfortunately, at the time, they only allocated seven and eventually eight 390 00:26:15,880 --> 00:26:16,880 total. 391 00:26:17,590 --> 00:26:22,130 for representing letters, both uppercase and lowercase, numbers on the keyboard 392 00:26:22,130 --> 00:26:26,090 as well, punctuation symbols as well. And so per our conversation a moment 393 00:26:26,350 --> 00:26:32,130 if the Americans in this room, so to speak, only used 8 -bit total, how many 394 00:26:32,130 --> 00:26:35,830 different characters can we represent with a computer in this story? 395 00:26:37,580 --> 00:26:42,600 So only 255, technically 256 if we, again, start counting from zero. 396 00:26:42,800 --> 00:26:48,020 So that's not nearly enough to represent all human languages, but it is enough 397 00:26:48,020 --> 00:26:50,480 to represent at least English among some others. 398 00:26:50,680 --> 00:26:53,420 So here, for instance, is a chart of the ASCII mapping. 399 00:26:53,660 --> 00:26:58,560 And sure enough, if we zoom in on this column here, 65 is capital A, 66 is 400 00:26:58,560 --> 00:27:03,320 capital B, dot, dot, dot, 72 is H, 73 is I, and so forth. 401 00:27:03,930 --> 00:27:07,390 So there is a standardized mapping for at least all of these English letters. 402 00:27:07,650 --> 00:27:12,250 Well, suppose you were to receive an email or a text message or like a Google 403 00:27:12,250 --> 00:27:15,210 Doc containing this pattern of zeros and ones. 404 00:27:15,490 --> 00:27:21,510 So 0, 1, 0, 0, 1, 0, 0, 0, and so forth and so forth. So three bytes worth. 405 00:27:21,890 --> 00:27:28,070 Three sets of eight bits, that is to say three bytes, each of which represents a 406 00:27:28,070 --> 00:27:29,810 single letter in ASCII. 407 00:27:31,800 --> 00:27:34,620 message have you received? Well, I'll do the math this time, so we don't have 408 00:27:34,620 --> 00:27:39,820 to. Suppose what you really received was decimal 72, 73, 33. 409 00:27:40,680 --> 00:27:42,580 What message did you just receive? 410 00:27:43,700 --> 00:27:45,480 If you recall the previous chart. 411 00:27:46,040 --> 00:27:51,040 High was, in fact, correct. Why? Because H is 72, I is 73, and wait a minute, 412 00:27:51,120 --> 00:27:53,040 33. So here's H, here's I. 413 00:27:53,320 --> 00:27:58,600 33, if we highlight it instead, happens to be an exclamation point. So that is 414 00:27:58,600 --> 00:28:02,540 literally what is going on underneath the hood, so to speak, when you get a 415 00:28:02,540 --> 00:28:05,660 message today that literally says in all caps and an exclamation point. 416 00:28:06,060 --> 00:28:10,840 Hi, your phone has received at least three bytes, each of which represents a 417 00:28:10,840 --> 00:28:11,840 letter of the alphabet. 418 00:28:11,980 --> 00:28:15,060 Your computer is quickly doing the mental math to figure out exactly what 419 00:28:15,060 --> 00:28:18,780 numbers those are and then looking up in the so -called ASCII chart in its 420 00:28:18,780 --> 00:28:23,540 memories, in some sense, what letter should you actually see on the screen 421 00:28:23,540 --> 00:28:29,780 there. And so if you were to then display that message, you would see it 422 00:28:29,780 --> 00:28:32,880 in English as opposed to those numeric equivalents. 423 00:28:34,519 --> 00:28:38,180 How else might we use this then? Well, here again is that chart. 424 00:28:38,400 --> 00:28:41,280 And maybe just to vary things, maybe take a little pressure off of me here, 425 00:28:41,280 --> 00:28:45,000 don't we try spelling something else, this time a different three -letter 426 00:28:45,240 --> 00:28:46,680 but maybe eight volunteers. 427 00:28:46,900 --> 00:28:48,380 Could we get a bite's worth of volunteers? 428 00:28:48,640 --> 00:28:51,440 And I can sweeten the deal with some stress balls in exchange. 429 00:28:51,640 --> 00:28:54,400 You just have to be comfortable coming up on stage and being on the internet. 430 00:28:54,400 --> 00:29:01,400 yes, one, two, how about three, four, how about five, six, seven, and how 431 00:29:01,400 --> 00:29:04,270 eight? Come on up. A round of applause for our volunteers. 432 00:29:05,190 --> 00:29:12,070 All right. 433 00:29:12,150 --> 00:29:16,490 So what I'm going to have each of you do is represent a bit in a particular 434 00:29:16,490 --> 00:29:20,390 order. So if you want to just, in any order, line yourselves up here facing 435 00:29:20,390 --> 00:29:21,990 audience. Come on over. 436 00:29:23,490 --> 00:29:24,449 All right. 437 00:29:24,450 --> 00:29:25,630 And we will have you represent. 438 00:29:25,850 --> 00:29:27,070 Well, we've got to see who ends up where. 439 00:29:27,370 --> 00:29:28,390 Scooch this way a little bit. 440 00:29:28,990 --> 00:29:29,990 This way, this way. 441 00:29:30,590 --> 00:29:34,270 All right, so you shall be the 1's place, and just hold that in front of 442 00:29:34,570 --> 00:29:35,570 2's place, 443 00:29:36,350 --> 00:29:42,870 4's place, 8's place, 16, 32, 444 00:29:43,350 --> 00:29:44,390 64. 445 00:29:45,320 --> 00:29:46,320 And 128. 446 00:29:46,420 --> 00:29:50,380 And just compress yourselves a little bit if you could. So each of these folks 447 00:29:50,380 --> 00:29:53,220 represents a bit in a particular place. 448 00:29:53,580 --> 00:29:56,620 And let's say this. If you're just standing there sort of uncomfortably 449 00:29:56,620 --> 00:30:00,520 any hand raised, we'll assume that you're representing a 0, quite simply. 450 00:30:00,520 --> 00:30:03,600 your hand goes up, though, the audience should assume that you're representing a 451 00:30:03,600 --> 00:30:06,320 1. And therefore, what we'll do is fill out a three -letter word. 452 00:30:06,520 --> 00:30:10,600 And on each round of this, you'll either stay Stay like this, or you'll raise 453 00:30:10,600 --> 00:30:15,200 your hand. But first, let's actually meet some of our volunteers here, 454 00:30:15,200 --> 00:30:18,420 with position number one, if you'd like to say your name, perhaps where you're 455 00:30:18,420 --> 00:30:19,420 from and or studying. 456 00:30:19,560 --> 00:30:21,080 Hi, my name is Brooke. 457 00:30:21,320 --> 00:30:25,560 I'm from Indiana, and I'm studying biology and computer science. 458 00:30:26,440 --> 00:30:27,440 Nice. Welcome. 459 00:30:27,840 --> 00:30:33,180 Hi, I'm Becca. I'm from like Maryland, D .C. area, and I'm studying electrical 460 00:30:33,180 --> 00:30:34,180 engineering. 461 00:30:34,520 --> 00:30:35,520 Welcome. 462 00:30:39,639 --> 00:30:43,620 Engineering. Hi, I'm Sharon. I'm from Rwanda, and I'm studying CS in math. 463 00:30:43,820 --> 00:30:48,420 Welcome. Hi, I'm Grace. I'm from Alabama, and I'm studying electrical 464 00:30:48,420 --> 00:30:49,420 engineering. 465 00:30:50,080 --> 00:30:54,340 Welcome. Hi, I'm Angelina. I'm from Maryland, and also I stay in Matthews. 466 00:30:54,360 --> 00:30:55,780 Matthews. Nice. 467 00:30:57,580 --> 00:31:01,460 And I'm studying applied math and econ, as well as viral science and public 468 00:31:01,460 --> 00:31:02,620 policy. Welcome. 469 00:31:03,400 --> 00:31:06,940 I'm Owen Bells, and I'm from rural Virginia, and I'm studying CS. 470 00:31:07,610 --> 00:31:08,610 Nice. Welcome, Ant. 471 00:31:08,910 --> 00:31:10,870 And my name is Max. I'm from London. 472 00:31:11,170 --> 00:31:14,630 I'm also staying in Matthews. And I'm studying computer science and 473 00:31:14,630 --> 00:31:18,630 neuroscience. Welcome aboard as well. If you're wondering, too, why I was 474 00:31:18,630 --> 00:31:21,650 wearing these glasses at the start, so very common on the internet nowadays is 475 00:31:21,650 --> 00:31:24,810 these POV videos. So it turns out these Ray -Bans actually record video footage. 476 00:31:24,930 --> 00:31:27,610 And we have a couple of them. And we thought we'd offer them to a couple of 477 00:31:27,610 --> 00:31:31,650 volunteers if anyone wants to record their point of view for everyone here. 478 00:31:31,890 --> 00:31:34,090 And Vlad here is going to help make sure they're recording. 479 00:31:34,720 --> 00:31:36,140 Second volunteer? Yes, number two. 480 00:31:36,400 --> 00:31:41,720 All right. So as Vlad gets those set, on the backs of your pieces of paper, you 481 00:31:41,720 --> 00:31:45,000 have instructions for the following three rounds. Each round represents a 482 00:31:45,000 --> 00:31:48,400 letter. The audience participation part of this is to actually do the mental 483 00:31:48,400 --> 00:31:51,880 math to figure out what number these volunteers are representing. 484 00:31:52,240 --> 00:31:58,040 So go ahead and execute round one, either keeping your hand down or raising 485 00:31:58,040 --> 00:31:59,040 appropriately. 486 00:32:01,450 --> 00:32:04,370 DAVID J. OK. What number are our volunteers here representing? 487 00:32:05,390 --> 00:32:10,670 66. DAVID J. 66, because we have a 64 plus a 2. That then maps to what ASCII 488 00:32:10,670 --> 00:32:14,970 letter? B. DAVID J. B was the first letter. OK, hands down. Round two, go. 489 00:32:16,410 --> 00:32:17,410 A little harder. 490 00:32:17,890 --> 00:32:19,430 What's now being represented? 491 00:32:22,830 --> 00:32:23,910 I'm starting to hear it. 492 00:32:26,370 --> 00:32:31,350 9 is, in fact, correct. 79, because we have a 64, and an 8, and a 4, and a 2, 493 00:32:31,410 --> 00:32:35,190 and a 1. So if it's a 79, we have the ASCII letter O. 494 00:32:35,910 --> 00:32:39,310 OK, so we've got B, O, and then lastly, third round, go. 495 00:32:42,370 --> 00:32:45,290 We have 01010111. 496 00:32:47,230 --> 00:32:48,230 What number is this? 497 00:32:49,310 --> 00:32:50,950 87, which spells the letter? 498 00:32:52,210 --> 00:32:53,970 W, which spells the word? 499 00:32:55,100 --> 00:32:59,380 Not bow, take a bow if you could. All right, a round of applause for our 500 00:32:59,380 --> 00:33:00,380 volunteers here. 501 00:33:01,200 --> 00:33:05,600 And come on up this way and help yourself to a CS50 stress ball. 502 00:33:06,880 --> 00:33:09,960 Thank you to our volunteers. So this is only to say we've now agreed on how we 503 00:33:09,960 --> 00:33:13,180 can represent numbers from zero on up. We've agreed on how we can represent 504 00:33:13,180 --> 00:33:17,820 letters, but at least letters using ASCII. And in fact, these are more than 505 00:33:17,820 --> 00:33:21,780 decoration. In fact, as a little bit of trivia by lecture's end, if you want to 506 00:33:21,780 --> 00:33:26,020 come up for your very own CS50 stress ball, turns out there's 64 light bulbs 507 00:33:26,020 --> 00:33:31,510 the foot of the stage here. If you break them down into eight bytes, or single 8 508 00:33:31,510 --> 00:33:33,470 -bit or single byte chunk. 509 00:33:33,670 --> 00:33:37,070 There's an eight -letter word that happens to be spelled out today using 510 00:33:37,070 --> 00:33:41,910 here ASCII chart. So today's mystery is, what exactly is that there word? 511 00:33:42,210 --> 00:33:46,710 But of course, if you have only 8 bits, you can only represent like 256 512 00:33:46,710 --> 00:33:49,590 characters, which sounds like plenty for English. 513 00:33:49,830 --> 00:33:50,830 And indeed it is. 514 00:33:51,180 --> 00:33:55,340 0 through 9, A through B, capital and uppercase and lowercase, as well as 515 00:33:55,340 --> 00:33:59,700 punctuation. But there's so many other human languages in the world that have 516 00:33:59,700 --> 00:34:03,520 other characters. For instance, we have not just the English alphabet. We might 517 00:34:03,520 --> 00:34:05,020 see here on a US English keyboard. 518 00:34:05,280 --> 00:34:06,660 We have accented characters. 519 00:34:07,140 --> 00:34:10,900 We have various Asian languages, have even many more glyphs. We need more than 520 00:34:10,900 --> 00:34:13,199 256 possible characters. 521 00:34:13,580 --> 00:34:17,920 And so nowadays, computers do not just use seven or even eight bits. 522 00:34:18,159 --> 00:34:21,620 They might use eight bits for some letters. like all of the English 523 00:34:21,800 --> 00:34:27,880 they might use 16 bits for certain other languages, maybe even 24 or 32 bits. 524 00:34:28,040 --> 00:34:32,080 And fun fact, if you have 32 bits, and we have more than that on the stage, if 525 00:34:32,080 --> 00:34:36,699 you've got 32 bits, you can actually represent as many as 4 billion possible 526 00:34:36,699 --> 00:34:39,400 characters, which is quite a bit. 527 00:34:39,940 --> 00:34:40,940 No pun intended. 528 00:34:41,219 --> 00:34:43,000 So what else can we represent? 529 00:34:43,280 --> 00:34:47,739 Well, the goal of this system, not just ASCII, but something known as Unicode, 530 00:34:47,840 --> 00:34:51,750 which is a newer standard, is to be backwards compatible with ASCII. So 531 00:34:51,750 --> 00:34:56,989 left all of those other numbers alone, 65, 66, 67, and so forth. But they added 532 00:34:56,989 --> 00:35:03,110 to it a superset of representations that maybe are 16, 24, or 32 bits. The goal 533 00:35:03,110 --> 00:35:08,710 being to be able digitally to represent all human languages, past, present, and 534 00:35:08,710 --> 00:35:12,870 future, and even through pictograms, things like 535 00:35:13,630 --> 00:35:17,990 smiley faces and the like, even people places things in emotions that transcend 536 00:35:17,990 --> 00:35:19,310 human language. 537 00:35:19,530 --> 00:35:23,150 And in fact, odds are, within the past few minutes or hours, most of you have 538 00:35:23,150 --> 00:35:28,250 used one or more of these here emoji, these pictograms, which it turns out are 539 00:35:28,250 --> 00:35:30,250 just characters on a keyboard. 540 00:35:30,570 --> 00:35:34,010 You might have to hit a special button to pull up that form of the keyboard, 541 00:35:34,010 --> 00:35:35,530 these are just here characters. 542 00:35:35,950 --> 00:35:40,010 And so these emoji have exploded in popularity for a number of reasons, one 543 00:35:40,010 --> 00:35:43,670 which is, my god, what are we going to do with 4 billion possible patterns of 544 00:35:43,670 --> 00:35:46,730 zeros and ones, we can start to kind of have some fun with it and represent 545 00:35:46,730 --> 00:35:49,690 things beyond English and human languages alone. 546 00:35:50,150 --> 00:35:54,870 Now, as an aside, when it comes to Unicode, it turns out Unicode years ago 547 00:35:54,870 --> 00:36:00,950 standardized this pattern of 32 zeros and ones to represent just one of those 548 00:36:00,950 --> 00:36:03,830 emoji. So emoji tend to use even more bits here. 549 00:36:04,390 --> 00:36:07,050 Anyone know what decimal number this is? 550 00:36:07,530 --> 00:36:10,270 This is not a fun mathematical exercise. The spoiler is. 551 00:36:10,570 --> 00:36:16,910 4 ,036 ,991 ,106 is the decimal number that actually represents, 552 00:36:17,370 --> 00:36:20,430 as of present, the most popular emoji in the world. 553 00:36:20,930 --> 00:36:25,810 Does anyone want to hazard a guess what emoji this here number represents? 554 00:36:27,630 --> 00:36:28,670 Hearts? Hearts? 555 00:36:28,910 --> 00:36:31,410 No, but it's actually this so -called face. 556 00:36:32,000 --> 00:36:33,380 with tears of joy. 557 00:36:33,740 --> 00:36:36,660 So perhaps think about the frequency with which you send that one. And even 558 00:36:36,660 --> 00:36:41,100 though it's obviously a picture on the screen, sure, it actually is more like a 559 00:36:41,100 --> 00:36:44,600 font. Because underneath the hood, it's indeed just a pattern of zeros and ones 560 00:36:44,600 --> 00:36:48,580 or a decimal number that the computer is storing. But the computer, be it Mac OS 561 00:36:48,580 --> 00:36:54,480 or Windows or iOS or Android, know to display that pattern as this here 562 00:36:54,780 --> 00:36:57,980 But the pictures might look different depending on the hardware. Why? Because 563 00:36:57,980 --> 00:37:01,650 there's companies like Google and Microsoft and Meta and others that have 564 00:37:01,650 --> 00:37:04,090 own artists on staff as employees. 565 00:37:04,390 --> 00:37:08,650 And those artists interpret the descriptions, like face with tears of 566 00:37:08,750 --> 00:37:12,330 differently. So those of you with an Android phone actually see face with 567 00:37:12,330 --> 00:37:16,050 of joy looking a little something like this. And if you have Telegram, for 568 00:37:16,050 --> 00:37:19,790 instance, installed on your phone, it's even more animated than that. It's this 569 00:37:19,790 --> 00:37:24,910 here emoji using the same pattern of zeros and ones. So different artists 570 00:37:24,910 --> 00:37:29,210 these here emoji in different ways. But all they are here are patterns. 571 00:37:30,510 --> 00:37:34,710 Another answer, save one, that was shouted out a moment ago. This is a sort 572 00:37:34,710 --> 00:37:40,610 cloud diagram of the most popular emoji as of a couple of years ago per Unicode, 573 00:37:40,690 --> 00:37:44,990 whereby the size of the emoji indicates its relative popularity. So heart, I did 574 00:37:44,990 --> 00:37:48,830 here over here, is indeed one of the most popular ones as well. 575 00:37:49,050 --> 00:37:50,050 Question. 576 00:37:52,880 --> 00:37:57,000 A really good question. Why do certain emoji not show up on one device or 577 00:37:57,000 --> 00:38:00,980 another? It depends on how recent the software is. Pretty much every year, the 578 00:38:00,980 --> 00:38:05,620 humans behind the Unicode Consortium release new emoji, which is to say they 579 00:38:05,620 --> 00:38:09,220 decide that this other pattern will represent this new emoji. This other 580 00:38:09,220 --> 00:38:12,940 will represent this new emoji. And unless you update your phone, your 581 00:38:13,020 --> 00:38:17,100 your desktop to the very latest software, and the manufacturer of that 582 00:38:17,100 --> 00:38:21,580 software also updates by hiring an artist to draw those pictures in their 583 00:38:21,580 --> 00:38:25,360 font, in their own style, you're going to see usually just like an empty black 584 00:38:25,360 --> 00:38:28,840 square or maybe just like a black and white heart instead of something more 585 00:38:28,840 --> 00:38:32,040 colorful, really just placeholders. Because it's as though you don't have 586 00:38:32,040 --> 00:38:36,020 right font installed, or really you have an older version of that same font 587 00:38:36,020 --> 00:38:39,840 installed. But it's become sort of an annual tradition that new and more emoji 588 00:38:39,840 --> 00:38:43,700 are released every year, which is among the reasons why these updates contain 589 00:38:43,700 --> 00:38:45,660 more and more. Yeah? 590 00:38:48,990 --> 00:38:53,370 That is an amazing segue. How do you represent color in bytes? 591 00:38:53,570 --> 00:38:58,230 Well, you use RGB, which happens to be, by coincidence, the next slide. So let's 592 00:38:58,230 --> 00:38:58,868 again recap. 593 00:38:58,870 --> 00:39:00,010 We know how to represent letters. 594 00:39:00,560 --> 00:39:03,720 We know how to represent numbers. We can even represent emoji. But those emoji, 595 00:39:03,860 --> 00:39:06,980 technically, on the screen are, of course, composed of colors, like a whole 596 00:39:06,980 --> 00:39:08,980 bunch of yellow for that there smiley face. 597 00:39:09,200 --> 00:39:13,500 How do computers then, using only zeros and ones, represent colors? 598 00:39:13,740 --> 00:39:17,040 Well, by convention, they typically use a system that by an acronym is called 599 00:39:17,040 --> 00:39:19,260 RGB, red, green, blue. 600 00:39:19,480 --> 00:39:23,600 And this is to say that a computer, to represent a single dot on the screen, 601 00:39:23,820 --> 00:39:28,480 maybe this one, this one, this one, will allocate some number of bits. 602 00:39:28,910 --> 00:39:33,430 or some number of bytes to represent the color of just that there dot, otherwise 603 00:39:33,430 --> 00:39:37,270 known as a pixel. You can actually see pixels on your phone or even on your TV 604 00:39:37,270 --> 00:39:40,890 or monitor. If you go really close, especially if it's an older monitor, you 605 00:39:40,890 --> 00:39:42,250 see the tiny little squares. 606 00:39:42,550 --> 00:39:47,290 Each of those has some number of bits telling the device what color to use. 607 00:39:47,530 --> 00:39:52,750 In particular, these devices typically use three numbers in total, three bytes. 608 00:39:52,950 --> 00:39:57,250 So that is to say 24 bits per pixel. And they do this. 609 00:39:57,660 --> 00:40:00,720 If you were to represent a single dot on the screen using these three numbers, 610 00:40:00,840 --> 00:40:06,140 just by intent here, this is 72, 73, 33, which in the context of a text message, 611 00:40:06,260 --> 00:40:09,500 an email, a Google Doc, represents, of course, high. 612 00:40:10,260 --> 00:40:14,720 Textually, what if a computer uses that same pattern of zeros and ones, that is 613 00:40:14,720 --> 00:40:19,400 the same pattern of decimal digits, to represent the color on a screen, which 614 00:40:19,400 --> 00:40:23,220 germane if you're opening an image using Photoshop. So you're using a different 615 00:40:23,220 --> 00:40:27,480 piece of software that knows about colors and images and not just text. 616 00:40:27,500 --> 00:40:32,240 this would imply that you want that dot on the screen to have a medium amount of 617 00:40:32,240 --> 00:40:34,920 red, a medium amount of green, and a little bit of blue. 618 00:40:35,140 --> 00:40:36,680 Why do I see medium and little? 619 00:40:36,880 --> 00:40:42,400 Well, again, if each of these numbers is using 8 bits or 1 byte, the highest we 620 00:40:42,400 --> 00:40:44,740 can count, as we discovered, was 255. 621 00:40:44,960 --> 00:40:49,780 So I'm kind of averaging here. So 72 out of 255 feels to me like a medium amount 622 00:40:49,780 --> 00:40:50,459 of red. 623 00:40:50,460 --> 00:40:52,640 33 feels like relatively little blue. 624 00:40:53,280 --> 00:40:58,100 But if now the computer combines those wavelengths of light, so to speak, a 625 00:40:58,100 --> 00:41:01,760 medium amount of red, medium amount of green, a little bit of blue, what you 626 00:41:01,760 --> 00:41:05,040 is the color code for a single dot. And does anyone want to guess what color 627 00:41:05,040 --> 00:41:06,680 roughly this represents? 628 00:41:07,640 --> 00:41:08,640 These three bytes? 629 00:41:10,740 --> 00:41:13,620 Not white, not purple, not brown. 630 00:41:14,400 --> 00:41:19,220 Yellow, in fact, is the answer. So it represents, in a single pixel, roughly 631 00:41:19,220 --> 00:41:21,080 this shade here of yellow. 632 00:41:21,320 --> 00:41:25,240 Which is to say, if we look back at any of those emoji, which, again, are 633 00:41:25,240 --> 00:41:29,480 represented by patterns of zeros and ones, but you and I as humans perceive 634 00:41:29,480 --> 00:41:33,600 as images on the screen, let me actually go ahead and zoom and zoom in further 635 00:41:33,600 --> 00:41:35,260 to one such sample emoji. 636 00:41:35,520 --> 00:41:38,500 And when you zoom in far enough, or you put the phone close enough to your face, 637 00:41:38,680 --> 00:41:42,820 you can actually see all of those little dots known as pixels, all of the little 638 00:41:42,820 --> 00:41:43,820 squares. 639 00:41:44,010 --> 00:41:48,410 that so many of these pixels are yellow, that is to say that that pattern of 640 00:41:48,410 --> 00:41:53,470 three bytes, 72, 73, 33, is used to represent this pixel. 641 00:41:53,710 --> 00:41:58,670 Another three identical bytes are used to represent this pixel, this one, this 642 00:41:58,670 --> 00:42:02,230 one, and so forth. So now if you've taken digital photos on your phone or a 643 00:42:02,230 --> 00:42:05,530 camera, you're probably generally familiar from the internet and hardware 644 00:42:05,530 --> 00:42:08,290 that a photograph is what, like one megabyte? 645 00:42:08,800 --> 00:42:13,460 10 megabytes, depending on the resolution of it. Well, megabyte means 646 00:42:13,460 --> 00:42:14,178 of bytes. 647 00:42:14,180 --> 00:42:18,140 Where are all of these bytes inside of these photographs or these images you're 648 00:42:18,140 --> 00:42:19,140 taking or downloading? 649 00:42:19,320 --> 00:42:24,740 They correspond to every one of the single pixels on the screen. There's at 650 00:42:24,740 --> 00:42:29,280 least three bytes being used to represent every one of those dots. 651 00:42:29,790 --> 00:42:33,290 As an aside, bit of a white lie, because nowadays there's fancy compression 652 00:42:33,290 --> 00:42:37,990 software that can use fewer than that many bytes. But in general, that's where 653 00:42:37,990 --> 00:42:41,310 all of those bytes, those millions of bytes, are coming from. 654 00:42:41,750 --> 00:42:44,770 So how was that for an answer to how do we represent colors? 655 00:42:44,990 --> 00:42:45,990 Thank you. 656 00:42:46,060 --> 00:42:51,180 So if we agreed now that there's this way and perhaps others to represent 657 00:42:51,180 --> 00:42:54,880 colors, well, how do we represent not just images but videos? 658 00:42:55,300 --> 00:42:59,260 Well, videos, once upon a time, or movies, were called motion pictures. 659 00:42:59,520 --> 00:43:04,480 So most pictures with motion. Why is that? Well, it's analogous to growing 660 00:43:04,560 --> 00:43:07,100 If you ever played with one of these picture books, and in fact, there's 661 00:43:07,100 --> 00:43:11,780 nowadays that have made these popular again, whereby you have a whole bunch of 662 00:43:11,780 --> 00:43:14,060 images on individual sheets of paper. 663 00:43:15,400 --> 00:43:20,180 through them fast enough, your human mind and eyes perceive it as actual 664 00:43:20,360 --> 00:43:23,860 even though you're just seeing image, image, image, image, image, image, but 665 00:43:23,860 --> 00:43:25,360 it's so fast it looks like motion. 666 00:43:25,760 --> 00:43:28,060 That's all a video is on your screen. 667 00:43:28,340 --> 00:43:32,560 That's all a film is on your TV. It is not, in fact, continuous motion. 668 00:43:32,820 --> 00:43:34,440 It's maybe 30 frames. 669 00:43:35,040 --> 00:43:40,340 or images per second, maybe 24 frames or images per second, which is to say we 670 00:43:40,340 --> 00:43:42,820 know how to represent numbers. We know how to represent letters. 671 00:43:43,060 --> 00:43:46,940 We know how to represent colors and those images. Now we kind of get videos 672 00:43:46,940 --> 00:43:51,120 free because it's just more of the same. Use more and more of those patterns. 673 00:43:51,280 --> 00:43:52,880 Why are videos so darn large? 674 00:43:53,100 --> 00:43:57,060 Why are they gigabytes to download, billions of bytes? Because there's so 675 00:43:57,060 --> 00:44:01,740 darn images, 30 some odd images per second in those kinds of videos. 676 00:44:02,410 --> 00:44:07,050 And maybe lastly, just to top off our multimedia, how could you represent 677 00:44:07,670 --> 00:44:12,870 Maybe musicians in the room, how, using only zeros and ones, could you represent 678 00:44:12,870 --> 00:44:14,990 something as sonorous as music? 679 00:44:16,770 --> 00:44:18,550 Something analog as digital, yeah. 680 00:44:21,000 --> 00:44:24,560 Yeah, so each number that we store in the computer could correspond to a 681 00:44:24,560 --> 00:44:29,220 frequency, which has a direct relationship to the sound or the pitch 682 00:44:29,400 --> 00:44:32,320 For instance, in the world of piano and many other instruments, you've got like 683 00:44:32,320 --> 00:44:36,240 your A, your B, your C. Maybe you have sharps and flats as well. We could just 684 00:44:36,240 --> 00:44:40,140 agree, like the ASCII people did years ago, to represent the musical note A, 685 00:44:40,300 --> 00:44:44,620 let's use this pattern. Musical note A sharp, let's use this pattern, and so 686 00:44:44,620 --> 00:44:48,740 forth. But maybe pitch alone or frequency alone is not enough. Maybe we 687 00:44:48,740 --> 00:44:49,740 that number. 688 00:44:49,930 --> 00:44:53,490 but maybe a second number for the volume, the sort of digital equivalent 689 00:44:53,490 --> 00:44:58,130 hard are you hitting the key on the piano. Maybe a third number for how long 690 00:44:58,130 --> 00:44:59,390 you holding the key down. 691 00:44:59,610 --> 00:45:05,290 So maybe the pitch and the volume and the duration, kind of like RGB, we could 692 00:45:05,290 --> 00:45:09,770 use three bytes to represent every musical note in some piece. 693 00:45:10,070 --> 00:45:13,770 And if we wanted to keep track of what instrument should be played by the 694 00:45:13,770 --> 00:45:18,630 computer to sound that music, well, maybe that's just a fourth byte or 695 00:45:18,630 --> 00:45:22,930 else is well, which is to say at the end of the day, all we have are these 0s 696 00:45:22,930 --> 00:45:25,550 and 1s to throw at the problem. 697 00:45:26,270 --> 00:45:29,310 So for now, that's it for representing information. 698 00:45:29,630 --> 00:45:33,370 We've got our numbers. We've got our letters. We've got our colors and 699 00:45:33,490 --> 00:45:34,790 our videos, and now sound. 700 00:45:35,170 --> 00:45:40,550 Any questions on how computers then are representing, as promised, those inputs 701 00:45:40,550 --> 00:45:45,950 and outputs using just 0s and 1s? Yeah, in the middle. 702 00:45:49,560 --> 00:45:53,040 Correct. So the computer is taking as input, at the end of the day, 0s and 1s 703 00:45:53,040 --> 00:45:57,320 and is outputting 0s and 1s. However, as we'll learn in this class, by writing 704 00:45:57,320 --> 00:46:01,990 software, by writing code that understands those zeros and ones, we 705 00:46:01,990 --> 00:46:06,190 not just literally seeing zeros and ones. We will see A, B, C. We will see 706 00:46:06,190 --> 00:46:07,970 colors. We will see video. 707 00:46:08,170 --> 00:46:12,390 We will hear sound so long as we write the code to interpret those zeros and 708 00:46:12,390 --> 00:46:15,670 ones. And indeed, it's worth noting now that that same pattern I keep using for 709 00:46:15,670 --> 00:46:17,370 an example, 72, 73, 33. 710 00:46:17,710 --> 00:46:20,570 How does a computer know is that the message high? 711 00:46:20,850 --> 00:46:22,250 Is that the color yellow? 712 00:46:22,510 --> 00:46:24,650 Is it a dot in a video alone? 713 00:46:25,210 --> 00:46:26,450 Just depends on the context. 714 00:46:26,690 --> 00:46:31,890 Simply put, if you're opening that pattern of zeros and ones with Excel or 715 00:46:31,890 --> 00:46:36,010 calculator program, odds are the software will interpret those three 716 00:46:36,010 --> 00:46:39,710 numbers, of course. If, though, you open that same pattern in a text messaging 717 00:46:39,710 --> 00:46:44,870 program, Google Docs, Microsoft Word, that same pattern will be interpreted as 718 00:46:44,870 --> 00:46:48,250 sequence of letters. Instead, if you open Photoshop that same pattern, you'll 719 00:46:48,250 --> 00:46:51,650 probably see a single dot that happens to be yellow. 720 00:46:52,420 --> 00:46:56,840 Conversely, once you yourself are a programmer or even better programmer, 721 00:46:56,840 --> 00:47:01,160 will be able to write in code how you want the computer to treat these 722 00:47:01,160 --> 00:47:05,560 of zeros and ones. You can essentially tell the computer, use this to store a 723 00:47:05,560 --> 00:47:10,200 number or a letter or a color or something else. That's the power the 724 00:47:10,200 --> 00:47:12,560 themselves have at the end of the day. 725 00:47:13,660 --> 00:47:17,040 Other questions on representing things with bits? 726 00:47:19,480 --> 00:47:20,480 All right. 727 00:47:20,620 --> 00:47:25,500 So lastly, then, in this middle of this black box, so to speak, is the sort of 728 00:47:25,500 --> 00:47:29,640 secret sauce that solves problems, that converts those inputs to outputs, those 729 00:47:29,640 --> 00:47:33,320 problems to solutions. So what is an algorithm? It's really just step -by 730 00:47:33,320 --> 00:47:38,160 instructions for solving some problem. And indeed, I think back to my own first 731 00:47:38,160 --> 00:47:42,220 time in CS50, where we learned the same from Professor Brian Kernaghan. And as 732 00:47:42,220 --> 00:47:45,820 luck would have it, just had my 25th reunion, where we pulled some video 733 00:47:45,820 --> 00:47:46,820 from 1996. 734 00:47:47,020 --> 00:47:52,240 And so we're actually fortunate. to have the very first few minutes of CS50 over 735 00:47:52,240 --> 00:47:57,200 25 years ago when I myself took it. But the lessons back then as today are 736 00:47:57,200 --> 00:48:02,300 fundamentally the same. And what's important indeed is to not only express 737 00:48:02,300 --> 00:48:06,320 yourself correctly, but precisely, as we'll explore today. 738 00:48:06,640 --> 00:48:11,320 This then is Professor Brian Kernaghan, who years ago very memorably introduced 739 00:48:11,320 --> 00:48:16,960 us and my classmates to algorithms by actually in class shaving his beard. 740 00:48:17,530 --> 00:48:19,470 if we could dim the lights here for Brian. 741 00:48:20,270 --> 00:48:22,770 The other thing that we're going to talk about in this class is the notion of an 742 00:48:22,770 --> 00:48:26,470 algorithm. An algorithm is a very precise description of how to do 743 00:48:26,890 --> 00:48:28,430 And the operative word there is precise. 744 00:48:28,710 --> 00:48:31,350 It has to be very, very, very, very precise. 745 00:48:31,650 --> 00:48:34,970 And the task that I am going to do is that I'm going to trim my beard, which 746 00:48:34,970 --> 00:48:36,670 gotten out of whack. 747 00:48:45,040 --> 00:48:49,760 And I brought a variety of things which one might use to 748 00:48:49,760 --> 00:48:56,140 trim beards with. 749 00:49:13,870 --> 00:49:16,790 So suffice it to say, I don't have much of a beard, but I do have this here 750 00:49:16,790 --> 00:49:20,950 other technology known once upon a time as a phone book. And these phone books, 751 00:49:21,030 --> 00:49:24,210 of course, have lots of information in them, happen to be storing numbers and 752 00:49:24,210 --> 00:49:28,550 letters in particular. For those unfamiliar, they are storing human names 753 00:49:28,550 --> 00:49:32,350 to Z here in English and associated with everyone's name is a number. So even if 754 00:49:32,350 --> 00:49:35,720 you've never had occasion to physically use this kind of device, turns out it's 755 00:49:35,720 --> 00:49:39,880 pretty much equivalent to the contacts or the address book app on your iOS 756 00:49:39,880 --> 00:49:43,200 or your Android phone as well. Why? Because if you pull up your contacts, of 757 00:49:43,200 --> 00:49:46,460 course, you see some familiar names here alphabetized by first name or last 758 00:49:46,460 --> 00:49:50,260 name. And if you click on any of those names, you reach the person you're 759 00:49:50,260 --> 00:49:51,860 presumably trying to call or text. 760 00:49:52,180 --> 00:49:56,580 Pictured here, then, is John Harvard's, whose number here is plus 1 -949 -468 761 00:49:56,580 --> 00:49:59,440 -2750, which you're welcome to call or text at your leisure. 762 00:49:59,660 --> 00:50:02,740 But here is John Harvard that's partway through the phone. 763 00:50:03,380 --> 00:50:07,900 Well, it turns out that physically in the phone book, we might use an 764 00:50:08,200 --> 00:50:12,740 step -by -step instructions for finding John Harvard in pretty much the same way 765 00:50:12,740 --> 00:50:17,360 as iOS, Android, Mac OS, Windows, or other operating systems themselves use. 766 00:50:17,580 --> 00:50:22,080 So I could, when looking for John Harvard, first name starting with J, I 767 00:50:22,080 --> 00:50:25,560 start at the beginning of the phone book and start looking page by page. 768 00:50:25,980 --> 00:50:29,540 by page for John Harvard. And if he's there, I can call. 769 00:50:29,980 --> 00:50:33,480 This is an algorithm. It's indeed step by step, but that was a bug. It's a few 770 00:50:33,480 --> 00:50:34,419 pages turned. 771 00:50:34,420 --> 00:50:37,060 But is this algorithm correct? 772 00:50:38,520 --> 00:50:40,600 Step by step, assuming I'm paying attention. 773 00:50:41,440 --> 00:50:45,740 So yes, if John Harvard is in here, I will eventually find him once I get to 774 00:50:45,740 --> 00:50:48,880 J section. Now, this is a little tedious. It's going to take a while, a 775 00:50:48,880 --> 00:50:51,940 dozen, a few hundred pages. So maybe I could do things a little smarter from 776 00:50:51,940 --> 00:50:54,520 grade school, like 2, 4, 6. 777 00:50:54,970 --> 00:50:59,610 8, 10, 12, 14, 16, and so forth going twice as fast. 778 00:50:59,890 --> 00:51:00,990 Is that algorithm correct? 779 00:51:01,730 --> 00:51:02,970 So no, but why? 780 00:51:04,330 --> 00:51:07,270 I could miss him, right? I could just get unlucky, really, with 50 -50 781 00:51:07,270 --> 00:51:11,430 probability because John Harvard could be sandwiched between two pages. 782 00:51:11,690 --> 00:51:13,990 Now, this isn't a complete loss, this algorithm. 783 00:51:14,190 --> 00:51:19,450 Maybe what I could do is if I get past the j section to k, I could double back 784 00:51:19,450 --> 00:51:23,770 at least one page just to make sure I didn't miss John Harvard. So I can still 785 00:51:23,770 --> 00:51:28,510 go twice as fast plus an extra step just to make sure I didn't mess up. So the 786 00:51:28,510 --> 00:51:32,150 first algorithm might take as many pages as there are in the phone book. So if 787 00:51:32,150 --> 00:51:36,010 this phone book has 1 ,000 pages, pages, in the worst case, if I'm not looking 788 00:51:36,010 --> 00:51:39,910 for John Harvard but someone whose name starts with Z, might take me 1 ,000 789 00:51:39,910 --> 00:51:41,310 pages to actually get there. 790 00:51:41,530 --> 00:51:44,630 Second algorithm, twice as fast literally, might take me 500. 791 00:51:45,290 --> 00:51:48,950 plus one step to get there, because I'm going two at a time, so long as I indeed 792 00:51:48,950 --> 00:51:49,848 fix that bug. 793 00:51:49,850 --> 00:51:54,190 But what we used to do back in the day, and what your phone is doing today, 794 00:51:54,450 --> 00:51:58,710 albeit digitally, is going roughly to the middle of the phone book, looking 795 00:51:58,710 --> 00:52:02,550 and realizing, oh, I'm accidentally in the M section, so halfway through the 796 00:52:02,550 --> 00:52:06,250 phone book. But what do I now know about John Harvard? Is he to the left or to 797 00:52:06,250 --> 00:52:07,250 the right? 798 00:52:07,600 --> 00:52:10,360 So he's obviously to the left, because j comes before m. 799 00:52:10,580 --> 00:52:14,100 So what I can do literally, and what your computer does figuratively, is tear 800 00:52:14,100 --> 00:52:19,040 the problem in half, throw half of the problem away, leaving us now with the 801 00:52:19,040 --> 00:52:21,900 same fundamental problem, but it's half as big. 802 00:52:22,180 --> 00:52:27,580 So I've gone from 1 ,000 pages suddenly to 500 pages. And compare this to the 803 00:52:27,580 --> 00:52:28,558 other two. 804 00:52:28,560 --> 00:52:34,900 1 ,000 pages, 999, 998, versus 1 ,000 pages, 998, 996, 805 00:52:35,060 --> 00:52:36,640 994. That's still slow. 806 00:52:37,000 --> 00:52:40,980 I went from 1 ,000 to 500 in just one step of this algorithm. 807 00:52:41,260 --> 00:52:43,340 What do I do next? I go roughly to the middle here. 808 00:52:43,620 --> 00:52:47,460 Oh, I went a little too far. I'm in the E section now. So is John Harvard to the 809 00:52:47,460 --> 00:52:48,460 left or right now? 810 00:52:49,040 --> 00:52:52,940 So he's to the right. So I can, again, tear the problem in half, throw the left 811 00:52:52,940 --> 00:52:57,040 half away, knowing now that John Harvard must alphabetically be in here. And I 812 00:52:57,040 --> 00:53:01,920 can divide and divide and divide and conquer this problem again and again by 813 00:53:01,920 --> 00:53:05,560 using that heuristic of going left or going right. And I dare say, if I do 814 00:53:05,560 --> 00:53:10,120 correctly, I'll eventually be left with one single page on which John Harvard's 815 00:53:10,120 --> 00:53:14,060 number actually is, or maybe he's not in the phone book at all. 816 00:53:14,480 --> 00:53:19,420 So how many steps maximally might that third and final algorithm take? It's not 817 00:53:19,420 --> 00:53:22,060 1 ,000. It's not even 500 or 501. 818 00:53:22,460 --> 00:53:27,640 How many times can you divide 1 ,000 pages in half again and again and again? 819 00:53:28,780 --> 00:53:29,780 Roughly. 820 00:53:31,340 --> 00:53:35,820 9, 10, so typically 10 times, give or take. There's a bit of rounding there 821 00:53:35,820 --> 00:53:39,800 because it's not a perfect power of 2, but roughly 10 times. That is 822 00:53:39,800 --> 00:53:44,180 fundamentally better than both of the two algorithms because I go from 1 ,000 823 00:53:44,180 --> 00:53:51,100 pages to 500 to 250 to 125 and so forth, literally having the problem again 824 00:53:51,100 --> 00:53:51,999 and again. 825 00:53:52,000 --> 00:53:56,140 So we can actually appreciate and see this even more so graphically. And this 826 00:53:56,140 --> 00:54:00,080 among the things we'll do later in the term when we speak to not only writing 827 00:54:00,080 --> 00:54:03,080 correct code, But is your code well designed? 828 00:54:03,480 --> 00:54:07,960 Is it better than your previous code? Is it better than someone else's code? Is 829 00:54:07,960 --> 00:54:11,080 it better than some other product? If you have given more thought to the 830 00:54:11,080 --> 00:54:15,440 algorithms and the quality thereof, you can perhaps minimize the time required 831 00:54:15,440 --> 00:54:17,920 to solve problems, but no less correctly. 832 00:54:18,280 --> 00:54:22,380 So if we have a simple xy plus here, on the y -axis or vertical is the amount of 833 00:54:22,380 --> 00:54:25,920 time to solve in whatever unit of measure, seconds, pages, however you 834 00:54:25,920 --> 00:54:29,860 count. On the horizontal or x -axis is the size of the problem. 835 00:54:30,160 --> 00:54:32,080 measured in, for instance, numbers of pages. 836 00:54:32,300 --> 00:54:36,220 So this would mean zero pages in the phone book. This would mean a lot of 837 00:54:36,220 --> 00:54:39,860 in the phone book. This would mean no time to solve. This would mean a lot of 838 00:54:39,860 --> 00:54:43,200 time to solve. What's the relationship, then, among those three algorithms? 839 00:54:43,560 --> 00:54:46,540 Well, the first one is essentially a straight line, a slope of 1. 840 00:54:46,780 --> 00:54:51,880 And if the phone book has n pages in it, we'll describe the slope here as 841 00:54:51,880 --> 00:54:57,880 essentially 1 over 1 for the first algorithm, turning page by page by page, 842 00:54:58,040 --> 00:55:02,390 which is to say, If we were to add one more page to the phone book next year, 843 00:55:02,750 --> 00:55:04,650 first algorithm is going to take one more step. 844 00:55:04,910 --> 00:55:08,990 But the second algorithm is definitely better. It's definitely faster, but it's 845 00:55:08,990 --> 00:55:09,990 still a straight line. 846 00:55:10,050 --> 00:55:13,990 So it's going to take roughly n over 2 steps on average, because you only have 847 00:55:13,990 --> 00:55:17,090 to go through half of the phone book, because you're going two pages at a 848 00:55:17,150 --> 00:55:21,290 instead of the whole phone book, in the worst case, if someone's name is Z, to 849 00:55:21,290 --> 00:55:22,770 go through every page in total. 850 00:55:23,050 --> 00:55:25,930 So if we actually compare these, let me just draw some dashed lines. 851 00:55:26,190 --> 00:55:30,080 Suppose that you have this many pages in the phone book. If you just draw this 852 00:55:30,080 --> 00:55:34,140 vertical white line here, it's going to take this much time in red using the 853 00:55:34,140 --> 00:55:37,660 first algorithm, but it's going to literally take half as much time in 854 00:55:37,660 --> 00:55:40,640 for the second algorithm, because you're literally going two pages at once. 855 00:55:40,920 --> 00:55:45,700 But the third and final algorithm is a fundamentally different shape. It 856 00:55:45,700 --> 00:55:47,320 looks a little something like this. 857 00:55:47,640 --> 00:55:51,940 It looks like it's flatter and flatter and flatter. It's always increasing. 858 00:55:52,240 --> 00:55:53,700 It never gets perfectly flat. 859 00:55:53,900 --> 00:55:58,920 But it grows so much more slowly as a function of phone book size. 860 00:55:59,200 --> 00:56:04,160 And for those who recall their logarithms, this would be described as 861 00:56:04,160 --> 00:56:04,879 of n. 862 00:56:04,880 --> 00:56:09,310 And in fact, that's where the math came from. Log base 2 of 1 ,000 is roughly 10 863 00:56:09,310 --> 00:56:12,430 in total, even if you need a calculator to confirm as much. 864 00:56:12,650 --> 00:56:15,610 But this shape is fundamentally different. Why? 865 00:56:15,830 --> 00:56:18,810 Well, suppose that Cambridge, where we are, and Alston, the town across the 866 00:56:18,810 --> 00:56:22,190 street next year, combine their two phone books. And they go from 1 ,000 867 00:56:22,190 --> 00:56:24,630 each to one phone book with 2 ,000 pages. 868 00:56:25,090 --> 00:56:28,490 The first algorithm is going to literally take twice as many steps or 869 00:56:28,850 --> 00:56:33,430 Second algorithm is going to take half as many, or 50 % more, because you're 870 00:56:33,430 --> 00:56:37,250 going two at a time. But the third algorithm is going to barely miss a 871 00:56:37,350 --> 00:56:38,350 Why? 872 00:56:38,570 --> 00:56:42,170 This is 1 ,000 pages here, and 2 ,000 pages is over there. 873 00:56:42,390 --> 00:56:45,710 Just inferring from the shape of the green line, it's not going to be much 874 00:56:45,710 --> 00:56:49,650 higher on the vertical axis than the other two were. 875 00:56:50,070 --> 00:56:55,930 So more specifically, if you have a 2 ,000 page phone book next year, how many 876 00:56:55,930 --> 00:56:59,070 more steps will it take you using that third and final algorithm? 877 00:56:59,950 --> 00:57:04,150 Just one, because you'll divide and conquer a 2 ,000 -page phone book into a 878 00:57:04,150 --> 00:57:07,350 ,000 -page phone book, and then you're back at the original story. 879 00:57:07,650 --> 00:57:11,310 And that's the sort of power of learning algorithms. That's the power of 880 00:57:11,310 --> 00:57:15,230 learning computer science and learning how to program is to be able to navigate 881 00:57:15,230 --> 00:57:19,350 big data, so to speak, things the size of Google, things the size of artificial 882 00:57:19,350 --> 00:57:24,030 intelligence training data sets using better and better, more clever 883 00:57:24,030 --> 00:57:27,630 that perform faster and therefore not only make the software more competitive, 884 00:57:27,730 --> 00:57:29,150 but also make it more usable. 885 00:57:29,550 --> 00:57:33,390 and more favorable for people like you and me when using that software. 886 00:57:34,220 --> 00:57:39,280 So when it comes to implementing algorithms as programmers, as computer 887 00:57:39,280 --> 00:57:43,040 scientists, what you're really doing is taking these algorithms, which might be 888 00:57:43,040 --> 00:57:46,780 expressed in English conceptually as we just did, but really just translating 889 00:57:46,780 --> 00:57:52,760 them to code, be it C or C++ or Python or R or Ruby or any number of other 890 00:57:52,760 --> 00:57:56,580 languages that exist in the world. But for now, let's consider how we might 891 00:57:56,580 --> 00:57:59,920 implement that algorithm using something that's literally still English but 892 00:57:59,920 --> 00:58:05,820 pseudocode, something that is still correct, but precise and finite, as per 893 00:58:05,820 --> 00:58:09,420 Professor Kernighan's advice, which is to say, use your own vernacular of 894 00:58:09,420 --> 00:58:13,380 English and just say what you mean, but very succinctly. There's no one way to 895 00:58:13,380 --> 00:58:15,280 write pseudocode. It's not some formal language. 896 00:58:15,500 --> 00:58:19,580 I'm just going to convert the steps I did intuitively to step -by -step 897 00:58:19,580 --> 00:58:20,960 instructions as follows. 898 00:58:21,480 --> 00:58:24,700 Step one, what I did was pretty much pick up the phone book. 899 00:58:24,960 --> 00:58:29,140 Step two, what I did was pretty much open to middle of phone book for the 900 00:58:29,140 --> 00:58:31,320 algorithm. Step three, look at page. 901 00:58:31,620 --> 00:58:32,620 Step four. 902 00:58:33,050 --> 00:58:37,230 If person is on page, then step five, call person. 903 00:58:37,470 --> 00:58:42,410 If that does not prove to be the case, step six, else if the person is earlier 904 00:58:42,410 --> 00:58:48,270 in the book, then open to the middle of the left half of the book, and then go 905 00:58:48,270 --> 00:58:50,090 back to line three. 906 00:58:51,670 --> 00:58:57,110 Then, else if the person is later in the book, open to the middle of the right 907 00:58:57,110 --> 00:59:00,030 half of the book, and again, go to line three. 908 00:59:00,630 --> 00:59:02,650 Else, there's a fourth and final case. 909 00:59:03,050 --> 00:59:06,290 If the person like John Harvard is not on the page, is not earlier, is not 910 00:59:06,290 --> 00:59:08,650 later, what's the fourth scenario we'd best consider? 911 00:59:09,930 --> 00:59:14,370 He's just not there, else we should do something specific like quit. Now, as an 912 00:59:14,370 --> 00:59:17,270 aside, everyone in this room has probably had one of these stupid 913 00:59:17,270 --> 00:59:20,850 support issues where your phone or your laptop or your desktop computer just 914 00:59:20,850 --> 00:59:24,190 freeze all of a sudden, or maybe it spontaneously reboots for no reason. 915 00:59:24,510 --> 00:59:28,950 Odds are that's because not you, but some other human made a mistake. 916 00:59:29,270 --> 00:59:33,130 They probably wrote code working at Microsoft or Apple or Google or 917 00:59:33,130 --> 00:59:36,950 else, and they didn't actually anticipate that, oh, there could be a 918 00:59:36,950 --> 00:59:41,390 scenario that could happen in the real world. world, but if there's no code 919 00:59:41,390 --> 00:59:45,790 tells the computer what to do in that fourth and final scenario, who knows 920 00:59:45,790 --> 00:59:48,050 the computer is going to do. It might, by default, reboot. 921 00:59:48,370 --> 00:59:49,570 It might, by default, freeze. 922 00:59:49,890 --> 00:59:53,530 That's just a hint of the bugs, the mistakes in software to come. 923 00:59:53,830 --> 00:59:58,710 But even though this is just one way to write this code, a .k .a. 924 00:59:59,050 --> 01:00:02,450 pseudocode, there are some salient characteristics that we'll use 925 01:00:02,450 --> 01:00:07,790 today. One, there are these verbs, these actions. And henceforth, as aspiring 926 01:00:07,790 --> 01:00:11,450 computer scientists or programmers, we're going to start to call these by 927 01:00:11,450 --> 01:00:14,250 more technical audience would. These are functions. 928 01:00:14,650 --> 01:00:16,910 A function is an action or a verb. 929 01:00:17,130 --> 01:00:21,150 It's like a bite -sized task that a computer can do for you. Those, then, 930 01:00:21,150 --> 01:00:25,470 functions in this here pseudocode. But there's other types of code in here. 931 01:00:25,930 --> 01:00:27,050 There are these things here. 932 01:00:27,600 --> 01:00:30,720 else if, else if, else, those are examples of what we're going to start 933 01:00:30,720 --> 01:00:34,920 conditional. These are sort of proverbial forks in the road where maybe 934 01:00:34,920 --> 01:00:39,680 this way, maybe you go this way, but you decide which way to go based on a 935 01:00:39,680 --> 01:00:44,290 question. The questions that you ask are what we'll technically call Boolean 936 01:00:44,290 --> 01:00:48,790 expressions, named after mathematician Boole. A Boolean expression is a 937 01:00:48,790 --> 01:00:54,810 with a yes or no answer, a true or false answer, a black or white answer, a one 938 01:00:54,810 --> 01:00:58,630 or zero answer. There's two possibilities, and there's a hint of the 939 01:00:58,630 --> 01:01:03,430 underneath. A Boolean expression is going to tell you yes or no, you should 940 01:01:03,430 --> 01:01:05,330 down that fork in the road. 941 01:01:05,980 --> 01:01:10,700 Notice what's important here is that indentation mattered as a result. 942 01:01:11,060 --> 01:01:15,740 Notice that on line four, when I first asked if the person is on page question 943 01:01:15,740 --> 01:01:20,900 mark, so to speak, I should only do line five per its indentation if the answer 944 01:01:20,900 --> 01:01:22,600 is yes or true. 945 01:01:22,800 --> 01:01:26,100 I should only open to the middle of the left half of the book and go back to 946 01:01:26,100 --> 01:01:27,100 line three if. 947 01:01:27,550 --> 01:01:31,910 Person is instead earlier in the book. So indentation in pseudocode and in many 948 01:01:31,910 --> 01:01:34,470 programming languages has a logical significance. 949 01:01:34,730 --> 01:01:36,870 It tells you whether to do things or not. 950 01:01:37,170 --> 01:01:42,030 But there's another construct in here, go back to, go back to, which literally 951 01:01:42,030 --> 01:01:46,170 makes me go back to line three, potentially again and again and again, 952 01:01:46,170 --> 01:01:50,010 some kind of cycle or what we'll typically call a loop instead. 953 01:01:50,600 --> 01:01:54,860 So even in this relatively simple real -world algorithm, we have these four 954 01:01:54,860 --> 01:01:58,820 fundamental characteristics of most computer programs that we will write in 955 01:01:58,820 --> 01:02:02,620 class and you might write beyond this class, that we have some technical 956 01:02:02,620 --> 01:02:06,740 now to describe them. But what's important to note is that line 8 and 957 01:02:07,500 --> 01:02:12,120 11, even though they're saying go back to line 3, go back to line 3, you might 958 01:02:12,120 --> 01:02:15,380 think you're running the risk of what we'll call an infinite loop, where you 959 01:02:15,380 --> 01:02:19,760 literally get stuck in a loop forever, which doesn't sound like a good thing if 960 01:02:19,760 --> 01:02:22,160 at some point you want to turn your computer off, even though it's still 961 01:02:22,160 --> 01:02:27,440 working. But these will not induce infinite loops. Why? What is happening 962 01:02:27,440 --> 01:02:31,660 this particular algorithm every time we go back to line 3 that guarantees 963 01:02:31,660 --> 01:02:35,240 eventually we will stop going back to line 3? 964 01:02:38,069 --> 01:02:41,530 Exactly. If the person is on the page, we will call them or we will quit. But 965 01:02:41,530 --> 01:02:45,490 more importantly, because we keep dividing and conquering the problem, in 966 01:02:45,490 --> 01:02:49,410 case, having the phone book, having the phone book, eventually, we're going to 967 01:02:49,410 --> 01:02:52,990 run out of phone book, in which case, indeed, John Harvard is either on that 968 01:02:52,990 --> 01:02:56,150 page or not, and we will call or we will quit instead. 969 01:02:56,630 --> 01:03:00,450 So we'll see in time. And in fact, allow me to promise, odds are at some point, 970 01:03:00,490 --> 01:03:04,930 you will write code that seems to take control of the computer for you, where 971 01:03:04,930 --> 01:03:07,530 it's doing something, doing something, doing something, and it literally won't. 972 01:03:07,530 --> 01:03:10,930 respond to you anymore, that's just going to be because of a mistake, a so 973 01:03:10,930 --> 01:03:15,230 -called bug that you yourself will invariably have added to your code 974 01:03:15,230 --> 01:03:19,670 accidentally, but will show you ways for terminating it or breaking out of those 975 01:03:19,670 --> 01:03:23,130 conditions. And indeed, what we'll do in just a little bit after a break for 976 01:03:23,130 --> 01:03:27,670 today's lecture is explore not just these concepts, but some of the ways you 977 01:03:27,670 --> 01:03:31,290 use them to solve real and very visual and audio problems. 978 01:03:31,530 --> 01:03:34,690 But for now, let's at least connect it to something that's been all too germane 979 01:03:34,690 --> 01:03:36,390 in recent months, the past few years. 980 01:03:36,750 --> 01:03:39,970 namely artificial intelligence, which is a topic we'll come back to at the end 981 01:03:39,970 --> 01:03:43,610 of the course, too, to give you a sense of what the connection is with what 982 01:03:43,610 --> 01:03:47,090 everyone's been talking about in the world of AI and what it is we're going 983 01:03:47,090 --> 01:03:49,790 spend the next few weeks building up to by writing code. 984 01:03:50,290 --> 01:03:54,790 If you were to try to implement something like a chat bot, for instance, 985 01:03:54,790 --> 01:03:58,230 just answers questions and has a conversation with you, you could do that 986 01:03:58,230 --> 01:04:02,790 pseudocode. And as we'll soon see, you can use C, Python, any number of other 987 01:04:02,790 --> 01:04:03,749 languages, too. 988 01:04:03,750 --> 01:04:05,630 That pseudocode might look like this. 989 01:04:05,960 --> 01:04:09,720 implementing a chatbot. You could tell the chatbot, if the student says hello 990 01:04:09,720 --> 01:04:11,880 you, then say hello back. 991 01:04:12,120 --> 01:04:15,440 And the indentation, as per earlier, implies this is conditional. 992 01:04:16,020 --> 01:04:20,960 Else, if the student says goodbye to you, say goodbye to the student. Else, 993 01:04:20,960 --> 01:04:25,260 the student asks you how you are, say you are well. So you can just enumerate 994 01:04:25,260 --> 01:04:29,260 question after question after question and just handle all of these conditional 995 01:04:29,260 --> 01:04:33,500 possibilities. But things kind of escalate quickly, especially with the 996 01:04:33,500 --> 01:04:34,600 today, like ChatGPT. 997 01:04:34,800 --> 01:04:38,460 Are we really going to have the wherewithal as programmers to write 998 01:04:38,460 --> 01:04:42,180 conditional, like else if the student asked why 1, 1, 1 in binary, 7 in 999 01:04:42,360 --> 01:04:45,880 This kind of hints at, oh my god, there's an infinite number of things 1000 01:04:45,880 --> 01:04:50,720 human could ask the chatbot. Do we really have to write an infinite number 1001 01:04:50,720 --> 01:04:51,720 conditionals? 1002 01:04:52,170 --> 01:04:55,710 not possible. There's not enough time in the day. There's not enough lines of 1003 01:04:55,710 --> 01:04:56,710 code available. 1004 01:04:56,990 --> 01:05:00,750 Artificial intelligence surely needs to be able to figure some of this out 1005 01:05:00,750 --> 01:05:06,190 instead. And so indeed, this is not how you implement AI. But rather, how you 1006 01:05:06,190 --> 01:05:11,810 implement an AI like a chatbot is you typically train it based on lots and 1007 01:05:11,810 --> 01:05:16,230 of data. You give it lots of inputs, lots of inputs, training data, and let 1008 01:05:16,230 --> 01:05:20,810 figure out what it should say in response to certain questions. And it 1009 01:05:20,810 --> 01:05:24,830 down to a lot of probability, a lot of statistics, otherwise known now as large 1010 01:05:24,830 --> 01:05:28,610 language models, which, if we really peek under the hood, are actually 1011 01:05:28,610 --> 01:05:31,690 implemented with what are called neural networks, inspired by the world of 1012 01:05:31,690 --> 01:05:35,990 biology, whereby we humans have all of these neurons that transmit electrical 1013 01:05:35,990 --> 01:05:39,650 signals such that my brain tells my hand to move this way, this way, and this 1014 01:05:39,650 --> 01:05:44,030 other way. And so what computer scientists have been doing over the past 1015 01:05:44,030 --> 01:05:49,530 years is implementing in software using literally zeros and ones, graphs or 1016 01:05:49,530 --> 01:05:52,890 networks, neural networks that look a little something like this, where each 1017 01:05:52,890 --> 01:05:56,770 the circles represents a neuron, each of the arrows represents a pathway between 1018 01:05:56,770 --> 01:06:02,350 them, and provides as input to these networks huge amounts of data, like all 1019 01:06:02,350 --> 01:06:05,870 the internet, all of Wikipedia, all of the books that it might consume as 1020 01:06:06,110 --> 01:06:10,870 And then the goal of this neural network, as per this single final neuron 1021 01:06:10,870 --> 01:06:15,270 here, is to produce an answer to a question. Maybe it's simple, like yes, 1022 01:06:15,570 --> 01:06:20,670 Or maybe it's something like the answer to the 111 question, or how are you, or 1023 01:06:20,670 --> 01:06:22,790 goodbye, or hello, or the like. 1024 01:06:23,030 --> 01:06:27,630 And what these neural networks do is use statistics and probability and try to 1025 01:06:27,630 --> 01:06:32,810 output the most probabilistically likely answer to this question that's been 1026 01:06:32,810 --> 01:06:34,530 asked and really just hope. 1027 01:06:35,100 --> 01:06:36,098 that it is correct. 1028 01:06:36,100 --> 01:06:39,800 There is no programmer at OpenAI or Google or Microsoft that's trying to 1029 01:06:39,800 --> 01:06:44,300 anticipate every one of these questions we might ask, not only in English, but 1030 01:06:44,300 --> 01:06:45,880 in other languages as well. 1031 01:06:46,510 --> 01:06:49,610 So you might be wondering why there's this eight -foot duck on the stage. So 1032 01:06:49,610 --> 01:06:54,430 persona that CS50's own AI takes is, in fact, that of a rubber duck. Because it 1033 01:06:54,430 --> 01:06:58,210 turns out, in programming circles, and this is true long before CS50, it has 1034 01:06:58,210 --> 01:07:01,590 often been recommended to students and aspiring programmers that you keep 1035 01:07:01,590 --> 01:07:06,230 literally a physical rubber duck on your desk. The idea being, in the absence of 1036 01:07:06,230 --> 01:07:11,290 a friend, family member, colleague, TA who could answer technical questions for 1037 01:07:11,290 --> 01:07:14,950 you, if you're alone in your room and mather at night, you can sort of talk to 1038 01:07:14,950 --> 01:07:19,310 the duck. maybe door closed, and ask the duck your questions, or more 1039 01:07:19,310 --> 01:07:23,750 importantly, talk the duck through what confusion you're having. And the mere 1040 01:07:23,750 --> 01:07:28,390 act of talking through the problem, explaining logically what you're trying 1041 01:07:28,390 --> 01:07:32,210 do, what you're actually doing, and what the error actually is, invariably that 1042 01:07:32,210 --> 01:07:35,330 sort of proverbial light bulb goes off and you realize, oh, I'm an idiot. 1043 01:07:35,590 --> 01:07:40,030 I hear in my own words where I've gone awry. And even though this duck will 1044 01:07:40,030 --> 01:07:44,570 never say anything back to you, that alone, rubber duck debugging, or rubber 1045 01:07:44,570 --> 01:07:48,580 duck tends to be a valuable programming technique, believe it or not. 1046 01:07:48,780 --> 01:07:52,620 But thanks to these large language models, we have not only physical but 1047 01:07:52,620 --> 01:07:56,860 ducks as well. And so available to you will be, in this class, not tools like 1048 01:07:56,860 --> 01:08:00,760 ChatGPT and the like, which are, through policy, disallowed. It is not 1049 01:08:00,760 --> 01:08:05,960 reasonable to use ChatGPT and the like. But you are allowed and encouraged to 1050 01:08:05,960 --> 01:08:10,380 use CS50's own AI -based tools, which resemble those same tools but know 1051 01:08:10,380 --> 01:08:14,660 something about CS50 and aspire to behave akin to a good team. teaching 1052 01:08:14,660 --> 01:08:18,660 guiding you to solutions as opposed to handing you something outright. 1053 01:08:18,939 --> 01:08:22,439 So this is a tool that will be available at literally this URL throughout the 1054 01:08:22,439 --> 01:08:23,660 course, cs50 .ai. 1055 01:08:23,920 --> 01:08:27,800 It will also be embedded in the programming environment you'll soon 1056 01:08:27,800 --> 01:08:31,840 is called Visual Studio Code, a cloud -based version thereof. The duck will 1057 01:08:31,840 --> 01:08:36,560 in that environment as well, as well as on stage from time to time, which is to 1058 01:08:36,560 --> 01:08:41,200 say we'll not only talk about but use throughout the course this thing known 1059 01:08:41,200 --> 01:08:42,200 AI. 1060 01:08:42,279 --> 01:08:46,020 But this is ultimately code that we're going to start writing next week. And 1061 01:08:46,020 --> 01:08:48,620 unfortunately, this code here is written in a language called C. 1062 01:08:48,880 --> 01:08:52,540 This is essentially the program that I lost two points on, like some 25 plus 1063 01:08:52,540 --> 01:08:53,299 years ago. 1064 01:08:53,300 --> 01:08:59,080 And it does look admittedly cryptic. And that's why today what we'll focus on is 1065 01:08:59,080 --> 01:09:04,000 not what this code looks like, nor the zeros and ones that that code gets 1066 01:09:04,000 --> 01:09:08,439 converted to so your computer can understand as input what you want it to 1067 01:09:08,520 --> 01:09:12,370 We're going to focus on a much more visual incarnation of this. But I know 1068 01:09:12,370 --> 01:09:16,010 far this has been a lot. So let's go ahead and take a five minute break here. 1069 01:09:16,170 --> 01:09:19,170 And when we come back in five, we'll do some actual programming. 1070 01:09:19,430 --> 01:09:20,430 So see you in five. 1071 01:09:21,450 --> 01:09:22,950 All right. 1072 01:09:24,290 --> 01:09:27,770 So it's now time to solve with actual code. 1073 01:09:28,350 --> 01:09:33,069 some actual problems, albeit in a fun and visual and audio way. But recall 1074 01:09:33,069 --> 01:09:34,210 where we left off was this. 1075 01:09:34,430 --> 01:09:38,569 Starting next week, you'll be writing code that ultimately looks like this. 1076 01:09:38,569 --> 01:09:42,290 thankfully, you will not be writing zeros and ones. And no normal person, 1077 01:09:42,290 --> 01:09:46,189 included, can understand what all of these zeros and ones are at a glance. 1078 01:09:46,490 --> 01:09:49,910 We could take out some paper pencil and probably figure it out very tediously. 1079 01:09:50,270 --> 01:09:52,149 But this is exactly the point. 1080 01:09:52,710 --> 01:09:56,690 Computers only understand this stuff. But what we as programmers will start 1081 01:09:56,690 --> 01:10:00,930 writing today and beyond is code at a higher level. 1082 01:10:01,190 --> 01:10:05,530 And indeed, this is going to be frequent within computer science, where there's 1083 01:10:05,530 --> 01:10:09,570 different levels of abstraction that we operate at. And the lowest level, the 1084 01:10:09,570 --> 01:10:12,110 nittiest gritty, is the zeros and ones the computer understands. 1085 01:10:12,470 --> 01:10:16,190 That's it in this class for zeros and ones. Hopefully, you at least have 1086 01:10:16,190 --> 01:10:22,250 your mind around why zeros and ones can be used in triples and in bytes to 1087 01:10:22,250 --> 01:10:23,330 represent higher and higher numbers. 1088 01:10:23,630 --> 01:10:28,720 But let's just now agree that computers can do that. let's abstract away from 1089 01:10:28,720 --> 01:10:33,240 that detail and focus on higher level languages than zeros and ones, namely a 1090 01:10:33,240 --> 01:10:36,400 language like this. So this is an example of the very first programming 1091 01:10:36,400 --> 01:10:40,020 I learned back in the day as per that homework in a language called C. 1092 01:10:40,320 --> 01:10:44,380 It's an older language, but it remains one of the most popular languages and 1093 01:10:44,380 --> 01:10:48,960 omnipresent languages nowadays because it's incredibly fast, and it's 1094 01:10:48,960 --> 01:10:52,700 particularly good at making devices operate quickly. 1095 01:10:53,520 --> 01:10:58,100 For us pedagogically, the value of C is not that you're probably in Silicon 1096 01:10:58,100 --> 01:11:02,460 Valley and other such jobs going to be using C yourself that much, but because 1097 01:11:02,460 --> 01:11:06,600 it's going to provide a conceptual foundation on top of which we introduce 1098 01:11:06,600 --> 01:11:10,760 languages like Python, which is newer and improved, so to speak, that gives 1099 01:11:10,760 --> 01:11:15,740 more and more functionality sort of for free out of the box by abstracting away 1100 01:11:15,740 --> 01:11:19,660 some of the stuff we'll focus on in the coming days first. So at the end of the 1101 01:11:19,660 --> 01:11:24,100 day, you should better understand languages like Python. and JavaScript 1102 01:11:24,100 --> 01:11:28,360 because of your underlying understanding of a language like C. 1103 01:11:28,860 --> 01:11:32,240 But this is too much for the first day. Many of you will think that this is too 1104 01:11:32,240 --> 01:11:37,260 much for the second week. But in fact, C is really only sort of scary looking 1105 01:11:37,260 --> 01:11:41,460 because all this darn punctuation in syntax, the semicolon, the parentheses, 1106 01:11:41,460 --> 01:11:43,880 double quotes, the curly braces, and the like. And I concur. 1107 01:11:44,100 --> 01:11:48,360 This is intellectually uninteresting. And a lot of the challenges early on 1108 01:11:48,360 --> 01:11:52,080 learning programming is you just don't have the muscle memory like that I or 1109 01:11:52,080 --> 01:11:55,900 some of the teaching fellows might, for knowing what symbols should be where. 1110 01:11:56,100 --> 01:12:01,100 But that's going to come with time and practice. I guarantee it. What we'll do 1111 01:12:01,100 --> 01:12:05,940 for today, though, is just throw away all of that intellectually uninteresting 1112 01:12:05,940 --> 01:12:08,000 detail and focus really on ideas. 1113 01:12:08,320 --> 01:12:11,660 And some of you might be in your comfort zone here, because if back in middle 1114 01:12:11,660 --> 01:12:14,880 school you were playing with a programming language called Scratch, you 1115 01:12:14,880 --> 01:12:18,440 probably using it at the time just to have fun in class or out of class, 1116 01:12:18,440 --> 01:12:22,430 games, animations, interactive art, what you probably probably didn't use it 1117 01:12:22,430 --> 01:12:26,270 for, at least in middle school, was to consider and explore programming 1118 01:12:26,270 --> 01:12:27,270 languages themselves. 1119 01:12:27,610 --> 01:12:31,430 But what's wonderful about Scratch, which is this graphical programming 1120 01:12:31,430 --> 01:12:35,230 from down the street at MIT where it was invented some years ago, is you can 1121 01:12:35,230 --> 01:12:39,690 program not by using your keyboard, per se, but by dragging and dropping puzzle 1122 01:12:39,690 --> 01:12:43,990 pieces, otherwise known as blocks, that will snap together if it makes logical 1123 01:12:43,990 --> 01:12:48,350 sense to do so. And what you won't have to deal with is parentheses and double 1124 01:12:48,350 --> 01:12:50,190 quotes and semicolons and all of that. 1125 01:12:50,510 --> 01:12:54,270 at least until next week. But the nice thing about Scratch is that after this 1126 01:12:54,270 --> 01:12:57,450 week and after the so -called problems at zero, the first assignment in which 1127 01:12:57,450 --> 01:13:02,190 you'll use Scratch, you'll have a mental model via which it will be easier to 1128 01:13:02,190 --> 01:13:04,750 pick up all of the subsequent syntax as well. 1129 01:13:04,970 --> 01:13:09,410 So let's see how we can start programming in Scratch by making the 1130 01:13:09,410 --> 01:13:10,309 programs first. 1131 01:13:10,310 --> 01:13:11,310 You can do this. 1132 01:13:11,600 --> 01:13:13,400 at scratch .mit .edu. 1133 01:13:13,680 --> 01:13:16,700 You needn't do this now in the moment. Problems at zero will walk you through 1134 01:13:16,700 --> 01:13:17,659 all of these steps. 1135 01:13:17,660 --> 01:13:23,560 But what I've done here is opened up at scratch .mit .edu, precisely the default 1136 01:13:23,560 --> 01:13:28,660 web page therein. This is after having clicked the Create button in Scratch, 1137 01:13:28,680 --> 01:13:32,500 which is going to allow me to create my first program. But first, a tour of the 1138 01:13:32,500 --> 01:13:37,140 user interface here and what is ultimately available to you. Well, 1139 01:13:37,140 --> 01:13:38,140 Scratch environment, 1140 01:13:38,540 --> 01:13:42,880 We'll see a few different regions of the screen. One, we have this palette of 1141 01:13:42,880 --> 01:13:44,020 puzzle pieces at left. 1142 01:13:44,240 --> 01:13:45,920 The blue ones relate to motion. 1143 01:13:46,160 --> 01:13:50,740 The purple ones relate to looks. The pink ones relate to sound and so forth. 1144 01:13:50,740 --> 01:13:54,700 the color of the block just roughly categorizes what that block's purpose in 1145 01:13:54,700 --> 01:13:55,559 life is. 1146 01:13:55,560 --> 01:13:58,820 We're going to be able to use those puzzle pieces by dragging and dropping 1147 01:13:58,820 --> 01:13:59,860 from left to right. 1148 01:14:00,140 --> 01:14:03,060 In the right here, in the middle of the screen, is where I'm going to write my 1149 01:14:03,060 --> 01:14:06,360 actual programs. This is where I'll drag and drop these puzzle pieces, lock them 1150 01:14:06,360 --> 01:14:08,630 together, and actually write them. write my code. 1151 01:14:09,150 --> 01:14:12,530 What am I going to be coding? Well, I'm going to be controlling one or more 1152 01:14:12,530 --> 01:14:15,870 sprites. Much like in the world of games, if familiar, a sprite is like a 1153 01:14:15,870 --> 01:14:17,390 character that you might see on the screen. 1154 01:14:17,630 --> 01:14:21,510 The default character in the world of Scratch is, in fact, a cat that looks 1155 01:14:21,510 --> 01:14:26,330 this. And if, in this case, I have just one cat, I can then make that cat do 1156 01:14:26,330 --> 01:14:30,230 things in his own little world at top right by making the cat move up, down, 1157 01:14:30,390 --> 01:14:34,130 left, right, spinning around, or doing other things as well. But if you want to 1158 01:14:34,130 --> 01:14:37,930 introduce a dog or a bird or any number of other custom characters, You just add 1159 01:14:37,930 --> 01:14:41,270 more sprites, and they get their own place in that same world. 1160 01:14:41,570 --> 01:14:45,190 As for how to think about movement in this world, it's actually pretty 1161 01:14:45,230 --> 01:14:48,650 even though it gets a little numeric for a moment. If scratch at the moment is 1162 01:14:48,650 --> 01:14:53,190 in the middle of the screen, the cat is at 0 comma 0. If you think about xy 1163 01:14:53,190 --> 01:14:56,790 coordinates or latitude longitude, if you move the cat all the way up, this 1164 01:14:56,790 --> 01:14:59,790 would still be x equals 0. But it would be y 180. 1165 01:15:00,250 --> 01:15:01,188 What's the 180? 1166 01:15:01,190 --> 01:15:06,880 180 pixels vertically, or dots on the screen. This is negative 180 pixels. on 1167 01:15:06,880 --> 01:15:10,700 the screen at the bottom. By contrast, if you go left and right, your x value 1168 01:15:10,700 --> 01:15:17,620 might change, negative 240, but y is 0, or positive 240, and y is 0 as well. But 1169 01:15:17,620 --> 01:15:21,000 most of the time, you won't need to know or care about what the pixel 1170 01:15:21,000 --> 01:15:22,460 coordinates of the cat are. 1171 01:15:22,700 --> 01:15:26,340 All you're generally going to care about, as the programmer most likely, is 1172 01:15:26,340 --> 01:15:30,540 you want the cat to go relatively up, down, left, or right, and let MIT figure 1173 01:15:30,540 --> 01:15:33,460 out the mathematics of moving this thing around in most cases. 1174 01:15:34,040 --> 01:15:37,800 All right, so let's go ahead and introduce the first of these programs by 1175 01:15:37,800 --> 01:15:43,020 something quite simple, as we did in C there, but a little more simply by 1176 01:15:43,020 --> 01:15:44,020 writing code as follows. 1177 01:15:44,260 --> 01:15:46,620 I'm going to go back to scratch .mit .edu. 1178 01:15:46,900 --> 01:15:49,360 I've already clicked per before the Create button. 1179 01:15:49,950 --> 01:15:54,070 And if I click on the yellow category of blocks here at left, and I'll zoom in, 1180 01:15:54,190 --> 01:15:57,890 we'll see a whole bunch of yellow puzzle pieces. And probably the most common 1181 01:15:57,890 --> 01:16:01,890 one you will use to write code in Scratch for just this first week is 1182 01:16:01,890 --> 01:16:03,610 when green flag clicked. 1183 01:16:03,850 --> 01:16:08,110 Why? Well, if we go back over to the cat's world at top right, notice that 1184 01:16:08,110 --> 01:16:12,710 the cat's rectangular world, there's not only a green flag for starting, there's 1185 01:16:12,710 --> 01:16:13,710 a red stop sign. 1186 01:16:14,000 --> 01:16:18,120 for stopping as well. So let's do this. Let me go ahead and click and drag when 1187 01:16:18,120 --> 01:16:20,680 green flag clicked anywhere into the middle and let go. 1188 01:16:21,100 --> 01:16:23,560 And now I'm going to go to look. 1189 01:16:24,020 --> 01:16:27,420 And it looks like there's a whole bunch of purple puzzle pieces here. I'm going 1190 01:16:27,420 --> 01:16:31,980 to choose something simple like say hello, drag it. And notice if I get just 1191 01:16:31,980 --> 01:16:35,620 close enough, it's going to want to magnetically snap together. So I'll just 1192 01:16:35,620 --> 01:16:39,160 that. And it does its thing. The fact that there's this white oval with text 1193 01:16:39,160 --> 01:16:43,400 means that is an input to this say puzzle piece. 1194 01:16:43,790 --> 01:16:47,930 I can literally then change what the input is if I want to more 1195 01:16:47,930 --> 01:16:52,450 say hello comma world, which in fact, according to Lore, was the very first 1196 01:16:52,450 --> 01:16:57,950 program written in C. And nowadays, in most every language, including in Brian 1197 01:16:57,950 --> 01:17:01,770 Kernaghan's book, so hello world is generally the first program that most 1198 01:17:01,770 --> 01:17:03,350 programmer first writes. 1199 01:17:03,570 --> 01:17:07,710 So that's it. As programs go, let me go ahead and zoom out here. Let me go over 1200 01:17:07,710 --> 01:17:11,310 to the right and click the green flag. And somewhat excitingly, maybe 1201 01:17:11,310 --> 01:17:15,780 underwhelmingly, We've now written a program that quite simply says, hello 1202 01:17:15,780 --> 01:17:16,820 world, on the screen. 1203 01:17:17,020 --> 01:17:20,200 Now let's make this a little more technical for just a moment. What is 1204 01:17:20,200 --> 01:17:21,820 here, puzzle piece, as I keep calling it? 1205 01:17:22,180 --> 01:17:27,560 It's actually a similar, it's an incarnation of one of the ideas from our 1206 01:17:27,560 --> 01:17:28,560 pseudocode before. 1207 01:17:28,840 --> 01:17:33,460 What did we call those actions and verbs last time in my pseudocode? 1208 01:17:34,510 --> 01:17:35,550 Functions, that's right. 1209 01:17:35,770 --> 01:17:41,290 So these purple puzzle pieces here are indeed functions. And some functions, as 1210 01:17:41,290 --> 01:17:45,890 we can see, take inputs, like hello comma world. After all, how does Scratch 1211 01:17:45,890 --> 01:17:50,030 know what to say? You have to provide the cat with input, which is to say 1212 01:17:50,030 --> 01:17:53,710 functions can indeed take inputs like this. In this case, one input, but we'll 1213 01:17:53,710 --> 01:17:56,350 see opportunities for passing in more input as well. 1214 01:17:56,630 --> 01:18:00,630 What the cat is doing, though, visually on the screen here at top right is 1215 01:18:00,630 --> 01:18:02,010 what's generally called a side effect. 1216 01:18:02,250 --> 01:18:05,550 Sometimes when you call a function, it does something visually. 1217 01:18:05,870 --> 01:18:09,470 And in this case, you're seeing literally a cartoon speech bubble, hello 1218 01:18:09,630 --> 01:18:11,710 That is the side effect of this function. 1219 01:18:11,950 --> 01:18:15,690 So if we now want to map this to our world of inputs and outputs and see 1220 01:18:15,690 --> 01:18:19,190 this side effect is, this is the paradigm I proposed at the start of 1221 01:18:19,190 --> 01:18:23,610 is computer science in a nutshell and will be the framework we use literally 1222 01:18:23,610 --> 01:18:25,590 throughout the class, no matter how... 1223 01:18:26,380 --> 01:18:28,420 no matter how the languages in particular evolve. 1224 01:18:28,700 --> 01:18:31,400 So what's the input to this particular program? 1225 01:18:31,680 --> 01:18:36,180 Well, this white oval, hello world is my input. The algorithm, step -by -step 1226 01:18:36,180 --> 01:18:40,500 instructions for solving some problem, is implemented in code, this language 1227 01:18:40,500 --> 01:18:44,800 called Scratch, by way of this purple puzzle piece. And the output... 1228 01:18:45,050 --> 01:18:49,490 of that function, given this input, is the side effect whereby the cat indeed 1229 01:18:49,490 --> 01:18:54,990 says hello world visually on the screen in that speech bubble. So the exact same 1230 01:18:54,990 --> 01:18:59,350 paradigm with which we began today governs how exactly this cat here works. 1231 01:18:59,630 --> 01:19:02,070 Well, let's actually go back to this program and make it a little more 1232 01:19:02,070 --> 01:19:03,070 interesting than that. 1233 01:19:03,470 --> 01:19:05,190 Let me go ahead and click the red stop sign. 1234 01:19:05,530 --> 01:19:09,810 And let me actually use a different type of puzzle piece, another function that 1235 01:19:09,810 --> 01:19:10,830 does something a little different. 1236 01:19:11,170 --> 01:19:13,050 First, I'm going to get rid of the say block. 1237 01:19:13,350 --> 01:19:16,650 So I'm going to not only pull it away, I'm going to drag it over anywhere it 1238 01:19:16,650 --> 01:19:18,530 left and just let go, and it will delete itself. 1239 01:19:19,460 --> 01:19:22,860 automatically, or I could right click or control click. And from a little menu, 1240 01:19:22,980 --> 01:19:24,820 I could also explicitly say delete. 1241 01:19:25,140 --> 01:19:30,280 And what I'm going to do now is under sensing, which is a light blue shade of 1242 01:19:30,280 --> 01:19:32,960 puzzle piece. There's a whole bunch here, but I'm going to focus on this 1243 01:19:33,520 --> 01:19:35,480 Ask something and wait. 1244 01:19:36,010 --> 01:19:39,150 And the default text is, what's your name? And that's fine. But because it's 1245 01:19:39,150 --> 01:19:42,930 white oval, that input can be manually changed by me if I wanted to change the 1246 01:19:42,930 --> 01:19:46,310 question. I'm going to drag it over here. It's going to magnetically snap 1247 01:19:46,310 --> 01:19:51,770 together. And I'm OK with that question. But what do I want to say with the 1248 01:19:51,770 --> 01:19:55,130 answer? Well, let's go ahead and do this. I could go to looks again. 1249 01:19:55,630 --> 01:20:00,410 I could grab another say block, let it snap in. And I could say something like, 1250 01:20:00,510 --> 01:20:02,670 hello, David. 1251 01:20:03,770 --> 01:20:07,110 But this is going to be the first of many bugs that I make, intentionally or 1252 01:20:07,110 --> 01:20:09,030 otherwise. Let me click the green flag. 1253 01:20:10,050 --> 01:20:14,210 Scratch is now, just like in a web browser, prompting me for some input 1254 01:20:14,210 --> 01:20:15,910 let me go ahead and type in my name, David. 1255 01:20:16,250 --> 01:20:18,530 Enter. And voila, like it worked. 1256 01:20:18,990 --> 01:20:19,990 Hello, David. 1257 01:20:20,210 --> 01:20:24,530 I'm kind of cheating though, right? Because if I zoom out, stop, and play 1258 01:20:24,790 --> 01:20:27,010 let me type in like Yulia's name here. 1259 01:20:27,290 --> 01:20:30,040 Enter. And it still says hello comma David. 1260 01:20:30,260 --> 01:20:34,120 So that didn't really implement the idea that I wanted. All right, so how can I 1261 01:20:34,120 --> 01:20:37,480 fix this? Well, it seems that this time I want more than a side effect. 1262 01:20:37,680 --> 01:20:42,220 I want to use the value that the human types in. And for this, we need another 1263 01:20:42,220 --> 01:20:45,670 feature of functions, which is that not only can they sometimes have Side 1264 01:20:45,670 --> 01:20:47,030 effect, something visually happens. 1265 01:20:47,350 --> 01:20:52,870 Some functions can hand you back a value, a so -called return value, that 1266 01:20:52,870 --> 01:20:58,870 allow you to actually reuse whatever the human typed in. So a return value is 1267 01:20:58,870 --> 01:21:02,250 something that gets virtually handed back to you. And you can store it in 1268 01:21:02,250 --> 01:21:04,850 something called a variable, like x, y, and z in mathematics. 1269 01:21:05,110 --> 01:21:06,750 And you can generally reuse it. 1270 01:21:07,020 --> 01:21:08,020 one or more times. 1271 01:21:08,200 --> 01:21:13,160 So let me actually draw our attention then to, at left, not only the blue 1272 01:21:13,160 --> 01:21:17,040 piece, ask what's your name and wait, but notice that there's a special puzzle 1273 01:21:17,040 --> 01:21:19,800 piece below it, this blue oval called answer. 1274 01:21:20,060 --> 01:21:23,520 And that represents what a computer scientist would call a return value. 1275 01:21:23,800 --> 01:21:28,080 So MIT has kind of bundled it together side by side to make clear that one of 1276 01:21:28,080 --> 01:21:29,480 those pieces relates to the other. 1277 01:21:29,780 --> 01:21:31,640 What it means is that I can do this. 1278 01:21:31,880 --> 01:21:37,730 I can drag this oval and use this oval as the input to the Now, notice it's not 1279 01:21:37,730 --> 01:21:39,810 the same size, but it is the right shape. So that's OK. 1280 01:21:40,390 --> 01:21:43,470 Scratch will sort of grow or shrink things to fit properly. 1281 01:21:44,090 --> 01:21:45,690 But this too isn't quite right. 1282 01:21:45,970 --> 01:21:47,170 Let me go ahead and do this. 1283 01:21:47,750 --> 01:21:51,310 Let me go ahead and stop that, click the green flag. I'll type in my name again, 1284 01:21:51,450 --> 01:21:52,930 D -A -V -I -D, Enter. 1285 01:21:53,880 --> 01:21:55,900 And it's just kind of weird or rude. 1286 01:21:56,120 --> 01:21:59,920 I wanted a hello, at least, and it just said David on the screen. OK, so I can 1287 01:21:59,920 --> 01:22:02,300 fix that. Let me stop with the red stop sign. 1288 01:22:02,580 --> 01:22:04,280 Let me just separate these temporarily. 1289 01:22:04,520 --> 01:22:07,400 And I can leave it in the middle there, but they have no logical connection 1290 01:22:07,400 --> 01:22:10,080 temporarily. Let me go back up to look. 1291 01:22:10,440 --> 01:22:12,800 Let me grab a say block, a second one. 1292 01:22:13,040 --> 01:22:16,700 And let me go ahead and say, just to be grammatical, like hello, comma, space. 1293 01:22:17,140 --> 01:22:19,000 And then I'll reconnect this here. 1294 01:22:19,260 --> 01:22:23,800 So at the moment, it looks like what I want. I want a hello, comma, and then 1295 01:22:23,800 --> 01:22:28,400 return value printed out based on whatever the human typed in. So let me 1296 01:22:28,400 --> 01:22:29,980 out. Let me click the green flag. 1297 01:22:30,760 --> 01:22:36,560 Again, what's your name? D -A -V -I -D. And watch the cat side effect enter. 1298 01:22:38,460 --> 01:22:40,680 still not greeting me properly. 1299 01:22:41,440 --> 01:22:44,200 There's no hello. And if in case it was too fast, let's do it again. 1300 01:22:44,520 --> 01:22:48,700 Green flag, D -A -V -I -D, enter. It just, it rudely just says my name, which 1301 01:22:48,700 --> 01:22:52,120 weird. What's the bug here though? It's a little more subtle. 1302 01:22:53,700 --> 01:22:54,700 Why? Yeah. 1303 01:22:56,390 --> 01:23:00,030 Yeah, it's just too quickly going over the say command or the say function in 1304 01:23:00,030 --> 01:23:03,170 this case. My Mac, your PC, your phone, it's just so darn fast. 1305 01:23:03,430 --> 01:23:07,190 Both are happening, but too fast for my human eyes to even notice. So we can 1306 01:23:07,190 --> 01:23:08,850 solve this in a number of ways. 1307 01:23:09,090 --> 01:23:11,210 I could actually use a different puzzle piece altogether. 1308 01:23:11,570 --> 01:23:15,010 In fact, MIT kind of anticipated this. Notice the first puzzle piece in purple 1309 01:23:15,010 --> 01:23:18,410 is say hello for a specific number of seconds. 1310 01:23:18,630 --> 01:23:21,710 And you can specify not just the message, but the number of seconds, ergo 1311 01:23:21,710 --> 01:23:26,560 inputs, otherwise now known as arguments to a function, an input. to a function 1312 01:23:26,560 --> 01:23:27,760 is just an argument now. 1313 01:23:28,240 --> 01:23:33,220 And that would be a fix here. I could maybe a little more explicitly do this. 1314 01:23:33,220 --> 01:23:38,020 could go under Events, scroll down a little bit. And sorry, under Control in 1315 01:23:38,020 --> 01:23:39,920 orange, I could grab a weight block. 1316 01:23:40,220 --> 01:23:44,160 And I could kind of insert it in the middle. And this might actually help. So 1317 01:23:44,160 --> 01:23:47,080 could click on the green flag, D -A -V -I -D, Enter. 1318 01:23:47,460 --> 01:23:48,460 Hello. 1319 01:23:49,000 --> 01:23:52,400 And I can change the timing to be a little more natural. But what if I want 1320 01:23:52,400 --> 01:23:55,860 cat to just say, hello, comma, David, all in one breath, so to speak? 1321 01:23:56,200 --> 01:23:59,480 Well, for that, I'm going to need to use a slightly different technique as 1322 01:23:59,480 --> 01:24:03,020 follows. Let me go ahead and get rid of the wait. Let me get rid of the second 1323 01:24:03,020 --> 01:24:05,440 say block and stop the cat with the stop sign. 1324 01:24:05,800 --> 01:24:07,760 Let me go under operators here. 1325 01:24:08,380 --> 01:24:13,060 And let me somewhat cleverly grab this, a join block at the bottom. By default, 1326 01:24:13,140 --> 01:24:16,340 it's using apple and banana as placeholders, but those are white ovals, 1327 01:24:16,340 --> 01:24:21,860 change those. Let me drag this over the white oval for the save function and let 1328 01:24:21,860 --> 01:24:23,140 go, and it will snap to fill. 1329 01:24:23,460 --> 01:24:28,060 Let me go ahead here and type hello comma space instead of apple. 1330 01:24:28,520 --> 01:24:30,500 And what should I do instead of banana? 1331 01:24:32,380 --> 01:24:34,680 Yeah, so the answer return value. 1332 01:24:36,420 --> 01:24:38,960 So let me go under sensing again. 1333 01:24:39,240 --> 01:24:42,420 Let me just drag another copy of it. And you can use these again and again and 1334 01:24:42,420 --> 01:24:43,420 again. They don't disappear. 1335 01:24:43,480 --> 01:24:49,840 I want to drag answer over banana so that the second input to join is 1336 01:24:49,940 --> 01:24:52,700 if you will, the output of the ask block. 1337 01:24:53,150 --> 01:24:53,809 Like that. 1338 01:24:53,810 --> 01:24:54,810 And it snaps to fit. 1339 01:24:55,090 --> 01:24:59,050 So now, if I go ahead and click the green flag once more, D -A -V -I -D, 1340 01:24:59,330 --> 01:25:03,390 now we have the sort of behavior aesthetically that I cared about. 1341 01:25:03,650 --> 01:25:07,370 But beyond the aesthetics of this, the goal here really was to map it to, 1342 01:25:07,450 --> 01:25:09,950 the same paradigm, which we'll see here. 1343 01:25:10,250 --> 01:25:14,370 The algorithm and the output and the input for this example are as follows. 1344 01:25:14,370 --> 01:25:16,970 input to the say block was quote unquote, what's your name? 1345 01:25:17,230 --> 01:25:21,430 The function, of course, implementing that algorithm in code was the ask and 1346 01:25:21,430 --> 01:25:22,430 wait block. 1347 01:25:22,540 --> 01:25:27,160 The output, though, of the ask block, recall, is not some visual side effect. 1348 01:25:27,160 --> 01:25:31,960 is a return value called answer, like a variable, a special variable like x, y, 1349 01:25:31,960 --> 01:25:36,240 and z in math. But in this one, we generally, in programming, describe 1350 01:25:36,240 --> 01:25:38,140 with actual words, not just letters. 1351 01:25:38,440 --> 01:25:44,800 But this output of the say block, I kind of want to make room for it to pass it 1352 01:25:44,800 --> 01:25:48,360 into the say block as a second argument. So let's do this. Let's take one step 1353 01:25:48,360 --> 01:25:53,950 back and propose that now for the join block, that I just used, it takes two 1354 01:25:53,950 --> 01:25:56,530 inputs, hello comma space and answer. 1355 01:25:56,930 --> 01:25:59,510 The function in question is indeed the join block. 1356 01:25:59,850 --> 01:26:03,690 The output of this had better be hello comma David. What do I want to do with 1357 01:26:03,690 --> 01:26:05,070 the output of the join block? 1358 01:26:05,290 --> 01:26:06,710 Well, let me clear the screen. 1359 01:26:07,010 --> 01:26:12,210 Let me move this over, because now the output of the join block is going to 1360 01:26:12,210 --> 01:26:16,870 instantly become the input to the save block so that the output now in this 1361 01:26:16,870 --> 01:26:20,590 multi -step process is the side effect of hello David. 1362 01:26:21,280 --> 01:26:26,960 So the fact that I nested these blocks on top of one another was very much 1363 01:26:26,960 --> 01:26:31,860 deliberate. If I zoom in here, notice that hello and answer are on top of 1364 01:26:32,140 --> 01:26:36,020 Join is on top of the say block. And if you think back to high school math, this 1365 01:26:36,020 --> 01:26:39,140 is like when you had parentheses and you had to do the things inside parentheses 1366 01:26:39,140 --> 01:26:42,640 before the things outside parentheses. It's the same idea, but I'm just 1367 01:26:42,640 --> 01:26:44,200 stacking them instead. 1368 01:26:44,560 --> 01:26:48,920 But output can become input depending on what the function there expects. 1369 01:26:49,400 --> 01:26:53,360 Let me pause here and see if there's any questions about, not so much like what 1370 01:26:53,360 --> 01:26:56,740 the cat is doing, but how the cat is doing this. 1371 01:26:58,900 --> 01:27:00,120 Questions at hand. 1372 01:27:01,700 --> 01:27:03,360 All right, well let's... 1373 01:27:03,720 --> 01:27:07,600 Let's make the cat more cat -like and do this. Let me throw away all the say 1374 01:27:07,600 --> 01:27:09,120 block and just let go there. 1375 01:27:09,360 --> 01:27:12,660 And let me introduce at bottom left a nice feature of Scratch whereby there's 1376 01:27:12,660 --> 01:27:15,960 also these extensions that tend to use the cloud, the internet, to give you 1377 01:27:15,960 --> 01:27:16,960 more functionality. 1378 01:27:17,040 --> 01:27:19,960 And in fact, I'm going to click on this extension up here, text to speech. 1379 01:27:20,220 --> 01:27:24,420 And if I click on that, I suddenly get a whole new category of blocks at the 1380 01:27:24,420 --> 01:27:25,480 bottom, text to speech. 1381 01:27:25,700 --> 01:27:26,780 They happen to be green. 1382 01:27:27,040 --> 01:27:31,200 But what's nice here is that I can actually now have the cat say something 1383 01:27:31,200 --> 01:27:32,200 audibly. 1384 01:27:33,160 --> 01:27:35,440 a speak block here instead of the say block. 1385 01:27:35,880 --> 01:27:37,640 I don't want it to just say hello. 1386 01:27:37,980 --> 01:27:38,980 Let me stop that. 1387 01:27:39,040 --> 01:27:43,060 So let me go back under Operators. Let me grab another join block, because I 1388 01:27:43,060 --> 01:27:44,060 threw the other one away. 1389 01:27:44,140 --> 01:27:46,620 Let me change apple to hello comma space again. 1390 01:27:46,860 --> 01:27:50,420 Let me go to Sensing. Let me drag answer to banana again. 1391 01:27:50,800 --> 01:27:52,720 And now let me hit the green flag. 1392 01:27:53,420 --> 01:27:55,400 And let me type in my name, D -A -V -I -D. 1393 01:27:55,700 --> 01:27:59,300 And in a moment, I'll hit Enter. And hello, David. 1394 01:28:00,220 --> 01:28:04,240 All right, it's not exactly cat -like, but it was synthesized. But it turns out 1395 01:28:04,240 --> 01:28:06,240 under these text -to -speech blocks, there's some others. 1396 01:28:06,720 --> 01:28:11,220 Set voice to Alto, for instance, seems to be the default. But let's change 1397 01:28:11,380 --> 01:28:14,880 So notice that some puzzle pieces don't just take white ovals. They might even 1398 01:28:14,880 --> 01:28:19,360 have dropdowns. So whoever created that puzzle piece decided in advance what the 1399 01:28:19,360 --> 01:28:23,160 available choices are for that input per the dropdown. 1400 01:28:23,440 --> 01:28:27,060 So I'm going to change it to squeak, or actually kitten. Sounds even more apt. 1401 01:28:27,220 --> 01:28:28,220 Let me zoom out. 1402 01:28:28,670 --> 01:28:29,670 Click the green flag. 1403 01:28:30,490 --> 01:28:32,270 Type my name, D -A -V -I -D. Enter. 1404 01:28:32,650 --> 01:28:33,650 Meow, meow. 1405 01:28:35,430 --> 01:28:39,070 That's interesting. So it doesn't seem to matter what I type. So how about 1406 01:28:39,070 --> 01:28:41,130 Malin? Meow, meow, meow. 1407 01:28:42,150 --> 01:28:46,850 So it seems to meow proportional to how long the phrase is that I typed in. It 1408 01:28:46,850 --> 01:28:50,950 can get a little creepy quickly. Like if I change kitten to giant, let me go 1409 01:28:50,950 --> 01:28:52,710 ahead and hit play. D -A -V -I -D. 1410 01:28:53,290 --> 01:28:54,290 Enter. 1411 01:28:54,390 --> 01:28:55,450 Hello, David. 1412 01:28:56,590 --> 01:28:57,890 So you can sort of... 1413 01:28:58,520 --> 01:29:01,880 for very non -academic ways, start to have fun with this, but just playing 1414 01:29:01,880 --> 01:29:04,900 around with these various inputs and outputs. But let's actually make the cat 1415 01:29:04,900 --> 01:29:08,420 something more cat -like and indeed meow instead of saying any words at all. So 1416 01:29:08,420 --> 01:29:09,600 let me throw all of that away. 1417 01:29:09,840 --> 01:29:11,360 Let me go now under sound. 1418 01:29:11,580 --> 01:29:15,060 Let me drag the play sound until done. 1419 01:29:15,340 --> 01:29:18,320 And notice in the dropdown here, by default, you just get the cat sound. 1420 01:29:18,540 --> 01:29:22,000 You can record your own sounds. There's a whole library of dogs and birds and 1421 01:29:22,000 --> 01:29:24,440 all sorts of sounds you can import into the program. 1422 01:29:24,640 --> 01:29:25,840 I'll keep it simple with cat. 1423 01:29:26,060 --> 01:29:27,320 And let me click the green flag. 1424 01:29:29,170 --> 01:29:32,730 All right. So the cat meowed once. If I want the cat to meow again, I could do 1425 01:29:32,730 --> 01:29:33,730 this. 1426 01:29:34,390 --> 01:29:37,150 If I want the cat to meow a third time, I could again hit play. 1427 01:29:38,340 --> 01:29:42,160 So this is kind of tedious. If to play this game, I have to keep clicking the 1428 01:29:42,160 --> 01:29:46,180 button, keep clicking the button to keep the cat alive virtually in this way. So 1429 01:29:46,180 --> 01:29:48,520 maybe I want this to happen again and again and again. 1430 01:29:48,760 --> 01:29:51,040 Well, let me just do that. Let me sort of drag and drop. 1431 01:29:51,260 --> 01:29:55,000 Or I could right click or Control click, and then a little menu would let me 1432 01:29:55,000 --> 01:29:56,680 copy paste or duplicate blocks. 1433 01:29:56,900 --> 01:29:58,040 But I'll just keep dragging and dropping. 1434 01:29:58,480 --> 01:29:59,480 Let's do this. 1435 01:30:01,680 --> 01:30:03,260 Cat's kind of hungry, unhappy. 1436 01:30:03,600 --> 01:30:08,740 So let's slow things down so it's adorable again. So let me go under 1437 01:30:09,260 --> 01:30:12,480 Let me grab one of those wait one second, and I'll plop this here. 1438 01:30:12,880 --> 01:30:14,520 Another one, let me plop it here. 1439 01:30:14,800 --> 01:30:15,800 Click Play again. 1440 01:30:18,560 --> 01:30:20,600 Cuter. Less hungry? Sure. 1441 01:30:20,860 --> 01:30:25,180 But this program is now, I dare say, correct, if my goal is to get the cat to 1442 01:30:25,180 --> 01:30:26,380 meow three times. 1443 01:30:26,660 --> 01:30:30,760 But now, even if you've never programmed before, critique this program. 1444 01:30:31,040 --> 01:30:34,520 It is not well -designed, even though it is correct. 1445 01:30:34,960 --> 01:30:36,780 In other words, it could be better. 1446 01:30:38,200 --> 01:30:39,680 How, might you think? 1447 01:30:41,340 --> 01:30:42,340 Yeah. 1448 01:30:42,600 --> 01:30:46,360 So using a loop. And why? Why are you encouraging me to use a loop, even 1449 01:30:46,360 --> 01:30:47,360 it works as is? 1450 01:30:48,820 --> 01:30:52,680 Yeah, so to summarize, it's just easier to use a loop because I could specify 1451 01:30:52,680 --> 01:30:56,880 explicitly in one place how many times I wanted to loop. And moreover, frankly, 1452 01:30:57,060 --> 01:31:00,960 any time you are copying and pasting something in code or dragging the same 1453 01:31:00,960 --> 01:31:03,940 thing again and again, odds are you're doing something foolish. Why? 1454 01:31:04,360 --> 01:31:08,300 Because you're repeating yourself unnecessarily. And this is a bit 1455 01:31:08,300 --> 01:31:12,880 suppose I want to change this program later so that the cat pauses two seconds 1456 01:31:12,880 --> 01:31:15,620 in between mouths. Well, obviously, I can just go in here and do two. 1457 01:31:15,920 --> 01:31:16,960 But what if I forget? 1458 01:31:17,520 --> 01:31:21,300 And suppose this program isn't like five or six puzzle pieces. Suppose it's 50 1459 01:31:21,300 --> 01:31:23,020 or 60 or 500 or 600. 1460 01:31:23,460 --> 01:31:26,420 Eventually, I or a colleague I'm working with is going to screw up. They're 1461 01:31:26,420 --> 01:31:30,100 going to change a value in one place, forget to change it in another. So why 1462 01:31:30,100 --> 01:31:32,280 you inviting the probability of making a mistake? 1463 01:31:32,540 --> 01:31:36,800 Just simplify things so that you only have to change inputs in one place. 1464 01:31:37,120 --> 01:31:41,060 So how can I do this? Let me zoom out. Let me throw most of this duplication 1465 01:31:41,060 --> 01:31:44,120 away, leaving me with just the play and the wait function. 1466 01:31:44,480 --> 01:31:48,480 Let me now, under Control as well, Grab one of these. I could, for instance, 1467 01:31:48,640 --> 01:31:49,640 repeat as follows. 1468 01:31:49,800 --> 01:31:50,800 Let me grab a repeat. 1469 01:31:51,040 --> 01:31:53,740 I'm going to have to kind of move these in two parts. So I'm going to move this 1470 01:31:53,740 --> 01:31:56,540 down. It's too small, but it will grow to fit the right shape. 1471 01:31:56,880 --> 01:31:58,740 Then let me reattach it up here. 1472 01:31:58,960 --> 01:32:01,100 Let me change the default 10 to a 3. 1473 01:32:01,340 --> 01:32:04,600 And now I think I've done exactly what you were encouraging, which is simplify. 1474 01:32:04,760 --> 01:32:05,860 And I click Play Now. 1475 01:32:09,880 --> 01:32:10,880 Meow. And? 1476 01:32:12,080 --> 01:32:17,060 Yeah, so still correct, but arguably better designed as a result. I can keep 1477 01:32:17,060 --> 01:32:20,700 things simple and change things now in just one place, and it will continue to 1478 01:32:20,700 --> 01:32:23,060 work. But this is getting a little tedious now, I claim. 1479 01:32:23,280 --> 01:32:26,260 Why am I re -implementing the idea of meowing? 1480 01:32:26,520 --> 01:32:31,960 Wouldn't MIT have been better to have just implemented a meow puzzle piece for 1481 01:32:31,960 --> 01:32:34,740 us? Because the whole thing is themed around a cat. Why is there not a meow 1482 01:32:34,740 --> 01:32:35,599 puzzle piece? 1483 01:32:35,600 --> 01:32:39,340 Why do I need to go through all of this complexity to build that functionality? 1484 01:32:39,860 --> 01:32:42,940 Well, what's nice about Scratch is And what's nice about programming languages 1485 01:32:42,940 --> 01:32:46,800 in general is you can generally invent your own puzzle pieces, your own 1486 01:32:46,800 --> 01:32:49,160 function, and then use and reuse them. 1487 01:32:49,400 --> 01:32:52,160 So let me go ahead and do this. I'm going to go under my block. 1488 01:32:52,560 --> 01:32:53,560 in pink down here. 1489 01:32:53,840 --> 01:32:56,000 I'm going to go ahead and click Make a Block. 1490 01:32:56,300 --> 01:32:59,020 And I'm going to be prompted with this interface here. And I'm going to call 1491 01:32:59,020 --> 01:33:02,800 this block literally Meow, because apparently MIT forgot to implement it 1492 01:33:02,940 --> 01:33:04,900 And I'm just going to go ahead and immediately click OK. 1493 01:33:05,360 --> 01:33:09,680 And what you'll see now is two things. One, on the screen, I've been given this 1494 01:33:09,680 --> 01:33:12,940 placeholder pink piece that says Define Meow. 1495 01:33:13,630 --> 01:33:14,449 as follows. 1496 01:33:14,450 --> 01:33:19,290 So anything I attach to the bottom of that define block is going to define the 1497 01:33:19,290 --> 01:33:20,290 meaning of meowing. 1498 01:33:20,510 --> 01:33:25,450 And at top left, notice what I have under my blocks. I now have a pink 1499 01:33:25,450 --> 01:33:30,190 piece called meow that is a new function that will do whatever that other block 1500 01:33:30,190 --> 01:33:32,530 of code tells the cat to do. 1501 01:33:32,750 --> 01:33:35,250 So what do I want to do here? Well, I'm going to keep it simple for now. 1502 01:33:35,800 --> 01:33:40,480 I'm going to move the play sound meow until done and wait two seconds. So 1503 01:33:40,480 --> 01:33:42,240 change it back to one second to move things along. 1504 01:33:42,600 --> 01:33:48,440 And now let me drag the meow puzzle piece over to my loop such that now 1505 01:33:48,440 --> 01:33:51,540 it going to do? It's going to meow three times. And just to be... 1506 01:33:52,280 --> 01:33:56,860 dramatic, out of sight, out of mind. Let me, for no technical reason, just drag 1507 01:33:56,860 --> 01:34:00,460 this all the way to the bottom of the screen and then scroll back up just to 1508 01:34:00,460 --> 01:34:03,640 make the point visually that now meowing exists. 1509 01:34:03,900 --> 01:34:09,080 That is an implementation detail that we can abstract away, not caring how it 1510 01:34:09,080 --> 01:34:13,380 exists, because I now know at a higher conceptual level, if I want to meow, I 1511 01:34:13,380 --> 01:34:17,280 just use the meow puzzle piece. And I or someone else dealt with already how to 1512 01:34:17,280 --> 01:34:18,280 implement meowing. 1513 01:34:18,400 --> 01:34:19,780 So now let me go ahead and hit play. 1514 01:34:24,910 --> 01:34:29,610 OK, so same exact code, but arguably better design, because I've now given 1515 01:34:29,610 --> 01:34:33,730 myself reusable code. So I don't have to copy -paste those several blocks. I can 1516 01:34:33,730 --> 01:34:35,090 just use meow again and again. 1517 01:34:35,310 --> 01:34:38,770 But let's make one refinement. Let me actually scroll down to where I did, in 1518 01:34:38,770 --> 01:34:42,910 fact, implement this. Let me Control -click or right -click on it. And let me 1519 01:34:42,910 --> 01:34:47,590 edit the pink block that I created a moment ago, because I want to practice 1520 01:34:47,590 --> 01:34:48,670 I've been preaching about input. 1521 01:34:48,910 --> 01:34:50,950 So I don't want this function just to be called meow. 1522 01:34:51,250 --> 01:34:54,840 I want this function to also take an input. 1523 01:34:55,060 --> 01:34:58,580 And just for consistency with our use of n earlier, which in computer science 1524 01:34:58,580 --> 01:35:00,900 generally means number, let me meow. 1525 01:35:01,560 --> 01:35:06,020 n times. And just so that this puzzle piece is even more programmer friendly, 1526 01:35:06,320 --> 01:35:11,300 let me add just a textual label that has no technical significance other than to 1527 01:35:11,300 --> 01:35:15,140 make this function sort of read left to right in a more English friendly way. 1528 01:35:15,380 --> 01:35:16,660 Meow n times. 1529 01:35:17,080 --> 01:35:18,080 Let me click OK. 1530 01:35:18,280 --> 01:35:22,760 And now notice this thing at the bottom has changed such that it's not only 1531 01:35:22,760 --> 01:35:28,080 called meow, there's explicit mention of n, which is a circle, which is exactly 1532 01:35:28,080 --> 01:35:30,440 the variable shape that we saw earlier. 1533 01:35:30,970 --> 01:35:31,970 when it was called answer. 1534 01:35:32,110 --> 01:35:33,650 This is not a return value, though. 1535 01:35:34,130 --> 01:35:37,350 This is what, again, we're going to call an argument, an input two -way 1536 01:35:37,350 --> 01:35:38,870 function. So let me do that. 1537 01:35:39,130 --> 01:35:42,310 I'm going to move this back up to the top so I can see everything in one 1538 01:35:43,010 --> 01:35:47,810 And I'm going to make one modification, because my goal now is to make a new and 1539 01:35:47,810 --> 01:35:52,590 improved version of meowing that actually takes into account how many 1540 01:35:52,590 --> 01:35:53,590 want the cat to meow. 1541 01:35:53,770 --> 01:35:59,470 So instead of using a loop in my own program under when green flag clicked, 1542 01:35:59,470 --> 01:36:00,650 going to detach this temporarily. 1543 01:36:01,190 --> 01:36:02,390 I'm going to move this away. 1544 01:36:02,930 --> 01:36:04,970 I'm going to move this code over here. 1545 01:36:05,450 --> 01:36:07,090 And I'm going to reattach it here. 1546 01:36:07,740 --> 01:36:12,440 So focusing for the moment on just the left, meow is now defined as repeating 1547 01:36:12,440 --> 01:36:16,880 three times the following two functions, play sound and wait. But that's not 1548 01:36:16,880 --> 01:36:18,540 quite right. I want to get rid of the three. 1549 01:36:18,960 --> 01:36:20,160 So what can I do? 1550 01:36:20,700 --> 01:36:25,600 Because I created this input to the meow function myself a moment ago, I can 1551 01:36:25,600 --> 01:36:30,680 actually drag a copy of it overwrite, that is, change the 3 to be generally an 1552 01:36:30,680 --> 01:36:35,280 n. So now I have a function called meow that will meow any number of times. 1553 01:36:35,520 --> 01:36:41,200 And what's nice now is my actual program that is governed by that green flag, I 1554 01:36:41,200 --> 01:36:46,020 can type in 3, I can type in 10, I can type in 100, and it will just work. And 1555 01:36:46,020 --> 01:36:49,960 henceforth, I can again dramatically sort of scroll this down so we don't 1556 01:36:50,000 --> 01:36:54,840 or care about it anymore. Now my program is a single line whereby this notion of 1557 01:36:54,840 --> 01:37:00,160 meowing has been abstracted away by just defining my own function or custom 1558 01:37:00,160 --> 01:37:01,160 block. 1559 01:37:01,820 --> 01:37:05,900 Questions then about just this idea, this principle of creating your own 1560 01:37:05,900 --> 01:37:08,520 functions to kind of hide implementation details. 1561 01:37:08,820 --> 01:37:11,700 Once you've solved the problem, therefore, you don't want to have to 1562 01:37:11,700 --> 01:37:13,200 that same problem ever again. 1563 01:37:13,780 --> 01:37:15,840 And that's the beauty of programming typically. 1564 01:37:17,600 --> 01:37:19,220 Questions on what here? 1565 01:37:19,840 --> 01:37:20,840 We just did. 1566 01:37:22,990 --> 01:37:24,310 All right. Well, let's do this. 1567 01:37:24,710 --> 01:37:28,250 Let's now make this a little more interactive in code. Let me go to this 1568 01:37:28,250 --> 01:37:31,770 flag. Let me scroll down and just throw all of this hard work away that we have 1569 01:37:31,770 --> 01:37:35,110 copies on the course's website of all of these programs step by step if you want 1570 01:37:35,110 --> 01:37:36,630 to review them in slower detail. 1571 01:37:37,050 --> 01:37:41,270 Let's do this. Under control, turns out there's other ways to loop. There's this 1572 01:37:41,270 --> 01:37:43,610 forever block that will just do something forever. 1573 01:37:44,030 --> 01:37:47,890 So in the forever block, there's some place for some other code. And I'm going 1574 01:37:47,890 --> 01:37:51,790 to move to the control section here and grab one of these, if. 1575 01:37:52,380 --> 01:37:55,240 So one of these conditionals. Let's plug that in here. 1576 01:37:55,480 --> 01:38:00,020 And now notice if, and then there's this sort of trapezoid -like placeholder 1577 01:38:00,020 --> 01:38:02,340 that's going to probably fit what? 1578 01:38:02,820 --> 01:38:04,080 The if is a conditional. 1579 01:38:04,820 --> 01:38:05,920 Forever is a loop. 1580 01:38:06,780 --> 01:38:08,680 Say and so forth have been functions. 1581 01:38:09,000 --> 01:38:10,560 What was the other key term we used? 1582 01:38:11,260 --> 01:38:12,480 So a Boolean expression. 1583 01:38:12,760 --> 01:38:15,800 We need to put one of those yes, no, or true, false questions here. So what are 1584 01:38:15,800 --> 01:38:19,200 those? Well, I've been using Scratch for some years, so I know under sensing 1585 01:38:19,200 --> 01:38:21,400 there's one of these shapes here. 1586 01:38:21,680 --> 01:38:25,540 Touching mouse pointer question mark. The question mark literally evokes the 1587 01:38:25,540 --> 01:38:29,200 whole idea of a Boolean expression being yes, no. It's way too big to fit, but 1588 01:38:29,200 --> 01:38:31,260 it is the right shape. So let me drag it, let go. 1589 01:38:31,640 --> 01:38:32,780 It's going to grow to fill. 1590 01:38:33,080 --> 01:38:37,800 And now let me go to sound. Let me grab that play sound meow until done and put 1591 01:38:37,800 --> 01:38:41,560 it inside that conditional such that what kind of program have I just 1592 01:38:41,560 --> 01:38:42,920 implemented here, arguably? 1593 01:38:44,540 --> 01:38:46,780 What will this program do when I click the green flag? 1594 01:38:49,660 --> 01:38:51,840 Well, nothing at the moment. 1595 01:38:54,040 --> 01:38:59,640 But I'm not touching the cat. So if I move the mouse pointer to the cat, 1596 01:38:59,900 --> 01:39:01,480 again, 1597 01:39:02,440 --> 01:39:05,880 Kind of like implementing the idea of petting a cat, if you will, because I'm 1598 01:39:05,880 --> 01:39:07,820 forever just waiting and waiting and waiting. 1599 01:39:08,060 --> 01:39:13,540 Is the mouse pointer touching that sprite, touching that cat? And only if 1600 01:39:13,540 --> 01:39:15,880 ahead and play that sound meow until done. 1601 01:39:16,100 --> 01:39:19,360 But now we can make things a little more interesting. Let me stop this, and let 1602 01:39:19,360 --> 01:39:22,940 me do something actually completely different. Let me throw all this hard 1603 01:39:22,940 --> 01:39:24,840 away. Let me go under extensions. 1604 01:39:25,140 --> 01:39:28,760 Let me go to video sensing, because lots of laptops, my own included, has a 1605 01:39:28,760 --> 01:39:29,760 little webcam nowadays. 1606 01:39:30,000 --> 01:39:34,660 Let me approve use of that. And you can see me in the frame. And let me do this. 1607 01:39:34,780 --> 01:39:39,240 Let me drag one of these when motion exceeds some measure. And for the trial 1608 01:39:39,240 --> 01:39:43,380 error, I figured out that 50 tends to work well. Let me step out of frame here 1609 01:39:43,380 --> 01:39:44,760 and program off to the side. 1610 01:39:45,100 --> 01:39:51,610 And if I go to play sound meow until done, Notice that this is an alternative 1611 01:39:51,610 --> 01:39:53,330 using when green flag clicked. 1612 01:39:53,550 --> 01:39:57,110 This is a category of block that's constantly waiting for what we'll call 1613 01:39:57,110 --> 01:40:00,550 event. An event is just something that can happen on the screen, a click, a 1614 01:40:00,550 --> 01:40:04,290 drag, a mouse movement, and so forth. So let me zoom out here. 1615 01:40:04,810 --> 01:40:07,670 And now, if I can do this, here we go. 1616 01:40:09,390 --> 01:40:10,390 No, too slow. 1617 01:40:11,770 --> 01:40:12,770 Still too slow. 1618 01:40:12,830 --> 01:40:14,650 Oh, wait, did I click play? Let's see. 1619 01:40:17,420 --> 01:40:18,420 Try again. 1620 01:40:21,080 --> 01:40:22,080 There we go. 1621 01:40:22,200 --> 01:40:26,420 OK, 50 is a little too high, apparently. So let's make this a little gentler. 1622 01:40:26,640 --> 01:40:30,160 10. OK, well, there we go. 1623 01:40:31,700 --> 01:40:32,700 There we go. 1624 01:40:32,780 --> 01:40:37,240 OK, so we've implemented now more physically the idea of actually 1625 01:40:37,240 --> 01:40:38,780 petting a cat. 1626 01:40:39,000 --> 01:40:40,720 So, oh, damn it. 1627 01:40:41,240 --> 01:40:42,240 OK. 1628 01:40:43,880 --> 01:40:45,620 OK, so this is a bug. Like, now. 1629 01:40:46,080 --> 01:40:47,080 That might fall. 1630 01:40:47,160 --> 01:40:51,520 So it's not stopping in response to the red stop sign. So what do you do in 1631 01:40:51,520 --> 01:40:54,660 doubt? Most extremely reboot. For now, I'm just going to close the window. 1632 01:40:56,110 --> 01:41:01,210 OK, so now we've seen all of those primitives that we saw in that 1633 01:41:01,270 --> 01:41:04,690 but incarnated in this graphical programming language. And again, without 1634 01:41:04,690 --> 01:41:07,510 parentheses and semicolons and double quotes and all the punctuation that we 1635 01:41:07,510 --> 01:41:12,250 will introduce before long. But for now, we have the mechanisms in place where 1636 01:41:12,250 --> 01:41:13,930 we can do some really interesting things. 1637 01:41:14,170 --> 01:41:17,770 So in fact, I thought, in the spirit of sort of thinking back on olden times, 1638 01:41:17,890 --> 01:41:21,650 thought I'd open up the very first program I wrote when I actually took, I 1639 01:41:21,650 --> 01:41:26,430 cross -registered in an MIT class and took a class that introduced but 1640 01:41:26,430 --> 01:41:30,110 teachers to scratch. And I implemented this program here called Oscar Time, 1641 01:41:30,270 --> 01:41:34,550 which was a game that used a childhood song that I was a fan of. And it allows 1642 01:41:34,550 --> 01:41:37,350 you to sort of drag trash into a trash can. 1643 01:41:37,570 --> 01:41:40,710 But to bring this to life, and perhaps in exchange for one stress ball, could 1644 01:41:40,710 --> 01:41:44,130 get one brave volunteer who wants to come up and control this here keyboard. 1645 01:41:44,130 --> 01:41:45,350 saw your hand first. Come on up. 1646 01:41:45,930 --> 01:41:46,930 Come on up. 1647 01:41:49,380 --> 01:41:53,280 And you'll see, thanks to the team, we also have this amazing lamppost here 1648 01:41:53,280 --> 01:41:56,160 being on Quincy Street as we are. Do you want to introduce yourself to the 1649 01:41:56,160 --> 01:41:58,320 group? Hi, my name is Anna. 1650 01:41:58,740 --> 01:42:01,460 I'm from Richmond, Virginia, and I'm in Weld. 1651 01:42:02,240 --> 01:42:04,140 Nice, Weld. All right, come on over. 1652 01:42:04,730 --> 01:42:08,390 So here, Andy, you'll have a chance to play the very first game I wrote in 1653 01:42:08,390 --> 01:42:11,710 Scratch, which admittedly is more complicated typically than we would 1654 01:42:11,710 --> 01:42:14,170 a student doing this for the very first time, as in problem set zero. 1655 01:42:14,430 --> 01:42:16,130 But what I'm going to do is full screen this year. 1656 01:42:16,350 --> 01:42:19,030 I'm going to click the green flag. And what you'll see on the screen are these 1657 01:42:19,030 --> 01:42:22,870 instructions. Drag as much falling trash as you can to Oscar's trash can before 1658 01:42:22,870 --> 01:42:24,230 his song ends. And here we go. 1659 01:42:28,590 --> 01:42:30,290 Oh, I love. 1660 01:42:32,240 --> 01:42:35,220 Anything 30 or 50 or 60. 1661 01:42:35,860 --> 01:42:42,680 Anything racket or rotten or rusty. Yes, I love. 1662 01:42:44,300 --> 01:42:45,380 Oh, there we go. 1663 01:42:45,620 --> 01:42:49,160 Nice. So as Anna continues to play, let's kind of tease this apart a little 1664 01:42:49,300 --> 01:42:53,220 So one, there's some costumes on the stage. Like that lamppost is actually 1665 01:42:53,220 --> 01:42:55,660 going to move. But there's a couple of sprites. 1666 01:42:55,920 --> 01:42:58,880 There's the trash can, which seems to be a character unto itself. 1667 01:42:59,160 --> 01:43:02,460 There's this piece of trash that keeps coming back and back. That is a sprite. 1668 01:43:02,720 --> 01:43:05,220 There's now this sneaker, which is another sprite. 1669 01:43:05,440 --> 01:43:09,160 And in fact, notice that Oscar, of course, keeps popping up from his sprite 1670 01:43:09,160 --> 01:43:11,980 in a while. So Oscar seems to have multiple costumes. 1671 01:43:12,340 --> 01:43:15,480 So I offer this as an example is you keep playing, if you would. Very good 1672 01:43:15,480 --> 01:43:16,259 so far. 1673 01:43:16,260 --> 01:43:19,000 This song goes on forever. This was a nightmare to implement, to listen to 1674 01:43:19,000 --> 01:43:19,759 all day long. 1675 01:43:19,760 --> 01:43:24,560 But how do we implement the rest of this? Well, notice that the trash, every 1676 01:43:24,560 --> 01:43:28,740 time she throws it into the trash can, does reappear somewhere different. So 1677 01:43:28,740 --> 01:43:30,360 there's some kind of randomness involved. 1678 01:43:30,600 --> 01:43:34,800 And indeed, Scratch will let you pick random numbers in a range. So maybe it 1679 01:43:34,800 --> 01:43:35,800 could be negative 240. 1680 01:43:36,000 --> 01:43:40,600 Maybe it could be positive 240 at the 180 point on the top of the screen. So 1681 01:43:40,600 --> 01:43:42,480 can sort of randomly put things on the screen. 1682 01:43:42,730 --> 01:43:46,750 There's apparently what kind of construct that makes the trash fall 1683 01:43:46,750 --> 01:43:49,290 again. I think no one's listening to me. They're all just watching you. But 1684 01:43:49,290 --> 01:43:54,130 what's making the trash fall from top to bottom? 1685 01:43:54,830 --> 01:43:58,670 So it's actually some kind of loop, because there's a motion block inside of 1686 01:43:58,670 --> 01:44:02,450 forever loop, probably, that just keeps moving the trash one pixel, one pixel, 1687 01:44:02,490 --> 01:44:06,130 one pixel, one pixel, one pixel, creating the illusion, therefore, of 1688 01:44:06,350 --> 01:44:10,870 And if we can crank the song a little bit more, you'll see that this is all 1689 01:44:10,870 --> 01:44:11,829 synchronized now. 1690 01:44:11,830 --> 01:44:13,630 Because they're trash. 1691 01:44:14,810 --> 01:44:17,730 Oh, I love trash. 1692 01:44:18,810 --> 01:44:22,090 Anything dirty or stingy or sassy. 1693 01:44:23,820 --> 01:44:26,380 The song keeps going forever, seemingly. 1694 01:44:26,960 --> 01:44:31,360 And now notice more and more sprites are appearing because they waited for... 1695 01:44:31,360 --> 01:44:32,360 Here we go, climax. 1696 01:44:32,480 --> 01:44:33,020 All 1697 01:44:33,020 --> 01:44:40,260 right, 1698 01:44:40,320 --> 01:44:41,940 a big round of applause for Hannah. 1699 01:44:42,520 --> 01:44:43,520 Nicely done. 1700 01:44:43,580 --> 01:44:44,960 There you go, there you go. 1701 01:44:45,480 --> 01:44:48,540 All right. So this is an interminable song. 1702 01:44:48,780 --> 01:44:52,160 And indeed, I spent hours building that. And just listening to that song on loop 1703 01:44:52,160 --> 01:44:53,580 was not the best way to program. 1704 01:44:53,800 --> 01:44:58,360 But the goal here is to really use it as just an intellectual exercise as to how 1705 01:44:58,360 --> 01:44:59,360 that was implemented. 1706 01:44:59,520 --> 01:45:03,080 And we won't do the entire thing in detail, because I will say back in the 1707 01:45:03,080 --> 01:45:05,700 when I was younger, I didn't necessarily write the cleanest code. 1708 01:45:05,920 --> 01:45:10,320 And in fact, if we see inside this and we poke around the bottom of the screen 1709 01:45:10,320 --> 01:45:12,040 here, you can see all of my different sprites. 1710 01:45:12,510 --> 01:45:14,070 And the code is kind of complex. 1711 01:45:14,410 --> 01:45:15,970 Things just kind of escalated quickly. 1712 01:45:16,210 --> 01:45:19,910 But I did not set out and write all of these programs all at once for each 1713 01:45:19,910 --> 01:45:24,210 sprite. I pretty much took baby steps, so to speak. And so for instance, let me 1714 01:45:24,210 --> 01:45:28,450 open up just a few sample building blocks here that speak to this, that are 1715 01:45:28,450 --> 01:45:29,450 written in advance. 1716 01:45:29,680 --> 01:45:33,560 So here's version 0. Computer scientists typically start counting at 0. And let 1717 01:45:33,560 --> 01:45:37,020 me show you this example here that only has two sprites on the screen. 1718 01:45:37,240 --> 01:45:40,360 We have Oscar the trash can, and we have the piece of trash. 1719 01:45:40,640 --> 01:45:43,020 And now notice, what does Oscar do? 1720 01:45:43,500 --> 01:45:48,460 Well, let me go ahead and zoom in on this script, as it's called. A program 1721 01:45:48,460 --> 01:45:52,640 script. When the green flag is clicked, Oscar switches his costume to Oscar 1. 1722 01:45:52,820 --> 01:45:55,020 That's his default costume where the lid is closed. 1723 01:45:55,320 --> 01:45:57,140 Then Oscar does this forever. 1724 01:45:57,820 --> 01:46:02,680 If Oscar is touching the mouse pointer, change the costume to Oscar 2. 1725 01:46:03,230 --> 01:46:05,330 Otherwise, change it back to Oscar 1. 1726 01:46:05,570 --> 01:46:09,530 So that whole idea of animation where Oscar's popping in and out is just like 1727 01:46:09,530 --> 01:46:14,070 quick costume change based on a loop inside of which is a conditional waiting 1728 01:46:14,070 --> 01:46:17,230 for the cursor, like Anna did, to get near the trash can. 1729 01:46:17,890 --> 01:46:22,210 Meanwhile, if we look at the piece of trash here, notice that the trash is 1730 01:46:22,210 --> 01:46:25,050 actually not doing anything in this first version because I didn't even 1731 01:46:25,050 --> 01:46:27,730 implement falling first. So let me hit the green flag. 1732 01:46:28,130 --> 01:46:31,890 Nothing is happening in this very first version. But notice, if I click on the 1733 01:46:31,890 --> 01:46:36,980 trash and drag, as soon as I'm touching Oscar, there comes that trash can lid. 1734 01:46:37,280 --> 01:46:41,780 And it was just the result of making this one program respond to that input. 1735 01:46:42,140 --> 01:46:43,360 All right, what did I do next? 1736 01:46:43,660 --> 01:46:47,380 Well, next, after taking that single baby step, I added one other feature. 1737 01:46:47,380 --> 01:46:48,840 see inside this version one. 1738 01:46:49,140 --> 01:46:54,040 Again, Oscar is behaving the exact same way. But notice this time, the trash is 1739 01:46:54,040 --> 01:46:55,200 designed to do the following. 1740 01:46:55,900 --> 01:46:59,700 First, I'm telling the program that the drag mode is draggable. That is, I want 1741 01:46:59,700 --> 01:47:02,200 the trash to be movable when the user clicks on it. 1742 01:47:02,480 --> 01:47:06,020 Then I tell the piece of trash to go to a random x location. 1743 01:47:06,240 --> 01:47:11,520 x is a horizontal, so it's going somewhere between 0 and 240, but all the 1744 01:47:11,520 --> 01:47:12,860 the top of the screen, 180. 1745 01:47:13,440 --> 01:47:17,620 Then forever, the piece of trash just changes by negative 1. So it just moves 1746 01:47:17,620 --> 01:47:19,060 down and down and down. 1747 01:47:19,340 --> 01:47:22,580 And without looking at the second script yet, let me just hit Play. And notice, 1748 01:47:22,740 --> 01:47:26,440 without even doing anything, And eventually, once there was lots of trash 1749 01:47:26,440 --> 01:47:29,560 falling, like Anna was struggling to keep up with this, it's just moving one 1750 01:47:29,560 --> 01:47:34,880 pixel at a time forever until, thankfully, MIT does stop things 1751 01:47:34,880 --> 01:47:38,240 they hit the bottom, lest a six -year -old get upset that all of a sudden 1752 01:47:38,240 --> 01:47:41,100 sprite is gone forever. So there is some special casing there. 1753 01:47:41,380 --> 01:47:43,460 But what else is this trash doing? 1754 01:47:43,860 --> 01:47:44,920 Let me zoom in here. 1755 01:47:45,660 --> 01:47:50,500 The piece of trash, also when the green flag is clicked, it's forever asking 1756 01:47:50,500 --> 01:47:53,060 this question. If you are touching Oscar. 1757 01:47:53,770 --> 01:47:59,630 Then pick a new random location between 0 and 240 at positive 180 and go back to 1758 01:47:59,630 --> 01:48:00,249 the top. 1759 01:48:00,250 --> 01:48:04,410 So in other words, as soon as this piece of trash is dragged over to Oscar like 1760 01:48:04,410 --> 01:48:05,410 this and I let go. 1761 01:48:06,160 --> 01:48:10,340 It recreates itself at the top. It's just sort of teleporting to the top, and 1762 01:48:10,340 --> 01:48:11,820 thus was born this feature. 1763 01:48:12,100 --> 01:48:15,260 And I won't slog through all of the individual features here, but if we do 1764 01:48:15,260 --> 01:48:18,860 one more and see inside this one. So now, let me go ahead and hit Play. 1765 01:48:19,160 --> 01:48:22,400 Notice at the top left of the screen, there's a score, currently 0. 1766 01:48:22,720 --> 01:48:26,800 But now, when I click the trash and let go, notice that the score is being 1767 01:48:26,800 --> 01:48:27,860 incremented by 1. 1768 01:48:28,100 --> 01:48:31,120 And this, in fact, is how, Anas, your score kept going higher and higher and 1769 01:48:31,120 --> 01:48:34,880 higher. Every time I noticed, oh, the trash is touching Oscar, let's not only 1770 01:48:34,880 --> 01:48:38,670 teleport, let's also increment a variable. And we didn't see this before, 1771 01:48:38,670 --> 01:48:44,550 I go to this oscar scratch now, you'll see that it is exactly the same. But if 1772 01:48:44,550 --> 01:48:50,950 now go to the trash piece here, And we go to when green flag clicked, you'll 1773 01:48:50,950 --> 01:48:54,190 that I'm initializing a variable in orange called score to zero. 1774 01:48:54,610 --> 01:48:59,130 But if we scroll down to the bottom, Oscar is also doing another thing in 1775 01:48:59,130 --> 01:49:02,990 parallel at the same time. When the green flag is clicked, Oscar is forever 1776 01:49:02,990 --> 01:49:07,230 checking, is the piece of trash touching Oscar? 1777 01:49:07,470 --> 01:49:12,710 If so, change the score by one, and then go to top, which is another location. 1778 01:49:13,180 --> 01:49:14,180 on there, that screen. 1779 01:49:14,280 --> 01:49:17,380 So in other words, even though at a glance something like Oscar time might 1780 01:49:17,380 --> 01:49:21,280 very complicated and it did take me hours, the goal, especially with 1781 01:49:21,280 --> 01:49:25,360 zero, is not going to be to bite off all of that at once, but to take proverbial 1782 01:49:25,360 --> 01:49:29,080 baby steps. Implement one tiny feature so that you feel like you're making 1783 01:49:29,080 --> 01:49:30,760 progress. Add another feature, another. 1784 01:49:31,080 --> 01:49:34,740 And invariably, you might run out of time and not get to the best version of 1785 01:49:34,740 --> 01:49:35,339 your vision. 1786 01:49:35,340 --> 01:49:38,440 But hopefully, it'll be good. Hopefully, it'll be better. But you'll have these 1787 01:49:38,440 --> 01:49:41,740 sort of mental milestones, hoping that you at least get to that point, because 1788 01:49:41,740 --> 01:49:45,530 as you will soon discover everything in the world of programming, unfortunately, 1789 01:49:45,830 --> 01:49:50,450 takes longer than you might expect. That was true for me 25 years ago, and it's 1790 01:49:50,450 --> 01:49:51,690 still true today. 1791 01:49:52,410 --> 01:49:56,270 Well, let me introduce one final set of examples here, this one written by one 1792 01:49:56,270 --> 01:49:58,230 of your own predecessors, a former student. 1793 01:49:58,550 --> 01:50:03,470 Let me go ahead and open up three baby steps, if you will, toward an end of 1794 01:50:03,470 --> 01:50:08,090 implementing a game called Ivy's Hardest Game, whereby it's now more 1795 01:50:08,090 --> 01:50:09,830 interactive, quite like Oscar time. 1796 01:50:10,050 --> 01:50:13,970 So at top right here, notice, and I'll zoom in, we have this world that's 1797 01:50:13,970 --> 01:50:18,110 initially very simple. Two black lines, two walls, if you will, and a Harvard 1798 01:50:18,110 --> 01:50:22,140 sprite in the middle. But when you click the green flag, notice, that nothing 1799 01:50:22,140 --> 01:50:24,800 happens initially except that the sprite jumps to the middle. 1800 01:50:25,040 --> 01:50:30,920 But I can hit the up key or the down key or the left key or the right key. But 1801 01:50:30,920 --> 01:50:35,280 if I try to go too far, even though it's not the edge of the world, it's only 1802 01:50:35,280 --> 01:50:38,520 touching that there black line, it's still going to stop as well. 1803 01:50:38,760 --> 01:50:41,840 So intuitively, how could you implement that type of program? 1804 01:50:42,100 --> 01:50:46,880 How could you get a sprite, from what we've seen, to respond to up, down, 1805 01:50:47,080 --> 01:50:49,960 right, but actually move when I touch my arrow keys? 1806 01:50:51,690 --> 01:50:53,050 Like what does it mean to move? Yeah. 1807 01:50:55,740 --> 01:50:59,200 Exactly. So much like with representing information, at the end of the day, all 1808 01:50:59,200 --> 01:51:02,920 we've got is zeros and ones. When it comes to algorithms, at the moment, all 1809 01:51:02,920 --> 01:51:07,320 have are functions and loops and conditionals and Boolean expressions and 1810 01:51:07,320 --> 01:51:08,320 some more things too. 1811 01:51:08,420 --> 01:51:10,860 But there's not all that much we have at our disposal. 1812 01:51:11,100 --> 01:51:14,880 So let me zoom out from this. And let me actually show you what the Harvard 1813 01:51:14,880 --> 01:51:16,660 sprite is doing. It's doing this. 1814 01:51:16,880 --> 01:51:21,180 When I go up to the green flag here, the Harvard sprite is going to 0, 0, so 1815 01:51:21,180 --> 01:51:24,980 dead center in the middle. And then it's forever doing two things, listening for 1816 01:51:24,980 --> 01:51:29,180 the key. and feeling for wall, left and right. Now, those are not puzzle pieces 1817 01:51:29,180 --> 01:51:33,880 that come with Scratch. I created my own custom blocks, my own functions, to 1818 01:51:33,880 --> 01:51:34,880 implement those ideas. 1819 01:51:35,360 --> 01:51:38,860 Let's not abstract away for now. Let's actually look at these features. And 1820 01:51:38,860 --> 01:51:42,440 indeed, to your instincts at left here, what does it mean to listen for the 1821 01:51:42,440 --> 01:51:47,980 keyboard? Well, if the up arrow key is pressed, change y by 1. Move up. 1822 01:51:48,490 --> 01:51:53,950 If the down arrow key is pressed, change y by negative 1. If the right arrow key 1823 01:51:53,950 --> 01:51:58,690 is pressed, change x by 1. If the left arrow key is pressed, change x by 1824 01:51:58,690 --> 01:52:02,210 negative 1. So take sort of all the magic out of moving up, down, left, 1825 01:52:02,210 --> 01:52:06,610 just quantizing it as plus, minus, this, and that. It's all numbers indeed at 1826 01:52:06,610 --> 01:52:07,469 the end of the day. 1827 01:52:07,470 --> 01:52:08,730 But what else is it doing? 1828 01:52:09,110 --> 01:52:12,950 Notice that it did indeed bounce off the wall. So my other custom function, 1829 01:52:13,070 --> 01:52:16,950 which I chose feelForWalls to kind of evoke this idea, it's asking two 1830 01:52:16,950 --> 01:52:21,980 questions. If you're touching the left wall, then change x by 1. So bounce in 1831 01:52:21,980 --> 01:52:25,640 the other direction. Else, if you're touching the right wall, bounce in the 1832 01:52:25,640 --> 01:52:26,640 negative 1 direction. 1833 01:52:26,880 --> 01:52:29,660 And so what are left wall and right wall? 1834 01:52:30,100 --> 01:52:33,080 I mean, I kind of cheated. I just used two more sprites. These sprites are 1835 01:52:33,080 --> 01:52:35,200 literally nothing except black lines. 1836 01:52:35,500 --> 01:52:40,200 But because they exist, I can ask that question in my conditional saying, are 1837 01:52:40,200 --> 01:52:43,980 you touching those other sprites? And I could have colored them any way I want. 1838 01:52:44,160 --> 01:52:48,180 But this is enough, if I zoom in, to implement this idea of going up, down, 1839 01:52:48,320 --> 01:52:53,100 left, and right. and preventing the sprite from leaving that little world. 1840 01:52:53,340 --> 01:52:57,680 All right, so if you'll agree that there's a way now to implement motion, 1841 01:52:57,760 --> 01:53:01,600 down, left, right, let's go ahead and implement this idea by adding a rival 1842 01:53:01,600 --> 01:53:02,880 the mix, like a Yale sprite. 1843 01:53:03,140 --> 01:53:06,620 And what the Yale sprite is going to do, if I click the green flag, is this. 1844 01:53:07,200 --> 01:53:10,340 So Harvard, at the moment, is still going to be movable with the arrow keys 1845 01:53:10,360 --> 01:53:11,360 down, left, right. 1846 01:53:11,420 --> 01:53:14,660 But Yale, for better or for worse, is just going to mindlessly bounce back and 1847 01:53:14,660 --> 01:53:17,760 forth from left to right forever, it would seem, the operative word being 1848 01:53:17,760 --> 01:53:21,360 forever. So how is that working? Well, let's look. Here is the Yale sprite at 1849 01:53:21,360 --> 01:53:22,318 the bottom. 1850 01:53:22,320 --> 01:53:24,660 Let's zoom in on its actual code here. 1851 01:53:24,880 --> 01:53:26,960 The Yale sprite starts at 0, 0. 1852 01:53:27,180 --> 01:53:30,560 It points in direction 90 degrees, which means left, right, essentially. 1853 01:53:30,920 --> 01:53:32,440 And then it forever does this. 1854 01:53:32,840 --> 01:53:37,660 If touching the left wall or touching the right wall, turn around 180 degrees. 1855 01:53:37,980 --> 01:53:41,900 So I don't want the Yale sprite to just stop by moving it one pixel to bounce 1856 01:53:41,900 --> 01:53:42,818 off slightly. 1857 01:53:42,820 --> 01:53:46,500 I want it to wrap around and just keep going and going and going forever. 1858 01:53:46,920 --> 01:53:49,000 And that's it. Everything else is the same. 1859 01:53:49,260 --> 01:53:54,320 So one final flourish. Let's add a more formidable adversary like MIT here, 1860 01:53:54,440 --> 01:53:58,660 whereby if I zoom in and hit Play, notice that if I move the Harvard 1861 01:53:59,410 --> 01:54:01,110 MIT comes chasing me now. 1862 01:54:01,390 --> 01:54:04,450 Now, how is this actually working? Yale is just kind of doing its thing, 1863 01:54:04,530 --> 01:54:07,350 bouncing back and forth. Now MIT is really latched on to me. 1864 01:54:08,010 --> 01:54:11,990 And it's following me up, down, left, right. So how is that logic now working? 1865 01:54:12,130 --> 01:54:14,870 Well, again, it's probably doing something forever, because that's why 1866 01:54:14,870 --> 01:54:16,850 continually doing it. Let's click on MIT. 1867 01:54:17,270 --> 01:54:20,310 This too is pretty simple, even though it's a pretty fancy idea. 1868 01:54:20,890 --> 01:54:25,230 Initially, the MIT sprite goes to a random position. But thereafter, it 1869 01:54:25,230 --> 01:54:29,290 points toward the Harvard logo outline, which is just the long name that your 1870 01:54:29,290 --> 01:54:31,390 predecessor or former student gave the name. 1871 01:54:31,760 --> 01:54:32,579 for that sprite. 1872 01:54:32,580 --> 01:54:35,340 And then it moves one step, one step, one step. 1873 01:54:35,720 --> 01:54:37,180 So suppose this were an actual game. 1874 01:54:37,460 --> 01:54:40,620 And in games, things get harder and harder. The adversary moves faster and 1875 01:54:40,620 --> 01:54:45,720 faster. How could we make MIT even faster by changing just one thing here? 1876 01:54:48,300 --> 01:54:49,420 How do we level up? 1877 01:54:51,030 --> 01:54:55,110 Change the one to two pixels at a time, two steps at a time. So let's see that. 1878 01:54:55,270 --> 01:54:56,990 Let's go ahead and zoom out. Let's hit Play. 1879 01:54:57,230 --> 01:55:00,730 And now notice that MIT is coming in much faster this time. 1880 01:55:01,430 --> 01:55:03,630 So it wasn't noticeably faster. 1881 01:55:03,930 --> 01:55:07,410 Let's do this. Let's move 10 steps at a time, so 10 steps faster than 1882 01:55:07,410 --> 01:55:08,410 originally. 1883 01:55:09,130 --> 01:55:12,630 And now notice it's kind of twitching back and forth in this way. Why? 1884 01:55:12,850 --> 01:55:16,470 Well, probably, if we worked out the math, probably the MIT sprite is 1885 01:55:16,470 --> 01:55:19,690 the sprite and it's bouncing off of it. But then it's realizing, oh, I went too 1886 01:55:19,690 --> 01:55:23,450 far. Wait a minute, I'm still touching it. Let me move down. So you can get 1887 01:55:23,450 --> 01:55:26,690 these perverse situations where there is actually a bug, be it logical or 1888 01:55:26,690 --> 01:55:30,090 aesthetical. But in this case, we probably want to fix that. So 10 is 1889 01:55:30,090 --> 01:55:32,370 too fast for this to work particularly well. 1890 01:55:32,630 --> 01:55:36,670 But the final, final flourish here really is to show you the actual version 1891 01:55:36,670 --> 01:55:40,330 game. that one of your predecessors, a past classmate, actually implemented. 1892 01:55:40,590 --> 01:55:44,570 Before thereafter, we will adjourn for cake in the transept, which is the CS50 1893 01:55:44,570 --> 01:55:50,810 tradition. But can we get one more final volunteer to come on up to play Ivy's 1894 01:55:50,810 --> 01:55:54,010 hardest game? I'm seeing your hand most enthusiastically there. Yeah, come on 1895 01:55:54,010 --> 01:55:55,450 down. Very happily. 1896 01:56:00,650 --> 01:56:05,350 In just a moment, we will indeed adjourn, but the goal here now is going 1897 01:56:05,350 --> 01:56:08,590 to navigate a maze that's a little more difficult than the last. 1898 01:56:08,870 --> 01:56:12,370 Let's have you first, though, introduce yourselves to your classmates in front. 1899 01:56:12,750 --> 01:56:17,570 Hi, y 'all. I'm Eric. I'm from Philadelphia, and I'm also from Hollis 1900 01:56:18,870 --> 01:56:20,370 One person from Hollis. Nice. 1901 01:56:20,590 --> 01:56:21,750 Okay. Welcome. 1902 01:56:21,970 --> 01:56:24,810 All right. So, Eric, go ahead and take the keyboard here. It, too, will be all 1903 01:56:24,810 --> 01:56:27,550 about up, down, left, right as soon as you click the green flag. 1904 01:56:28,330 --> 01:56:29,450 And if we can crank the music. 1905 01:56:33,730 --> 01:56:38,450 So notice the black walls are a little more involved than last time, but the 1906 01:56:38,450 --> 01:56:41,670 goal is to get to the sprite all the way at right and just touch it, at which 1907 01:56:41,670 --> 01:56:45,770 point you move to the next level. At the next level, of course, has Yale doing 1908 01:56:45,770 --> 01:56:46,970 its thing back and forth. 1909 01:56:48,730 --> 01:56:50,110 But you've made it to level three. 1910 01:56:50,630 --> 01:56:55,690 But now there's two Yale. So another sprite is in the mix that's randomly 1911 01:56:55,690 --> 01:56:57,830 a little different in terms of direction. 1912 01:56:59,590 --> 01:57:00,590 Three Yale. 1913 01:57:04,590 --> 01:57:06,830 Next level, MIT's in. 1914 01:57:10,110 --> 01:57:11,110 Nice. 1915 01:57:16,400 --> 01:57:17,920 The walls are now gone. 1916 01:57:20,760 --> 01:57:21,780 Princeton's in the mix. 1917 01:57:27,300 --> 01:57:28,300 Nice. 1918 01:57:33,820 --> 01:57:35,120 Two Princetons. 1919 01:57:38,160 --> 01:57:39,160 Oh. 1920 01:57:43,030 --> 01:57:44,270 Okay, new life. 1921 01:57:49,570 --> 01:57:54,930 Okay, another life. 1922 01:57:55,310 --> 01:57:56,310 Nice, nice. 1923 01:58:00,410 --> 01:58:01,410 Nice. 1924 01:58:04,090 --> 01:58:06,710 Second to last level. Three Princeton. 1925 01:58:08,670 --> 01:58:10,150 Last level. 1926 01:58:20,190 --> 01:58:24,690 Congratulations. All right. This then was CS50. 1927 01:58:24,910 --> 01:58:27,110 Welcome aboard. Cake is now served. 183531

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